Class: TwitterCldr::Segmentation::StateMachine

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

Constant Summary collapse

START_STATE =
1
STOP_STATE =
0
NEXT_STATES =
4
ACCEPTING =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boundary_type, locale, metadata, ftable, rtable, status_table, category_table) ⇒ StateMachine

Returns a new instance of StateMachine.



60
61
62
63
64
65
66
67
68
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 60

def initialize(boundary_type, locale, , ftable, rtable, status_table, category_table)
  @boundary_type = boundary_type
  @locale = locale
  @metadata = 
  @ftable = ftable
  @rtable = rtable
  @status_table = status_table
  @category_table = category_table
end

Instance Attribute Details

#boundary_typeObject (readonly)

Returns the value of attribute boundary_type.



57
58
59
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 57

def boundary_type
  @boundary_type
end

#category_tableObject (readonly)

Returns the value of attribute category_table.



58
59
60
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 58

def category_table
  @category_table
end

#ftableObject (readonly)

Returns the value of attribute ftable.



58
59
60
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 58

def ftable
  @ftable
end

#localeObject (readonly)

Returns the value of attribute locale.



57
58
59
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 57

def locale
  @locale
end

#metadataObject (readonly)

Returns the value of attribute metadata.



58
59
60
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 58

def 
  @metadata
end

#rtableObject (readonly)

Returns the value of attribute rtable.



58
59
60
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 58

def rtable
  @rtable
end

#status_tableObject (readonly)

Returns the value of attribute status_table.



58
59
60
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 58

def status_table
  @status_table
end

Class Method Details

.instance(boundary_type, locale) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 20

def instance(boundary_type, locale)
  resource_path = find_resource(boundary_type, locale)

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

    new(
      boundary_type,
      locale,
      Metadata.new(rsrc[:metadata]),
      StateTable.load16(rsrc[:forward_table]),
      StateTable.load16(rsrc[:backward_table]),
      StatusTable.load(rsrc[:status_table]),
      CategoryTable.load16(rsrc[:category_table])
    )
  end
end

Instance Method Details

#handle_next(cursor) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 70

def handle_next(cursor)
  result = initial_position = cursor.position
  state = START_STATE
  row = state * (.category_count + 4)
  category = 3
  mode = :run

  if ftable.bof_required?
    category = 2
    mode = :start
  end

  until state == STOP_STATE
    if cursor.eos?
      break if mode == :stop
      mode = :stop
      category = 1
    elsif mode == :run
      category = category_table.get(cursor.codepoint)

      if (category & 0x4000) != 0
        category &= ~0x4000
      end

      cursor.advance
    else
      mode = :run
    end

    state = ftable[row + NEXT_STATES + category]
    row = state * (.category_count + 4)

    if ftable[row + ACCEPTING] == -1
      # match found
      result = cursor.position
    end
  end

  cursor.position = result

  # don't let cursor get stuck
  if cursor.position == initial_position
    cursor.advance
  end

  result
end