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

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.



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

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.



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

def boundary_type
  @boundary_type
end

#category_tableObject (readonly)

Returns the value of attribute category_table.



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

def category_table
  @category_table
end

#ftableObject (readonly)

Returns the value of attribute ftable.



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

def ftable
  @ftable
end

#localeObject (readonly)

Returns the value of attribute locale.



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

def locale
  @locale
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#rtableObject (readonly)

Returns the value of attribute rtable.



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

def rtable
  @rtable
end

#status_tableObject (readonly)

Returns the value of attribute status_table.



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

def status_table
  @status_table
end

Class Method Details

.instance(boundary_type, locale) ⇒ Object



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

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



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
# File 'lib/twitter_cldr/segmentation/state_machine.rb', line 71

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)

    if ftable[row + ACCEPTING] == ACCEPTING_UNCONDITIONAL
      # 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