Module: ChineseInitial::Core

Defined in:
lib/chinese_initial/core.rb

Class Method Summary collapse

Class Method Details

.chinese?(chines_code_point) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/chinese_initial/core.rb', line 21

def self.chinese?(chines_code_point)
  (CHINESE_CODE_START..CHINESE_CODE_END) === chines_code_point
end

.get_initial(word) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/chinese_initial/core.rb', line 5

def self.get_initial(word)
  return if word.nil? || word.to_s.strip.empty?
  chinese = word[0]
  codepoint = chinese.codepoints[0]
  chinese_initial = (chinese?(codepoint) ? CODE_TABLE[codepoint - CHINESE_CODE_START] : chinese)
  return chinese_initial.upcase
end

.to_pinyin(word) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/chinese_initial/core.rb', line 13

def self.to_pinyin(word)
  pinyin = ""
  word.each_codepoint do |codepoint|
    pinyin << (chinese?(codepoint) ? CODE_TABLE[codepoint - CHINESE_CODE_START] : chinese)
  end
  return pinyin.downcase
end