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 =
3
ACCEPTING =
0
ACCEPTING_UNCONDITIONAL =
1
LOOKAHEAD =
1

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.



62
63
64
65
66
67
68
69
70
71
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 62

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
  @lookahead_matches = Array.new(.lookahead_results_size, 0)
end

Instance Attribute Details

#boundary_typeObject (readonly)

Returns the value of attribute boundary_type.



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

def boundary_type
  @boundary_type
end

#category_tableObject (readonly)

Returns the value of attribute category_table.



60
61
62
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 60

def category_table
  @category_table
end

#ftableObject (readonly)

Returns the value of attribute ftable.



60
61
62
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 60

def ftable
  @ftable
end

#localeObject (readonly)

Returns the value of attribute locale.



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

def locale
  @locale
end

#metadataObject (readonly)

Returns the value of attribute metadata.



60
61
62
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 60

def 
  @metadata
end

#rtableObject (readonly)

Returns the value of attribute rtable.



60
61
62
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 60

def rtable
  @rtable
end

#status_tableObject (readonly)

Returns the value of attribute status_table.



60
61
62
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 60

def status_table
  @status_table
end

Class Method Details

.instance(boundary_type, locale) ⇒ Object



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

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



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
117
118
119
120
121
122
123
124
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 73

def handle_next(cursor)
  result = initial_position = cursor.position
  state = START_STATE
  row = state * (.category_count + NEXT_STATES)
  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)
      cursor.advance
    else
      mode = :run
    end

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

    if accepting == ACCEPTING_UNCONDITIONAL
      # match found
      result = cursor.position
    elsif accepting > ACCEPTING_UNCONDITIONAL
      if (lookahead_result = @lookahead_matches[accepting]) >= 0
        cursor.position = lookahead_result
        return lookahead_result
      end
    end

    if (rule = ftable[row + LOOKAHEAD]) != 0
      @lookahead_matches[rule] = cursor.position
    end
  end

  cursor.position = result

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

  result
end