This repository was archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
135 lines (108 loc) ยท 4.3 KB
/
generator.py
File metadata and controls
135 lines (108 loc) ยท 4.3 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
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
import json
import os
import sys
VERSION = "1.0.0"
WORDBOOK_VERSION = 100
SUPPORTED_DICTS = {
"Senior": "้ซไธญ็่ฏๅ
ธ",
"PureEnglishAndExample": "ๆ้็ฎๆ้ไน"
}
def printLogo():
print("""โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโฆโโโโโโฌโโโโ โ
โ โ โโโโค โโโโโโโ โ โโโโโ โ
โ โฉ โโโโโโโฉ โฉโโโโโดโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Welcome to use WordBookTools!
Developer: RedbeanW
Repo: https://github.com/PenUniverse/WordBookTools
Version: %s""" % VERSION)
def terminate(msg:str = None):
if msg:
print(msg)
os.system('pause')
sys.exit(-1)
if __name__ == '__main__':
printLogo()
if not os.path.exists('WordBook.json'):
terminate('่ฏทๅฐ WordBook.json ๆพๅจ่ๆฌๅ็บง็ฎๅฝๅ้่ฏใ')
obj = None
try:
with open('WordBook.json', 'r', encoding='utf-8') as file:
obj = json.load(file)
except:
terminate('่งฃๆ WordBook.json ๆถๅบ้...')
if obj["version"] != WORDBOOK_VERSION:
terminate('ไธๆฏๆ็ๅ่ฏๆฌๅฏผๅบๆ ผๅผ๏ผ็ๆฌไธๅน้
๏ผ%s!=%s๏ผ' % (obj["version"], WORDBOOK_VERSION))
else:
obj = obj["data"]
sorted_words = []
handled_words = []
fromSenior = obj["Senior"]
fromPureEng = obj["PureEnglishAndExample"]
# Sort all words.
for i in range(5):
frequency = 5 - i
for word in fromSenior:
if frequency == fromSenior[word]["frequency"]:
sorted_words.append(word)
for word in fromPureEng:
if word not in sorted_words: # can improve performance
sorted_words.append(word)
# Handle
result = str()
count = 0
max = len(sorted_words)
for word in sorted_words:
count += 1
# priority [senior]->[pure_eng]
if word in fromSenior:
print('[processing(%s/%s)|Senior] "%s".' % (count, max, word))
item = fromSenior[word]
## handle marker
mark = min(item["frequency"], 3) * '\*'
## handle trans
tset = {}
for t in item["trans"]:
key = t["pos"]
if key in tset:
tset[key] += '๏ผ' + t['sense']
else:
tset[key] = t['sense']
trans = ''
for pos in tset:
trans += ('*%s* %s | ' % (pos, tset[pos]))
trans = trans[:len(trans) - 3]
## handle idiomatic
idios = ''
if "idiomatic" in item:
is_final_idio_has_colloc = False
for idio in item["idiomatic"]:
idios += "**`%s`** %s" % (idio["colloc"]["en"], idio["colloc"]["zh"])
if "sents" in idio:
idios += '\n\n> %s\n> %s\n\n' % (idio["sents"][0]['en'], idio["sents"][0]['zh'])
is_final_idio_has_colloc = True
else:
idios += '\n'
is_final_idio_has_colloc = False
if not is_final_idio_has_colloc:
idios += '\n'
## generate this word
result += "### %s%s\n\n###### %s\n\n%s" % (word, '', trans, idios)
elif word in fromPureEng:
print('[processing(%s/%s)|PureEng] "%s".' % (count, max, word))
item = fromPureEng[word]
## handle trans
trans = ''
for t in item['pure']['word']['trs']:
if 'pos' in t:
trans += (' *%s* %s | ' % (t['pos'], t['tran']))
else:
trans += (' %s | ' % (t['tran']))
trans = trans[:len(trans) - 3]
result += "**%s**%s\n" % (word, trans.replace('ใ','[').replace('ใ',']'))
with open('WordBook.md', 'w+', encoding='utf-8') as file:
file.write(result)
print("Completed, see 'WordBook.md'.")