Class: Yosina::Transliterators::ProlongedSoundMarks::Transliterator
- Inherits:
-
BaseTransliterator
- Object
- BaseTransliterator
- Yosina::Transliterators::ProlongedSoundMarks::Transliterator
- Includes:
- CharType
- Defined in:
- lib/yosina/transliterators/prolonged_sound_marks.rb
Overview
Transliterator for prolonged sound marks
Constant Summary
Constants included from CharType
Instance Attribute Summary collapse
-
#allow_prolonged_hatsuon ⇒ Object
readonly
Returns the value of attribute allow_prolonged_hatsuon.
-
#allow_prolonged_sokuon ⇒ Object
readonly
Returns the value of attribute allow_prolonged_sokuon.
-
#replace_prolonged_marks_following_alnums ⇒ Object
readonly
Returns the value of attribute replace_prolonged_marks_following_alnums.
-
#skip_already_transliterated_chars ⇒ Object
readonly
Returns the value of attribute skip_already_transliterated_chars.
Instance Method Summary collapse
-
#call(input_chars) ⇒ Enumerable<Char>
Convert hyphen-like characters to appropriate prolonged sound marks.
-
#initialize(options = {}) ⇒ Transliterator
constructor
Initialize the transliterator with options.
Methods included from CharType
#alphanumeric?, #fullwidth?, #fullwidth_alphanumeric?, #halfwidth?, #halfwidth_alphanumeric?, #hatsuon?, #hiragana?, #hyphen_like?, #katakana?, #prolongable?, #prolonged_sound_mark?, #sokuon?
Constructor Details
#initialize(options = {}) ⇒ Transliterator
Initialize the transliterator with options
99 100 101 102 103 104 105 |
# File 'lib/yosina/transliterators/prolonged_sound_marks.rb', line 99 def initialize( = {}) super() @skip_already_transliterated_chars = .fetch(:skip_already_transliterated_chars, false) @allow_prolonged_hatsuon = .fetch(:allow_prolonged_hatsuon, false) @allow_prolonged_sokuon = .fetch(:allow_prolonged_sokuon, false) @replace_prolonged_marks_following_alnums = .fetch(:replace_prolonged_marks_following_alnums, false) end |
Instance Attribute Details
#allow_prolonged_hatsuon ⇒ Object (readonly)
Returns the value of attribute allow_prolonged_hatsuon.
85 86 87 |
# File 'lib/yosina/transliterators/prolonged_sound_marks.rb', line 85 def allow_prolonged_hatsuon @allow_prolonged_hatsuon end |
#allow_prolonged_sokuon ⇒ Object (readonly)
Returns the value of attribute allow_prolonged_sokuon.
85 86 87 |
# File 'lib/yosina/transliterators/prolonged_sound_marks.rb', line 85 def allow_prolonged_sokuon @allow_prolonged_sokuon end |
#replace_prolonged_marks_following_alnums ⇒ Object (readonly)
Returns the value of attribute replace_prolonged_marks_following_alnums.
85 86 87 |
# File 'lib/yosina/transliterators/prolonged_sound_marks.rb', line 85 def replace_prolonged_marks_following_alnums @replace_prolonged_marks_following_alnums end |
#skip_already_transliterated_chars ⇒ Object (readonly)
Returns the value of attribute skip_already_transliterated_chars.
85 86 87 |
# File 'lib/yosina/transliterators/prolonged_sound_marks.rb', line 85 def skip_already_transliterated_chars @skip_already_transliterated_chars end |
Instance Method Details
#call(input_chars) ⇒ Enumerable<Char>
Convert hyphen-like characters to appropriate prolonged sound marks
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/yosina/transliterators/prolonged_sound_marks.rb', line 111 def call(input_chars) offset = 0 processed_char_in_lookahead = false lookahead_buf = [] last_non_prolonged_char = nil Chars.enum do |y| input_chars.each do |char| unless lookahead_buf.empty? if !char.c.empty? && hyphen_like?(char.c.ord) processed_char_in_lookahead = true unless char.source.nil? lookahead_buf << char next end prev_non_prolonged_char = last_non_prolonged_char last_non_prolonged_char = char if (prev_non_prolonged_char.nil? || alphanumeric?(prev_non_prolonged_char.c.ord)) && ( !@skip_already_transliterated_chars || !processed_char_in_lookahead ) halfwidth = halfwidth?( prev_non_prolonged_char.nil? ? last_non_prolonged_char.c.ord : prev_non_prolonged_char.c.ord ) replacement = halfwidth ? "\u002d" : "\uff0d" lookahead_buf.each do |buffered_char| y << Char.new(c: replacement, offset: offset, source: buffered_char) offset += replacement.length end else lookahead_buf.each do |buffered_char| y << buffered_char.with_offset(offset) offset += buffered_char.c.length end end lookahead_buf.clear y << char.with_offset(offset) offset += char.c.length last_non_prolonged_char = char processed_char_in_lookahead = false next end if !char.c.empty? && hyphen_like?(char.c.ord) should_procses = !@skip_already_transliterated_chars || !char.transliterated? if should_procses && !last_non_prolonged_char.nil? if prolongable_char?(last_non_prolonged_char.c.ord) replacement = halfwidth?(last_non_prolonged_char.c.ord) ? "\uff70" : "\u30fc" y << Char.new(c: replacement, offset: offset, source: char) offset += replacement.length next elsif @replace_prolonged_marks_following_alnums && alphanumeric?(last_non_prolonged_char.c.ord) lookahead_buf << char next end end else last_non_prolonged_char = char end y << char.with_offset(offset) offset += char.c.length end end end |