Class: Kotoshu::Readers::ConvTable
- Inherits:
-
Object
- Object
- Kotoshu::Readers::ConvTable
- Defined in:
- lib/kotoshu/readers/aff_data.rb
Overview
Conversion table for ICONV/OCONV.
Instance Attribute Summary collapse
-
#pairs ⇒ Array<Array<String>>
Array of [pattern, replacement] pairs.
Instance Method Summary collapse
-
#call(word) ⇒ String
Apply conversions to word.
-
#initialize(pairs) ⇒ ConvTable
constructor
Create a new conversion table.
Constructor Details
#initialize(pairs) ⇒ ConvTable
Create a new conversion table.
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
#pairs ⇒ Array<Array<String>>
Array of [pattern, replacement] 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.
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 |