Class: AtlasEngine::AddressValidation::Token::Sequence::Comparison

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_sequenceObject (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_sequenceObject (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_comparisonsObject (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_tokensObject (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_distanceObject



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

Returns:

  • (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

Returns:

  • (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

#inspectObject



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

Returns:

  • (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

Returns:

  • (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_countObject



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

Returns:

  • (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