Class: FuzzyMatch::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_match/record.rb

Overview

Records are the tokens that are passed around when doing scoring and optimizing.

Constant Summary collapse

WORD_BOUNDARY =

“Foo’s” is one word “North-west” is just one word “Bolivia,” is just Bolivia

%r{\W*(?:\s+|$)}
EMPTY =
[].freeze
BLANK =
''.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original, options = {}) ⇒ Record

Returns a new instance of Record.



15
16
17
18
19
# File 'lib/fuzzy_match/record.rb', line 15

def initialize(original, options = {})
  @original = original
  @read = options[:read]
  @stop_words = options.fetch(:stop_words, EMPTY)
end

Instance Attribute Details

#originalObject (readonly)

Returns the value of attribute original.



11
12
13
# File 'lib/fuzzy_match/record.rb', line 11

def original
  @original
end

#readObject (readonly)

Returns the value of attribute read.



12
13
14
# File 'lib/fuzzy_match/record.rb', line 12

def read
  @read
end

#stop_wordsObject (readonly)

Returns the value of attribute stop_words.



13
14
15
# File 'lib/fuzzy_match/record.rb', line 13

def stop_words
  @stop_words
end

Instance Method Details

#cleanObject



25
26
27
28
29
30
31
32
33
# File 'lib/fuzzy_match/record.rb', line 25

def clean
  @clean ||= begin
    memo = whole.dup
    stop_words.each do |stop_word|
      memo.gsub! stop_word, BLANK
    end
    memo.strip.freeze
  end
end

#inspectObject



21
22
23
# File 'lib/fuzzy_match/record.rb', line 21

def inspect
  "w(#{clean.inspect})"
end

#similarity(other) ⇒ Object



39
40
41
# File 'lib/fuzzy_match/record.rb', line 39

def similarity(other)
  Similarity.new self, other
end

#wholeObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fuzzy_match/record.rb', line 43

def whole
  @whole ||= case read
  when ::NilClass
    original
  when ::Numeric, ::String
    original[read]
  when ::Proc
    read.call original
  when ::Symbol
    original.respond_to?(read) ? original.send(read) : original[read]
  else
    raise "Expected nil, a proc, or a symbol, got #{read.inspect}"
  end.to_s.strip.freeze
end

#wordsObject



35
36
37
# File 'lib/fuzzy_match/record.rb', line 35

def words
  @words ||= clean.downcase.split(WORD_BOUNDARY).freeze
end