Class: Kotoshu::Readers::PhonetTable
- Inherits:
-
Object
- Object
- Kotoshu::Readers::PhonetTable
- 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
-
#table ⇒ Array<Array<String>>
Array of [pattern, replacement] pairs.
Instance Method Summary collapse
-
#initialize(table) ⇒ PhonetTable
constructor
Create a new phonetic table.
-
#parse_rule(search, replacement) ⇒ Rule
Parse a phonetic rule.
Constructor Details
#initialize(table) ⇒ PhonetTable
Create a new phonetic table.
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
#table ⇒ Array<Array<String>>
Array of [pattern, replacement] pairs
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.
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 |