-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChineseDictionary.py
More file actions
95 lines (75 loc) · 4.09 KB
/
ChineseDictionary.py
File metadata and controls
95 lines (75 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import argparse
import json
import os
class ChineseDictionary:
def __init__(self, lite: bool = False):
"""
初始化中文字典,預設使用完整版資料庫。
:param lite: 如果為 True,則使用精簡版 Dictionary_lite.json
"""
filename = "Dictionary_lite.json" if lite else "Dictionary.json"
script_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(script_dir, filename)
if not os.path.exists(data_path):
raise FileNotFoundError(f"找不到資料檔案: {data_path}")
with open(data_path, "r", encoding="utf-8") as f:
self.data = json.load(f)
def unicode(self, char: str) -> int:
"""取得中文字的 Unicode(十進制)"""
return self.data.get(char, {}).get("unicode", None)
def unicode_hex(self, char: str) -> str:
"""取得中文字的 Unicode(十六進制)"""
return self.data.get(char, {}).get("unicode_hex", None)
def radical(self, char: str) -> str:
"""取得中文字的部首"""
return self.data.get(char, {}).get("radical", None)
def radical_count(self, char: str) -> int:
"""取得部首的筆畫數"""
return self.data.get(char, {}).get("radical_count", None)
def strokes_count(self, char: str) -> int:
"""取得中文字的非部首筆畫數"""
return self.data.get(char, {}).get("strokes_count", None)
def strokes_total(self, char: str) -> int:
"""取得中文字的總筆畫數"""
return self.data.get(char, {}).get("strokes_total", None)
def alternate(self, char: str) -> list:
"""取得中文字的異體字"""
return self.data.get(char, {}).get("alternate", [])
def semantic_variant(self, char: str) -> list:
"""取得中文字的語義變體"""
return self.data.get(char, {}).get("semantic_variant", [])
def pronunciation_mandarin(self, char: str) -> list:
"""取得中文字的普通話拼音(含漢語拼音 & 注音)"""
return self.data.get(char, {}).get("pronunciation_mandarin", [])
def pronunciation_cantonese(self, char: str) -> list:
"""取得中文字的粵語拼音"""
return self.data.get(char, {}).get("pronunciation_cantonese", [])
def pronunciation_southern_min(self, char: str) -> list:
"""取得中文字的閩南語拼音"""
return self.data.get(char, {}).get("pronunciation_southern_min", [])
def component(self, char: str) -> dict:
"""取得中文字的左右部件"""
return self.data.get(char, {}).get("component", {})
def get_character_info(keyword):
dictionary = ChineseDictionary() # 不帶入參數到物件, 預設使用完整版
if keyword in dictionary.data: # 檢查keyword是否存在於字典
char = keyword
print(f"『{char}』的 Unicode(十進制): {dictionary.unicode(char)}")
print(f"『{char}』的 Unicode(十六進制): {dictionary.unicode_hex(char)}")
print(f"『{char}』的部首: {dictionary.radical(char)}")
print(f"『{char}』的部首筆畫數: {dictionary.radical_count(char)}")
print(f"『{char}』的非部首筆畫數: {dictionary.strokes_count(char)}")
print(f"『{char}』的總筆畫數: {dictionary.strokes_total(char)}")
print(f"『{char}』的異體字: {dictionary.alternate(char)}")
print(f"『{char}』的語義變體: {dictionary.semantic_variant(char)}")
print(f"『{char}』的普通話拼音: {dictionary.pronunciation_mandarin(char)}")
print(f"『{char}』的粵語拼音: {dictionary.pronunciation_cantonese(char)}")
print(f"『{char}』的閩南語拼音: {dictionary.pronunciation_southern_min(char)}")
print(f"『{char}』的組件: {dictionary.component(char)}")
else:
print(f"找不到字元:{keyword}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="查詢中文字元資訊")
parser.add_argument("--keyword", required=True, help="要查詢的字元")
args = parser.parse_args()
get_character_info(args.keyword)