Class: CBETA::Gaiji

Inherits:
Object
  • Object
show all
Defined in:
lib/cbeta/gaiji.rb

Overview

存取 CBETA 缺字資料庫

Instance Method Summary collapse

Constructor Details

#initializeGaiji

載入 CBETA 缺字資料庫



6
7
8
9
# File 'lib/cbeta/gaiji.rb', line 6

def initialize()
  fn = File.join(File.dirname(__FILE__), '../data/gaiji.json')
  @gaijis = JSON.parse(File.read(fn))
end

Instance Method Details

#[](cb) ⇒ Hash{String => Strin, Array<String>}?

取得缺字資訊

Return:

{
  "zzs": "[得-彳]",
  "unicode": "3775",
  "unicode-char": "㝵",
  "zhuyin": [ "ㄉㄜˊ", "ㄞˋ" ]
}

Examples:

g = Cbeta::Gaiji.new
g["CB01002"]

Parameters:

  • cb (String)

    缺字 CB 碼

Returns:

  • (Hash{String => Strin, Array<String>})

    缺字資訊

  • (nil)

    如果該 CB 碼在 CBETA 缺字庫中不存在



28
29
30
# File 'lib/cbeta/gaiji.rb', line 28

def [](cb)
  @gaijis[cb]
end

#update_from_p5(p5_folder, output_json_filename) ⇒ Object

讀 XML P5 檔頭的缺字資料,更新現有缺字資料,輸出 JSON



48
49
50
51
52
# File 'lib/cbeta/gaiji.rb', line 48

def update_from_p5(p5_folder, output_json_filename)
  update_from_p5_folder(p5_folder)
  s = JSON.pretty_generate(@gaijis)
  File.write(output_json_filename, s)
end

#zhuyin(cb) ⇒ Array<String>

傳入缺字 CB 碼,傳回注音 array

資料來源:CBETA 於 2015.5.15 提供的 MS Access 缺字資料庫

Examples:

g = CBETA::Gaiji.new
g.zhuyin("CB00023") # return [ "ㄍㄢˇ", "ㄍㄢ", "ㄧㄤˊ", "ㄇㄧˇ", "ㄇㄧㄝ", "ㄒㄧㄤˊ" ]

Parameters:

  • cb (String)

    缺字 CB 碼

Returns:

  • (Array<String>)


42
43
44
45
# File 'lib/cbeta/gaiji.rb', line 42

def zhuyin(cb)
  return nil unless @gaijis.key? cb
  @gaijis[cb]['zhuyin']
end