forked from anttipham/cc-cedict-for-yomichan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (124 loc) · 4.86 KB
/
main.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import argparse
import json
import re
import os
import zipfile
from dataclasses import dataclass
from io import TextIOWrapper
from pathlib import Path
from decode_pinyin import decode_pinyin
TERM_BANK_SIZE = 4000
@dataclass
class Args:
dict_file: TextIOWrapper
is_separate: bool
is_number: bool
output_directory: str
def parse_file() -> Args:
"""Parses cmd argument. Returns the dictionary file object."""
parser = argparse.ArgumentParser("main.py", description="Converts CC-CEDICT file to a Yomichan-compatible dictionary format")
parser.add_argument("dictpath",
type=argparse.FileType("r", encoding="utf-8"))
parser.add_argument("--separate",
action="store_true",
help="Use new bullet points as the seperator instead of commas")
parser.add_argument("--pinyin-numbers",
action="store_true",
help="Use tone numbers, for example 课 [ke4] instead of 课 [kè]")
parser.add_argument("--output-directory",
help="Output directory",
default=".")
args = parser.parse_args()
return Args(args.dictpath, args.separate, args.pinyin_numbers, args.output_directory)
def get_date(dict_file):
for line in dict_file:
if line.startswith("#! date="):
date = re.search(r"(\d{4})-(\d{2})-(\d{2})", line)[0]
return date
def create_index(date):
index = { "title": "CC-CEDICT",
"format": 3,
"revision": f"cc_cedict_{date}",
"sequenced": True }
return index
def termbank_creator(args: Args):
def format_pinyin(match):
pinyin: str = match.group().lower()
return pinyin.replace("u:", "ü")
def to_pinyin(match):
return decode_pinyin(match.group())
def format_CL(match):
text = match.group()
# Sometimes there's not a space after comma
# First, delete space to avoid double spaces
text = text.replace(", ", ",")
# Then add the space
text = text.replace(",", ", ")
# Also add spaces to colons
text = text.replace(":", ": ")
return text
def split_CL(match):
text = format_CL(match)
text = " (" + text.removeprefix("/").removesuffix("/") + ")\n"
return text
index = 1
def create_termbank():
nonlocal index
termbank = []
# line = "課 课 [ke4] /subject/course/CL:門|门[men2]/class/lesson/CL:堂[tang2],節|节[jie2]/to levy/tax/form of divination/"
for line in args.dict_file:
if len(termbank) >= TERM_BANK_SIZE:
return termbank
pinyin = re.sub(r"\[.+?\]", format_pinyin, line.strip())
if not args.is_number:
pinyin = re.sub(r"\[.+?\]", to_pinyin, pinyin)
chars, pronunciation, meaning = re.split(r" \[|\] ", pinyin, 2)
matches = chars.split()
if matches[0] == matches[1]:
del matches[1]
# The meaning part starts and ends with slashes
meaning = meaning.removeprefix("/").removesuffix("/")
if args.is_separate:
# Format counter
meaning = re.sub(r"\/CL:.+?\/", format_CL, meaning)
meaning = meaning.replace("/", "\n")
else:
# Different word starts after the counter
meaning = re.sub(r"\/CL:.+?\/", split_CL, meaning)
meaning = meaning.replace("/", ", ")
meanings = meaning.split("\n")
entries = [[match, pronunciation, "", "", 2, meanings, index, ""] for match in matches]
termbank.extend(entries)
index += 1
return termbank
return create_termbank
def create_termbanks(args: Args):
index = 1
create_termbank = termbank_creator(args)
while True:
term_bank = create_termbank()
if not term_bank:
break
yield term_bank
index += 1
def format_obj(obj):
return json.dumps(obj, ensure_ascii=False, separators=(',', ':'))
def main():
args = parse_file()
date = get_date(args.dict_file)
filename = f"CC-CEDICT-{date}"
filename += "-bullets" if args.is_separate else ""
filename += "-numberedpinyin" if args.is_number else ""
filename += ".zip"
output_file = Path(args.output_directory, filename)
with zipfile.ZipFile(output_file, "w") as zipf:
zipf.writestr("index.json", format_obj(create_index(date)))
# Skip last comment line in dict_file
next(args.dict_file)
for i, term_bank in enumerate(create_termbanks(args)):
zipf.writestr(f"term_bank_{i+1}.json", format_obj(term_bank))
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
print(f'date={date}', file=fh)
print("Done")
if __name__ == "__main__":
main()