class Gimchi

Attributes

chosungs[R]
jongsungs[R]
jungsungs[R]

Public Class Methods

Char(ch) click to toggle source
# File lib/gimchi.rb, line 14
def Char ch
  kchar ch
end
chosung?(ch) click to toggle source

@param [String] ch @return [Boolean]

# File lib/gimchi.rb, line 46
def chosung? ch
  @chosung_set.include? ch
end
complete_korean_char?(ch) click to toggle source

Checks if the given character is a “complete” korean character. “Complete” Korean character must have chosung and jungsung, with optional jongsung. @param [String] ch A string of size 1

# File lib/gimchi.rb, line 74
def complete_korean_char? ch
  raise ArgumentError.new('Lengthy input') if str_length(ch) > 1

  # Range of Korean chracters in Unicode 2.0: AC00(가) ~ D7A3(힣)
  ch.unpack('U').all? { | c | c >= 0xAC00 && c <= 0xD7A3 }
end
compose(chosung, jungsung = nil, jongsung = nil) click to toggle source

Compose 3 elements into a Korean character String @param [String] chosung @param [String] jungsung @param [String] jongsung @return [String]

# File lib/gimchi.rb, line 30
def compose chosung, jungsung = nil, jongsung = nil
  if chosung.nil? && jungsung.nil?
    ""
  elsif chosung && jungsung
    n1, n2, n3 =
    n1 = chosungs.index(chosung) || 0
    n2 = jungsungs.index(jungsung) || 0
    n3 = ([nil] + jongsungs).index(jongsung) || 0
    [ 0xAC00 + n1 * (21 * 28) + n2 * 28 + n3 ].pack('U')
  else
    chosung || jungsung
  end
end
decompose(ch) click to toggle source

Decompose a Korean character into 3 components @param [String] ch Korean character @return [Array]

# File lib/gimchi.rb, line 21
def decompose ch
  kchar(ch).to_a
end
jongsung?(ch) click to toggle source

@param [String] ch @return [Boolean]

# File lib/gimchi.rb, line 58
def jongsung? ch
  @jongsung_set.include? ch
end
jungsung?(ch) click to toggle source

@param [String] ch @return [Boolean]

# File lib/gimchi.rb, line 52
def jungsung? ch
  @jungsung_set.include? ch
end
kchar(ch) click to toggle source

@deprecated @private

# File lib/gimchi.rb, line 83
def kchar ch
  Gimchi::Char.new(ch)
end
kchar?(ch)
Alias for: korean_char?
korean_char?(ch) click to toggle source

Checks if the given character is a korean character. @param [String] ch A string of size 1

# File lib/gimchi.rb, line 64
def korean_char? ch
  raise ArgumentError.new('Lengthy input') if str_length(ch) > 1

  complete_korean_char?(ch) || @all.include?(ch)
end
Also aliased as: kchar?
pronounce(str, options = {}) click to toggle source

Returns the pronunciation of the given string containing Korean characters. Takes optional options hash.

@param [String] Input string @param [Hash] options Options @option options [Boolean] each_char Each character of the string is pronounced respectively. @option options [Boolean] slur Strings separated by whitespaces are processed again as if they were contiguous. @option options [Boolean] number Numberic parts of the string is also pronounced in Korean. @option options [Array] except Allows you to skip certain transformations. @return [String] Output string

# File lib/gimchi.rb, line 106
def pronounce str, options = {}
  options = {
    :each_char => false,
    :slur      => false,
    :number    => true,
    :except    => [],
    :debug     => false
  }.merge options

  str = read_number(str) if options[:number]

  result, transforms = @pronouncer.send :pronounce!, str, options

  if options[:debug]
    return result, transforms
  else
    return result
  end
end
read_number(str) click to toggle source

Reads numeric expressions in Korean way. @param [String, Number] str Numeric type or String containing numeric expressions @return [String] Output string

# File lib/gimchi.rb, line 90
def read_number str
  str.to_s.gsub(/(([+-]\s*)?[0-9,]*,*[0-9]+(\.[0-9]+(e[+-][0-9]+)?)?)(\s*.)?/) {
    read_number_sub($1, $5)
  }
end
romanize(str, options = {}) click to toggle source

Returns the romanization (alphabetical notation) of the given Korean string. en.wikipedia.org/wiki/Korean_romanization @param [String] str Input Korean string @param [Hash] options Options @option options [Boolean] as_pronounced If true, pronounce is internally called before romanize @option options [Boolean] number Whether to read numeric expressions in the string @option options [Boolean] slur Same as :slur in pronounce @return [String] Output string in Roman Alphabet @see Korean#pronounce

# File lib/gimchi.rb, line 135
def romanize str, options = {}
  options = {
    :as_pronounced => true,
    :number        => true,
    :slur          => false
  }.merge options

  rdata = @config[:romanization]
  post_subs = rdata[:post_substitution]
  rdata = [rdata[:chosung], rdata[:jungsung], rdata[:jongsung]]

  str = pronounce str,
    :each_char => !options[:as_pronounced],
    :number    => options[:number],
    :slur      => options[:slur],
    # 제1항 [붙임 1] ‘ㅢ’는 ‘ㅣ’로 소리 나더라도 ‘ui’로 적는다.
    :except => %w[rule_5_3]
  dash = rdata[0]["ㅇ"]
  romanization = ""

  romanize_chunk = lambda do |chunk|
    chunk.each_char.map { |ch| kchar(ch) rescue ch }.each do |kc|
      kc.to_a.each_with_index do |comp, idx|
        next if comp.nil?
        comp = rdata[idx][comp] || comp
        comp = comp[1..-1] if comp[0, 1] == dash &&
            (romanization.empty? || romanization[-1, 1] =~ /\s/)
        romanization += comp
      end
    end

Private Class Methods

str_length(str) click to toggle source
# File lib/gimchi/patch_1.8.rb, line 7
def str_length str
  str.scan(/./mu).length
end