Class: Sawarineko::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/sawarineko/converter.rb

Overview

Convert plain text to Sawarineko text.

Instance Method Summary collapse

Constructor Details

#initialize(encoding = Encoding::UTF_8) ⇒ Converter

Initialize a Converter. Get the encoding of source. Initialize Regexps for conversion.

encoding - The Encoding of source (default: Encoding::UTF_8).



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sawarineko/converter.rb', line 15

def initialize(encoding = Encoding::UTF_8)
  @encoding = Encoding.find(encoding)
  @hiragana_regex = Regexp.new(''.encode(@encoding)).freeze
  @katakana_regex = Regexp.new(''.encode(@encoding)).freeze
  @hangul_regex = case @encoding
                  when Encoding::UTF_8, Encoding::UTF_16BE,
                       Encoding::UTF_16LE, Encoding::EUC_KR
                    Regexp.new('[나-낳]'.encode(@encoding)).freeze
                  when Encoding::CP949
                    Regexp.new('[나-낳낛-낤낥-낲]'.encode(@encoding)).freeze
                  end
end

Instance Method Details

#convert(source) ⇒ Object

Convert the source.

source - The String source to convert.

Returns the String converted to Sawarineko.



33
34
35
36
37
38
39
40
41
# File 'lib/sawarineko/converter.rb', line 33

def convert(source)
  new_source = source.gsub(@hiragana_regex, 'にゃ'.encode(@encoding).freeze)
                     .gsub(@katakana_regex, 'ニャ'.encode(@encoding).freeze)
  if @hangul_regex
    new_source.gsub(@hangul_regex) { |ch| convert_hangul(ch) }
  else
    new_source
  end
end