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

Inherits:
Object
  • Object
show all
Extended by:
Normalizer, T::Sig
Defined in:
app/models/atlas_engine/address_validation/token/sequence.rb,
app/models/atlas_engine/address_validation/token/sequence/comparator.rb,
app/models/atlas_engine/address_validation/token/sequence/comparison.rb,
app/models/atlas_engine/address_validation/token/sequence/comparison_policy.rb

Defined Under Namespace

Classes: Comparator, Comparison, ComparisonPolicy

Constant Summary collapse

ACCEPTABLE_CHARACTERS =
/\p{Alnum}/
TokenOrSynonyms =
T.type_alias { T.any(Token, Synonyms) }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalizer

normalize

Constructor Details

#initialize(tokens: [], raw_value: nil) ⇒ Sequence

Returns a new instance of Sequence.



73
74
75
76
77
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 73

def initialize(tokens: [], raw_value: nil)
  @raw_value = raw_value
  @tokens = group_by_overlapping_offsets(tokens)
    .map { |tkns| tkns.one? ? T.must(tkns.first) : Synonyms.new(tokens: tkns) }
end

Instance Attribute Details

#raw_valueObject (readonly)

Returns the value of attribute raw_value.



58
59
60
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 58

def raw_value
  @raw_value
end

#tokensObject (readonly)

Returns the value of attribute tokens.



55
56
57
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 55

def tokens
  @tokens
end

Class Method Details

.from_string(string) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 16

def from_string(string)
  start_offset = 0
  end_offset = 0
  position = 0

  tokens = Annex29.segment_words(string).filter_map do |substring|
    start_offset = end_offset
    end_offset = start_offset + substring.length

    normalized_substring = normalize(substring)
    # annex 29 returns whitespace and punctuation as separate substrings
    next unless normalized_substring.match?(ACCEPTABLE_CHARACTERS)

    token = Token.new(
      value: normalized_substring,
      start_offset: start_offset,
      end_offset: end_offset,
      position: position,
      type: number?(substring) ? "<NUM>" : "<ALPHANUM>",
    )

    position += 1

    token
  end

  new(tokens: tokens, raw_value: string)
end

.number?(string) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 45

def number?(string)
  !Float(string).nil?
rescue
  false
end

Instance Method Details

#==(other) ⇒ Object



87
88
89
90
91
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 87

def ==(other)
  return false unless other.is_a?(Sequence)

  tokens == other.tokens
end

#empty?Boolean

Returns:

  • (Boolean)


63
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 63

def empty? = tokens.empty?

#inspectObject



80
81
82
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 80

def inspect
  "<seq #{tokens.inspect}/>"
end

#lengthObject



69
70
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 69

def length = tokens.length
# rubocop:enable Rails/Delegate

#permutationsObject



85
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 85

def permutations = recursive_permutations(tokens)

#sizeObject



66
# File 'app/models/atlas_engine/address_validation/token/sequence.rb', line 66

def size = tokens.size