class TwitterCldr::Segmentation::StateMachine

Constants

ACCEPTING
ACCEPTING_UNCONDITIONAL
NEXT_STATES
START_STATE
STOP_STATE

Attributes

boundary_type[R]
category_table[R]
ftable[R]
locale[R]
metadata[R]
rtable[R]
status_table[R]

Public Class Methods

instance(boundary_type, locale) click to toggle source
# 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
new(boundary_type, locale, metadata, ftable, rtable, status_table, category_table) click to toggle source
# File lib/twitter_cldr/segmentation/state_machine.rb, line 61
def initialize(boundary_type, locale, metadata, ftable, rtable, status_table, category_table)
  @boundary_type = boundary_type
  @locale = locale
  @metadata = metadata
  @ftable = ftable
  @rtable = rtable
  @status_table = status_table
  @category_table = category_table
end

Private Class Methods

cache() click to toggle source
# File lib/twitter_cldr/segmentation/state_machine.rb, line 53
def cache
  @cache ||= {}
end
find_resource(boundary_type, locale) click to toggle source
# File lib/twitter_cldr/segmentation/state_machine.rb, line 41
def find_resource(boundary_type, locale)
  path = TwitterCldr.resource_file_path(
    ['shared', 'segments', 'rules', locale, boundary_type]
  )

  return path if TwitterCldr.resource_exists?(path)

  TwitterCldr.resource_file_path(
    ['shared', 'segments', 'rules', 'root', boundary_type]
  )
end

Public Instance Methods

handle_next(cursor) click to toggle source
# File lib/twitter_cldr/segmentation/state_machine.rb, line 71
def handle_next(cursor)
  result = initial_position = cursor.position
  state = START_STATE
  row = state * (metadata.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 * (metadata.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