Class: AtlasEngine::AddressValidation::Token::Sequence::Comparison
- Inherits:
-
Object
- Object
- AtlasEngine::AddressValidation::Token::Sequence::Comparison
- Extended by:
- T::Sig
- Includes:
- Comparable
- Defined in:
- app/models/atlas_engine/address_validation/token/sequence/comparison.rb
Constant Summary collapse
- DEFAULT_PARTIAL_MATCH_THRESHOLD_PERCENT =
0.5
Instance Attribute Summary collapse
-
#left_sequence ⇒ Object
readonly
Returns the value of attribute left_sequence.
-
#right_sequence ⇒ Object
readonly
Returns the value of attribute right_sequence.
-
#token_comparisons ⇒ Object
readonly
Returns the value of attribute token_comparisons.
-
#unmatched_tokens ⇒ Object
readonly
Returns the value of attribute unmatched_tokens.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #aggregate_distance ⇒ Object
- #better_than?(other_comparison) ⇒ Boolean
- #equivalent_to?(other_comparison) ⇒ Boolean
-
#initialize(unmatched_tokens:, token_comparisons:, left_sequence:, right_sequence:) ⇒ Comparison
constructor
A new instance of Comparison.
- #inspect ⇒ Object
- #match? ⇒ Boolean
- #merge(other_comparison) ⇒ Object
- #potential_match?(threshold_percent: DEFAULT_PARTIAL_MATCH_THRESHOLD_PERCENT) ⇒ Boolean
- #token_match_count ⇒ Object
- #worse_than?(other_comparison) ⇒ Boolean
Constructor Details
#initialize(unmatched_tokens:, token_comparisons:, left_sequence:, right_sequence:) ⇒ Comparison
Returns a new instance of Comparison.
24 25 26 27 28 29 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 24 def initialize(unmatched_tokens:, token_comparisons:, left_sequence:, right_sequence:) @unmatched_tokens = unmatched_tokens @token_comparisons = token_comparisons @left_sequence = left_sequence @right_sequence = right_sequence end |
Instance Attribute Details
#left_sequence ⇒ Object (readonly)
Returns the value of attribute left_sequence.
14 15 16 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 14 def left_sequence @left_sequence end |
#right_sequence ⇒ Object (readonly)
Returns the value of attribute right_sequence.
14 15 16 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 14 def right_sequence @right_sequence end |
#token_comparisons ⇒ Object (readonly)
Returns the value of attribute token_comparisons.
14 15 16 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 14 def token_comparisons @token_comparisons end |
#unmatched_tokens ⇒ Object (readonly)
Returns the value of attribute unmatched_tokens.
14 15 16 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 14 def unmatched_tokens @unmatched_tokens end |
Instance Method Details
#<=>(other) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 32 def <=>(other) # > num matches # longest subsequence # < num unmatched (kinda related to < aggregate edit distance) # < aggregate edit distance # > num prefixes # > num suffixes matches = count_by_qualifier(:equal) <=> other.count_by_qualifier(:equal) return matches * -1 if matches.nonzero? unmatched = unmatched_tokens.size <=> other.unmatched_tokens.size return unmatched if unmatched.nonzero? longest_subsequence = longest_subsequence_comparison <=> other.longest_subsequence_comparison return -1 * longest_subsequence if longest_subsequence.nonzero? edit_distance = aggregate_distance <=> other.aggregate_distance return edit_distance if edit_distance.nonzero? prefixes = count_by_qualifier(:prefix) <=> other.count_by_qualifier(:prefix) return prefixes * -1 if prefixes.nonzero? (count_by_qualifier(:suffix) <=> other.count_by_qualifier(:suffix)) * -1 end |
#aggregate_distance ⇒ Object
104 105 106 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 104 def aggregate_distance token_comparisons.sum(&:edit_distance) + unmatched_tokens.map(&:value).sum(&:length) end |
#better_than?(other_comparison) ⇒ Boolean
69 70 71 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 69 def better_than?(other_comparison) self < other_comparison end |
#equivalent_to?(other_comparison) ⇒ Boolean
79 80 81 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 79 def equivalent_to?(other_comparison) self == other_comparison end |
#inspect ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 58 def inspect parts = ["["] token_comparisons.each do |comparison| parts << "\n#{comparison.inspect}" end parts << "\n" unless token_comparisons.empty? parts << "]" "<seqcomp unmatched:#{unmatched_tokens.inspect} comp:#{parts.join}/>" end |
#match? ⇒ Boolean
94 95 96 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 94 def match? aggregate_distance == 0 && unmatched_tokens.empty? end |
#merge(other_comparison) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 84 def merge(other_comparison) AddressValidation::Token::Sequence::Comparison.new( unmatched_tokens: unmatched_tokens + other_comparison.unmatched_tokens, token_comparisons: (token_comparisons + other_comparison.token_comparisons).uniq, left_sequence: left_sequence.equal?(other_comparison.left_sequence) ? left_sequence : nil, right_sequence: right_sequence.equal?(other_comparison.right_sequence) ? right_sequence : nil, ) end |
#potential_match?(threshold_percent: DEFAULT_PARTIAL_MATCH_THRESHOLD_PERCENT) ⇒ Boolean
99 100 101 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 99 def potential_match?(threshold_percent: DEFAULT_PARTIAL_MATCH_THRESHOLD_PERCENT) matched_tokens_percent >= threshold_percent && matched_length_percent >= threshold_percent end |
#token_match_count ⇒ Object
109 110 111 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 109 def token_match_count token_comparisons.size end |
#worse_than?(other_comparison) ⇒ Boolean
74 75 76 |
# File 'app/models/atlas_engine/address_validation/token/sequence/comparison.rb', line 74 def worse_than?(other_comparison) self > other_comparison end |