Class: Kotoshu::Readers::PhonetTable

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/readers/aff_data.rb

Overview

Phonetic table for PHONE directive.

Defined Under Namespace

Classes: Rule

Constant Summary collapse

RULE_PATTERN =

Pattern for matching phonetic rules. Updated to support extended ASCII (Latin-1) characters like É, À, etc.

/^(?<letters>[[:alpha:]]+)(\((?<optional>[[:alpha:]]+)\))?(?<lookahead>[-]+)?(?<flags>[\^$<]*)(?<priority>\d)?$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ PhonetTable

Create a new phonetic table.

Parameters:

  • table (Array<Array<String>>)

    Array of [pattern, replacement] pairs



317
318
319
320
321
322
323
324
# File 'lib/kotoshu/readers/aff_data.rb', line 317

def initialize(table)
  @table = table
  @rules = Hash.new { |h, k| h[k] = [] }

  table.each do |search, replacement|
    @rules[search[0]] << parse_rule(search, replacement)
  end
end

Instance Attribute Details

#tableArray<Array<String>>

Array of [pattern, replacement] pairs

Returns:

  • (Array<Array<String>>)

    the current value of table



286
287
288
# File 'lib/kotoshu/readers/aff_data.rb', line 286

def table
  @table
end

Instance Method Details

#parse_rule(search, replacement) ⇒ Rule

Parse a phonetic rule.

Parameters:

  • search (String)

    Search pattern

  • replacement (String)

    Replacement string

Returns:

  • (Rule)

    Parsed rule

Raises:

  • (ArgumentError)


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/kotoshu/readers/aff_data.rb', line 331

def parse_rule(search, replacement)
  match = RULE_PATTERN.match(search)
  raise ArgumentError, "Not a proper rule: #{search.inspect}" unless match

  text = match['letters'].chars
  text << "[#{match['optional']}]" if match['optional']

  if match['lookahead']
    lookahead_len = match['lookahead'].length
    regex = text[0...-lookahead_len].join + "(?=#{text[-lookahead_len..].join})"
  else
    regex = text.join
  end

  Rule.new(
    search: Regexp.new(regex),
    replacement:,
    start: match['flags']&.include?('^'),
    end: match['flags']&.include?('$'),
    priority: match['priority']&.to_i || 5,
    followup: !match['lookahead'].nil?
  )
end