Class: FuzzyMatch::Record
- Inherits:
-
Object
- Object
- FuzzyMatch::Record
- 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
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#read ⇒ Object
readonly
Returns the value of attribute read.
-
#stop_words ⇒ Object
readonly
Returns the value of attribute stop_words.
Instance Method Summary collapse
- #clean ⇒ Object
-
#initialize(original, options = {}) ⇒ Record
constructor
A new instance of Record.
- #inspect ⇒ Object
- #similarity(other) ⇒ Object
- #whole ⇒ Object
- #words ⇒ Object
Constructor Details
Instance Attribute Details
#original ⇒ Object (readonly)
Returns the value of attribute original.
11 12 13 |
# File 'lib/fuzzy_match/record.rb', line 11 def original @original end |
#read ⇒ Object (readonly)
Returns the value of attribute read.
12 13 14 |
# File 'lib/fuzzy_match/record.rb', line 12 def read @read end |
#stop_words ⇒ Object (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
#clean ⇒ Object
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 |
#inspect ⇒ Object
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 |
#whole ⇒ Object
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 |
#words ⇒ Object
35 36 37 |
# File 'lib/fuzzy_match/record.rb', line 35 def words @words ||= clean.downcase.split(WORD_BOUNDARY).freeze end |