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
10
11
12
13
14
15
16
17
# File 'lib/cbeta/gaiji.rb', line 6

def initialize()
  fn = File.join(File.dirname(__FILE__), '../data/gaiji.json')
  @gaijis = JSON.parse(File.read(fn))
  
  @zzs = {}
  @gaijis.each do |k,v|
    if v.key? 'zzs'
      zzs = v['zzs']
      @zzs[zzs] = k
    end
  end
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 缺字庫中不存在



36
37
38
# File 'lib/cbeta/gaiji.rb', line 36

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

#update_from_p5(p5_folder, output_json_filename) ⇒ Object

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



56
57
58
59
60
# File 'lib/cbeta/gaiji.rb', line 56

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>)


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

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

#zzs2pua(zzs) ⇒ Object

傳入 組字式,取得 PUA



63
64
65
66
67
# File 'lib/cbeta/gaiji.rb', line 63

def zzs2pua(zzs)
  return nil unless @zzs.key? zzs
  gid = @zzs[zzs]
  CBETA.pua(gid)
end