-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
48 lines (38 loc) · 1.14 KB
/
Copy pathquery.py
File metadata and controls
48 lines (38 loc) · 1.14 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
查询 fcitx5 词库中指定词的词频。
用法:
python query.py <词库文件> <要查询的词>
"""
import sys
def query(dict_path, word):
found = []
try:
with open(dict_path, "r", encoding="utf-8") as f:
for line in f:
s = line.strip()
if not s or s.startswith("#"):
continue
parts = s.split()
if len(parts) >= 3 and parts[0] == word:
found.append((parts[1], parts[2]))
except FileNotFoundError:
print(f"文件不存在:{dict_path}")
sys.exit(1)
if not found:
print(f"未找到:{word}")
else:
print(f"{word} 的词频:")
for pinyin, freq in found:
print(f" {pinyin}\t{freq}")
def main():
if len(sys.argv) < 3:
print("用法: python query.py <词库文件> <要查询的词>")
print("示例: python query.py frost_dict_for_fcitx5.txt 的")
sys.exit(1)
dict_path = sys.argv[1]
word = sys.argv[2]
query(dict_path, word)
if __name__ == "__main__":
main()