-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCreateAnkiImport_GreWord.py
More file actions
167 lines (163 loc) · 7.71 KB
/
CreateAnkiImport_GreWord.py
File metadata and controls
167 lines (163 loc) · 7.71 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
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
162
163
164
165
166
167
# coding:utf-8
import json
import codecs
import os
from my_helpers import *
file_name_new3000 = 'new3000_base_d.txt'
file_name_zhuji = 'zhuji_base_d.txt'
file_name_bzsdbdc = 'base_data\\bzsdbdc_dic.txt'
output_file_GreWord = 'AnkiImportData_GreWord.txt'
new3000_base_d = None
zhuji3000_base_d = None
bzsdbdc_data = None
new3000_base_d = is_file_and_json_load(file_name_new3000)
zhuji3000_base_d = is_file_and_json_load(file_name_zhuji)
bzsdbdc_data = is_file_and_json_load(file_name_bzsdbdc)
no_data_new3000 = new3000_base_d is None
no_data_zhuji = zhuji3000_base_d is None
no_data_bzsdbdc = bzsdbdc_data is None
def add_extra_fields():
if no_data_new3000:
print 'New3000 data file does not exists! Nothing can be done...'
return
iter_path = [('all','',True), ('key','usages',False),('all','',False)]
for word, usage_d in iter_through_general(new3000_base_d, iter_path):
usage_d['audio'] = ''
usage_d['mynotes'] = ''
for word_d in new3000_base_d.itervalues():
word_d['similar_form'] = ''
def convert_to_GreWord():
if no_data_new3000:
print 'New3000 data file does not exists! Nothing can be done...'
return
if no_data_zhuji:
print 'No data of zhuji!'
if no_data_bzsdbdc:
print 'No data of bzsdbdc!'
output_list = []
None_repr = u''
join_by_line_break = u'<br>'.join
replace_with_br = lambda _str: _str.replace('\n', '<br>')
tag_pos_prefix = ' in_'
for word in new3000_base_d:
# new 3000 part
"""
the structure of a word of new3000_base_d.txt
{'phon': u"[\u02cc\xe6d'l\u026ab]",
'pos': (1, 6),
'usages': [{'ants': u'\u53cd\u3000considered, planned, premeditated, rehearsed \u9884\u5148\u8ba1\u5212\u7684',
'ants_d': {'cn': u'\u9884\u5148\u8ba1\u5212\u7684',
'en': u'considered, planned, premeditated, rehearsed ',
'en_cn': u'considered, planned, premeditated, rehearsed \u9884\u5148\u8ba1\u5212\u7684'},
'der': '',
'examples': u'content...',
'en': u'not bad for an ad-lib comedy routine',
'en_cn': u'content...'},
'exp': u'*adj.* \u5373\u5174\u7684\uff1amade or done **without previous thought or preparation**',
'exp_d': {'cn': u'\u5373\u5174\u7684',
'en': u'made or done **without previous thought or preparation**',
'en_cn': u'\u5373\u5174\u7684\uff1amade or done **without previous thought or preparation**'},
'ph_symbl': u"[\u02cc\xe6d'l\u026ab]",
'pspeech': u'adj.',
'syns': u'content...'}
"""
one_new3000_word_d = new3000_base_d[word]
word_pos_L, word_pos_U = one_new3000_word_d['pos']
word_pos = u'L' + unicode(word_pos_L) + u' U' + unicode(word_pos_U)
num_usages = len(one_new3000_word_d['usages'])
usages_tag = unicode(num_usages) + u'_usage'
for usage_index, usage in enumerate(one_new3000_word_d['usages']):
word_phs = usage['ph_symbl']
word_tags = usages_tag + tag_pos_prefix + 'zaiyaoniming3000'
if not no_data_zhuji:
if word in zhuji3000_base_d:
word_tags += tag_pos_prefix + 'zhuji3000'
if not no_data_bzsdbdc:
if word in bzsdbdc_data:
word_tags += tag_pos_prefix + 'bzsdbdc'
usage_index = unicode(usage_index+1)
word_uid = unicode(word) + usage_index
ph_symbl = usage['ph_symbl']
word_Audio = usage['audio']
pspeech = usage['pspeech']
exp_en = usage['exp_d']['en']
exp_cn = usage['exp_d']['cn']
exp_en_cn = usage['exp_d']['en_cn']
# combine other explanation
#usage_index_l = range(num_usages)
#usage_index_l.remove(usage_index)
#exp_other = ['**考法%d**:'%(i+1) + one_new3000_word_d['usages'][i]['exp_d']['en_cn'] +'\n' for i in usage_index_l]
# use word_block_str as all explanation
exp_all = one_new3000_word_d['word_block_str']
examples_en = usage['examples_d']['en']
examples_cn = usage['examples_d']['cn']
examples_en_cn = usage['examples_d']['en_cn']
examples_others = ''
ants_en = usage['ants_d']['en']
ants_cn = usage['ants_d']['cn']
ants_en_cn = usage['ants_d']['en_cn']
syns = usage['syns']
# der from the book zaiyaoniming3000
der_new3000 = usage['der']
# bzsdbdc part
how_to_mem_bzsdbdc = None_repr
if not no_data_bzsdbdc:
if word in bzsdbdc_data:
how_to_mem_bzsdbdc = bzsdbdc_data[word]['combined']
# zhuji3000 part
how_to_mem_zhuji3000, eytma_gr, eytma_gr_exp, eytma_cognates = None_repr, None_repr, None_repr, None_repr
'''
the structure of a word of zhuji3000_base_d
{'content': u'[\u6839] per- [through] + vad [go] + -e [v.], go through, \u904d\u5e03 \u2192 vt. \u5f25\u6f2b\uff0c\u5145\u6ee1\n',
'ety': 'vad, vag, ced',
'etyma_cognates_l': u'pervade, evasive, extravagant, vague, cessation, incessant',
'etyma_group_explanation': u'group explanation content',
'phon': u"[p\u0259r've\u026ad]",
'pos': u'6, 7',
'summary': u'summary content',
'word': u'pervade'}
'''
if not no_data_zhuji:
if word in zhuji3000_base_d:
how_to_mem_zhuji3000 = zhuji3000_base_d[word]['content']
eytma_gr = zhuji3000_base_d[word]['ety']
eytma_gr_exp = zhuji3000_base_d[word]['etyma_group_explanation']
eytma_cognates = zhuji3000_base_d[word]['etyma_cognates_l']
# extra fields
mynotes = usage['mynotes']
similar_form = one_new3000_word_d['similar_form']
"""
Anki GreWord Structure
word_uid word usage_index ph_symbl word_audio pspeech mynotes
exp_en exp_cn exp_en_cn exp_all
examples_en examples_cn examples_encn examples_others
ants_en ants_cn ants_encn
syns der_new3000
how_to_mem_bzsdbdc how_to_mem_zhuji3000
etyma_group etyma_group_exp etyma_cognates
position similar_form tags
"""
one_line = [word_uid, word, usage_index, ph_symbl, word_Audio, pspeech, mynotes,
exp_en, exp_cn, exp_en_cn, exp_all,
examples_en, examples_cn, examples_en_cn, examples_others,
ants_en, ants_cn, ants_en_cn] +\
[syns, der_new3000, how_to_mem_bzsdbdc, how_to_mem_zhuji3000,
eytma_gr, eytma_gr_exp, eytma_cognates, word_pos, similar_form, word_tags]
for index, _str in enumerate(one_line):
_str = replace_with_br(collapse_blank_line(_str).strip(' \n'))
one_line[index] = custom_html_element(_str)
output_list.append(one_line)
output_list.sort(key=lambda x: x[0])
return output_list
def main():
add_field_audio_and_mynotes()
output_list = convert_to_GreWord()
if output_list is None:
return
with codecs.open(output_file_GreWord, 'w', encoding='utf-8') as f:
for one_line in output_list:
one_string = u'\t'.join(one_line) + '\n'
f.write(one_string)
del output_list
if __name__ == '__main__':
main()