Class: Spot::Prime

Inherits:
Object
  • Object
show all
Defined in:
lib/spot/prime.rb

Constant Summary collapse

@@ignore =
YAML.load(File.read("#{File.dirname(__FILE__)}/ignore.yml"))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, compare, options = {}) ⇒ Prime

Returns a new instance of Prime.



13
14
15
16
17
18
19
20
21
22
# File 'lib/spot/prime.rb', line 13

def initialize(results, compare, options = {})
  @options = {
    popularity: 7,
    exclude: 2,
    limit: 0.7,
    offset: 10
  }.merge(options)

  @results, @compare = results, compare
end

Class Method Details

.ignore?(this, compare = nil) ⇒ Boolean

Returns Boolean.

Returns:

  • (Boolean)

    Boolean



67
68
69
70
71
# File 'lib/spot/prime.rb', line 67

def self.ignore?(this, compare = nil)
  @@ignore.
    reject{ |value| compare.to_s.match(/#{value}/i) }.
    map{ |value| this.match(/#{value}/i) }.any?
end

Instance Method Details

#clean(value) ⇒ Object

Returns String A cleaned string.

Returns:

  • String A cleaned string



58
59
60
# File 'lib/spot/prime.rb', line 58

def clean(value)
  Spot::Clean.new(value).process
end

#resultsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spot/prime.rb', line 27

def results
  @results.sort_by(&:popularity).map do |result|
    match = clean(result.to_s).split(/\s+/).sort
    raw = clean(@compare).split(/\s+/).sort

    if raw.length < match.length
      diff = (match - raw)
      res = diff.length.to_f / match.length
    else
      diff = (raw - match)
      res = diff.length.to_f / raw.length
    end

    if diff.length > 1 and not match.map{ |m| diff.include?(m) }.all?
      res =+ diff.map do |value|
        match.map { |m| Levenshtein.normalized_distance(value, m) }.inject(:+)
      end.inject(:+) / @options[:offset]
    end
    
    [res - result.popularity / @options[:popularity], result]
  end.reject do |_, result|
    Spot::Prime.ignore?(result.to_s, @compare)
  end.sort_by do |distance, _|
    distance
  end.map(&:last)
end