Class: TwitterCldr::Segmentation::Suppressions

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/twitter_cldr/segmentation/suppressions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forward_trie, backward_trie) ⇒ Suppressions

Returns a new instance of Suppressions.



45
46
47
48
# File 'lib/twitter_cldr/segmentation/suppressions.rb', line 45

def initialize(forward_trie, backward_trie)
  @forward_trie = forward_trie
  @backward_trie = backward_trie
end

Instance Attribute Details

#backward_trieObject (readonly)

Returns the value of attribute backward_trie.



43
44
45
# File 'lib/twitter_cldr/segmentation/suppressions.rb', line 43

def backward_trie
  @backward_trie
end

#forward_trieObject (readonly)

Returns the value of attribute forward_trie.



43
44
45
# File 'lib/twitter_cldr/segmentation/suppressions.rb', line 43

def forward_trie
  @forward_trie
end

Class Method Details

.instance(boundary_type, locale) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/twitter_cldr/segmentation/suppressions.rb', line 14

def instance(boundary_type, locale)
  resource_path = find_resource(boundary_type, locale)
  return NullSuppressions.instance unless resource_path

  cache[resource_path] ||= begin
    rsrc = TwitterCldr.get_resource(resource_path)

    new(
      Marshal.load(rsrc[:forwards_trie]),
      Marshal.load(rsrc[:backwards_trie])
    )
  end
end

Instance Method Details

#should_break?(cursor) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/twitter_cldr/segmentation/suppressions.rb', line 50

def should_break?(cursor)
  idx = cursor.position

  # consider case when a space follows the '.' (so we handle i.e. "Mr. Brown")
  idx -= 2 if cursor.codepoint(idx - 1) == 32
  node = backward_trie.root

  found = loop do
    break false if idx < 0 || idx >= cursor.length
    node = node.child(cursor.codepoint(idx))
    break false unless node
    break true if node.value
    idx -= 1
  end

  return true unless found

  node = forward_trie.root

  loop do
    return true if idx >= cursor.length
    node = node.child(cursor.codepoint(idx))
    return true unless node
    return false if node.value
    idx += 1
  end
end