Skip to content

Commit

Permalink
chore: script
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf committed Nov 30, 2023
1 parent 2068916 commit 7813cab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 33 additions & 1 deletion tools/schemagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def handle_gen_chars():
opencc_for_pinyin = None
def word_to_pinyin(word):
global opencc_for_pinyin
if args and args.opencc_for_pinyin:
if args and 'opencc_for_pinyin' in args:
if not opencc_for_pinyin:
opencc_for_pinyin = opencc.OpenCC(args.opencc_for_pinyin)
maybe_pinyin = ' '.join(lazy_pinyin(opencc_for_pinyin.convert(word)))
Expand Down Expand Up @@ -368,6 +368,33 @@ def handle_update_compact_dict():
print(f'{word}\t{newcode}')


def handle_update_char_weight():
initialize_pinyin_table()
with open(args.rime_dict) as f:
for l in f:
l = l.strip()
m = regex.match(r'^([^\t])\t([a-z][a-z];[a-z][a-z])\t(\d+)(.*)$', l)
if not m:
print(l)
else:
char = m[1]
code = m[2]
weight = int(m[3])
comment = m[4]

sp = code.split(';')[0]
wt = pinyin_table.get(char, {})
for (py, w) in wt.items():
try:
if to_double_pinyin(py) == sp:
weight = w
break
except:
weight = w

print(f'{char}\t{code}\t{weight}{comment}')


###############
### 程序入口 ###
###############
Expand Down Expand Up @@ -404,6 +431,9 @@ def handle_update_compact_dict():
update_compact_dict = subparsers.add_parser('update-compact-dict', help='更新 *compact* 詞庫中的輔助碼爲新輔助碼')
update_compact_dict.add_argument('--rime-dict', help='輸入rime格式詞庫(無frontmatter)', required=True)

update_char_weight = subparsers.add_parser('update-char-weight', help='更新 chars 詞庫中的詞頻')
update_char_weight.add_argument('--rime-dict', help='輸入rime格式詞庫', required=True)

if __name__ == '__main__':
args = parser.parse_args()
if args.command == 'gen-chars':
Expand All @@ -414,3 +444,5 @@ def handle_update_compact_dict():
handle_gen_fixed()
elif args.command == 'update-compact-dict':
handle_update_compact_dict()
elif args.command == 'update-char-weight':
handle_update_char_weight()
2 changes: 1 addition & 1 deletion tools/zrmify.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def 韻母轉換(pinyin: str) -> str:
'un': 'p',
'ang': 'h', 'eng': 'g', 'ing': 'y', 'ong': 's',
'ia': 'w', 'iao': 'c', 'ian': 'm', 'iang': 'd', 'iong': 's',
'ua': 'w', 'uo': 'o', 'uai': 'y', 'uan': 'r', 'uang': 'd'
'ua': 'w', 'uo': 'o', 'uai': 'y', 'uan': 'r', 'van': 'r', 'uang': 'd'
}
if pinyin in 映射表:
return 映射表[pinyin]
Expand Down

0 comments on commit 7813cab

Please sign in to comment.