Class: Kotoshu::Readers::ConvTable

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

Overview

Conversion table for ICONV/OCONV.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pairs) ⇒ ConvTable

Create a new conversion table.

Parameters:

  • pairs (Array<Array<String>>)

    Array of [pattern, replacement] pairs



153
154
155
156
# File 'lib/kotoshu/readers/aff_data.rb', line 153

def initialize(pairs)
  @pairs = pairs
  @table = pairs.map { |pat1, pat2| compile_row(pat1, pat2) }.sort_by { |search, _| search.length }
end

Instance Attribute Details

#pairsArray<Array<String>>

Array of [pattern, replacement] pairs

Returns:

  • (Array<Array<String>>)

    the current value of pairs



147
148
149
# File 'lib/kotoshu/readers/aff_data.rb', line 147

def pairs
  @pairs
end

Instance Method Details

#call(word) ⇒ String

Apply conversions to word.

Parameters:

  • word (String)

    Input word

Returns:

  • (String)

    Converted word



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/kotoshu/readers/aff_data.rb', line 162

def call(word)
  pos = 0
  result = ''

  while pos < word.length
    matches = @table.select { |_, pattern| pattern.match?(word, pos) }
                       .sort_by { |search, _| search.length }
                       .reverse

    if matches.any?
      search, _, replacement = matches.first
      result += replacement
      pos += search.length
    else
      result += word[pos]
      pos += 1
    end
  end

  result
end