Class: MathMetadata::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/math_metadata_lookup/lookup.rb

Overview

Main class for searching through all sites

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Lookup

:sites can be :all or array of allowed sites ([:mrev, :zbl])



11
12
13
14
# File 'lib/math_metadata_lookup/lookup.rb', line 11

def initialize( opts={} )
  @options = { :sites => :all, :verbose => true }.merge(opts)
  @sites = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

calls method for each site



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/math_metadata_lookup/lookup.rb', line 17

def method_missing(meth, *args)
  result = []

  sites = SITES.dup
  if (@options[:sites] != :all) or @options[:sites].kind_of?(Array)
    allowed = [@options[:sites]].flatten
    sites.delete_if{|s| not allowed.include?(s::ID) }
  end

  sites.each do |klass|
    site = klass.new(:verbose => @options[:verbose], :nwords => args[0][:nwords])

    entry = {:site => klass::ID, :name => klass::NAME, :url => klass::URL}
    entry[:result] = site.send(meth, *args)

    result << entry
  end

  Result.new(result)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/math_metadata_lookup/lookup.rb', line 8

def options
  @options
end

Instance Method Details

#heuristic(args = {}) ⇒ Object

returns best result for each site



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/math_metadata_lookup/lookup.rb', line 40

def heuristic( args={} )
  opts = {:threshold => 0.6, :authors => []}.merge(args)
  result = Result.new
  
  # use only authors surnames
  args_dup = opts.dup
  args_dup[:authors].map!{|a| a =~ /([^,]+)/; $1 ? $1 : a}
  args_dup[:authors].map!{|a| a =~ /([^ ]+) \S+/; $1 ? $1 : a}
  args_dup[:nwords] = 2
  sites = article(args_dup)

  # query article has to contain full names
  query_article = Article.new( {:title => args[:title].to_s, :authors => args[:authors], :year => args[:year]} )
  sites.each do |site|
    site[:result].to_a.each do |article|
      next if article[:title].to_s.empty?
      article[:similarity] = query_article.similarity(article)
    end
    site[:result].to_a.delete_if{|a| a[:similarity].to_f < opts[:threshold].to_f}
    if site[:result].to_a.size > 0
      site[:result].sort!{|a,b| a[:similarity]<=>b[:similarity]}
      site[:result].reverse!
      site[:result] = [site[:result].to_a.first]
    end
  end

  sites
end

#reference(args = {}) ⇒ Object

parse reference string and execute heuristic to query for article in databases



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/math_metadata_lookup/lookup.rb', line 71

def reference( args={} )
  ref = Reference.new args[:reference]
  pp ref if args[:verbose]

  opts = {:threshold => 0.6}.merge(args)
  opts[:title] = ref.article[:title]
  opts[:authors] = ref.article[:authors]
  opts[:year] = ref.article[:year]

  heuristic opts
end