Class: FuzzyMatch::Wrapper
- Inherits:
-
Object
- Object
- FuzzyMatch::Wrapper
- Defined in:
- lib/fuzzy_match/wrapper.rb
Overview
Wrappers 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+|$)}
Instance Attribute Summary collapse
-
#fuzzy_match ⇒ Object
readonly
Returns the value of attribute fuzzy_match.
-
#literal ⇒ Object
readonly
Returns the value of attribute literal.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#rendered ⇒ Object
readonly
Returns the value of attribute rendered.
Instance Method Summary collapse
-
#initialize(fuzzy_match, record, literal = false) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #inspect ⇒ Object
- #read ⇒ Object
- #render ⇒ Object (also: #to_str)
- #similarity(other) ⇒ Object
- #variants ⇒ Object
- #words ⇒ Object
Constructor Details
#initialize(fuzzy_match, record, literal = false) ⇒ Wrapper
Returns a new instance of Wrapper.
14 15 16 17 18 |
# File 'lib/fuzzy_match/wrapper.rb', line 14 def initialize(fuzzy_match, record, literal = false) @fuzzy_match = fuzzy_match @record = record @literal = literal end |
Instance Attribute Details
#fuzzy_match ⇒ Object (readonly)
Returns the value of attribute fuzzy_match.
9 10 11 |
# File 'lib/fuzzy_match/wrapper.rb', line 9 def fuzzy_match @fuzzy_match end |
#literal ⇒ Object (readonly)
Returns the value of attribute literal.
11 12 13 |
# File 'lib/fuzzy_match/wrapper.rb', line 11 def literal @literal end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
10 11 12 |
# File 'lib/fuzzy_match/wrapper.rb', line 10 def record @record end |
#rendered ⇒ Object (readonly)
Returns the value of attribute rendered.
12 13 14 |
# File 'lib/fuzzy_match/wrapper.rb', line 12 def rendered @rendered end |
Instance Method Details
#inspect ⇒ Object
20 21 22 |
# File 'lib/fuzzy_match/wrapper.rb', line 20 def inspect "#<FuzzyMatch::Wrapper render=#{render.inspect} variants=#{variants.length}>" end |
#read ⇒ Object
24 25 26 |
# File 'lib/fuzzy_match/wrapper.rb', line 24 def read fuzzy_match.read unless literal end |
#render ⇒ Object Also known as: to_str
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fuzzy_match/wrapper.rb', line 28 def render @render ||= begin memo = case read when ::Proc read.call record when ::Symbol if record.respond_to?(read) record.send read else record[read] end when ::NilClass record else record[read] end.to_s.dup fuzzy_match.stop_words.each do |stop_word| stop_word.apply! memo end memo.strip! @render = memo.freeze end end |
#similarity(other) ⇒ Object
58 59 60 |
# File 'lib/fuzzy_match/wrapper.rb', line 58 def similarity(other) Similarity.new self, other end |
#variants ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fuzzy_match/wrapper.rb', line 62 def variants @variants ||= begin fuzzy_match.normalizers.inject([ render ]) do |memo, normalizer| if normalizer.apply? render memo << normalizer.apply(render) end memo end.uniq end end |
#words ⇒ Object
54 55 56 |
# File 'lib/fuzzy_match/wrapper.rb', line 54 def words @words ||= render.downcase.split(WORD_BOUNDARY) end |