This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport-krdict.py
303 lines (280 loc) · 14 KB
/
import-krdict.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import sys
import xml.etree.ElementTree as ET
import bot
import datetime
import tzlocal
PERMLINK_FMT = 'https://krdict.korean.go.kr/dicSearch/SearchView?ParaWordNo=%s'
def build_records_from_xml(filename):
records = []
xml = ET.parse(filename)
root = xml.getroot()
entries = root.findall('Lexicon/LexicalEntry')
for entry in entries:
rec = {}
rec['항목ID'] = entry.get('val')
for item in entry:
if item.tag == 'feat':
if item.get('att') == 'homonym_number':
rec['동형어 번호'] = item.get('val')
elif item.get('att') == 'lexicalUnit':
rec['구분'] = item.get('val')
elif item.get('att') == 'partOfSpeech':
rec['품사'] = item.get('val')
elif item.get('att') == 'vocabularyLevel':
rec['어휘 등급'] = item.get('val')
elif item.get('att') == 'subjectCategiory':
rec['의미 범주'] = item.get('val')
elif item.get('att') == 'semanticCategory':
rec['주제 및 상황 범주'] = item.get('val')
elif item.get('att') == 'origin':
rec['원어'] = item.get('val')
elif item.get('att') == 'annotation':
rec['전체 참고'] = item.get('val')
else:
raise Exception('Unknown att ' + item.get('att'))
elif item.tag == 'Lemma':
for subitem in item:
if subitem.get('att') == 'writtenForm':
rec['표제어'] = subitem.get('val')
elif subitem.get('att') == 'variant':
rec['검색용 이형태'] = subitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
elif item.tag == 'WordForm':
type = [x.get('val') for x in item if x.get('att') == 'type'][0]
if type == '발음':
if '발음' not in rec:
rec['발음'] = []
subrec = {}
for subitem in item:
if subitem.get('att') == 'type':
pass
elif subitem.get('att') == 'pronunciation':
if subitem.get('val'):
subrec['발음'] = subitem.get('val')
else:
subrec['발음'] = ''
elif subitem.get('att') == 'sound':
subrec['URL'] = subitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
rec['발음'].append(subrec)
elif type == '활용':
# '형태;발음;URL', 별개의 발음은 별개 활용에 하나 추가
if '활용' not in rec:
rec['활용'] = []
subrec = {}
for subitem in item:
if subitem.tag == 'feat':
if subitem.get('att') == 'type':
pass
elif subitem.get('att') == 'writtenForm':
if subitem.get('val'):
subrec['형태'] = subitem.get('val')
else:
subrec['형태'] = ''
elif subitem.get('att') == 'pronunciation':
if '발음' in subrec:
rec['활용'].append(subrec)
word = subrec['형태']
subrec = { '형태': word }
subrec['발음'] = subitem.get('val')
elif subitem.get('att') == 'sound':
subrec['발음 URL'] = subitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
elif subitem.tag == 'FormRepresentation':
rec['활용'].append(subrec)
word = subrec['형태']
subrec = {}
for subsubitem in subitem:
if subsubitem.get('att') == 'type':
frtype = subsubitem.get('val')
elif subsubitem.get('att') == 'writtenForm':
if subsubitem.get('val'):
subrec['형태'] = subsubitem.get('val')
else:
subrec['형태'] = ''
elif subsubitem.get('att') == 'pronunciation':
subrec['발음'] = subsubitem.get('val')
elif subsubitem.get('att') == 'sound':
subrec['발음 URL'] = subsubitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
# subrec[frtype] = subsubrec
rec['활용'].append(subrec)
subrec = {}
else:
raise Exception('Unknown tag ' + subitem.tag)
if subrec:
rec['활용'].append(subrec)
else:
raise Exception('Unknown WordForm type ' + type)
elif item.tag == 'Sense':
sense = {}
sense['의미ID'] = item.get('val')
for subitem in item:
if subitem.tag == 'feat':
if subitem.get('att') == 'annotation':
sense['의미 참고'] = subitem.get('val')
elif subitem.get('att') == 'definition':
sense['뜻풀이'] = subitem.get('val')
elif subitem.get('att') == 'syntacticAnnotation':
sense['문형 참고'] = subitem.get('val')
elif subitem.get('att') == 'syntacticPattern':
sense['문형'] = subitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
elif subitem.tag == 'SenseRelation':
subsense = {}
subsensetype = ''
for subsubitem in subitem:
if subsubitem.get('att') == 'type':
subsensetype = subsubitem.get('val')
elif subsubitem.get('att') == 'id':
if subsubitem.get('val'):
subsense['항목ID'] = subsubitem.get('val')
elif subsubitem.get('att') == 'lemma':
if subsubitem.get('val'):
subsense['표제어'] = subsubitem.get('val')
elif subsubitem.get('att') == 'homonymNumber':
if subsubitem.get('val'):
subsense['동형어 번호'] = subsubitem.get('val')
else:
raise Exception('Unknown att ' + subsubitem.get('att'))
# 내용이 없는 SenseRelation tag가 있으니 내용 여부 확인
if subsense:
if '관련어:' + subsensetype not in sense:
sense['관련어:' + subsensetype] = []
sense['관련어:' + subsensetype].append(subsense)
elif subitem.tag == 'SenseExample':
subtype, example = '', ''
for subsubitem in subitem:
if subsubitem.get('att') == 'type':
subtype = subsubitem.get('val')
elif subsubitem.get('att') == 'example':
example = subsubitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
if subtype == '1':
tt = '용례'
else:
tt = '용례:' + subtype
if tt not in sense:
sense[tt] = []
sense[tt].append(example)
elif subitem.tag == 'Multimedia':
subtype = ''
subsense = {}
for subsubitem in subitem:
if subsubitem.get('att') == 'type':
subtype = subsubitem.get('val')
elif subsubitem.get('att') == 'label':
subsense['레이블'] = subsubitem.get('val')
elif subsubitem.get('att') == 'url':
subsense['URL'] = subsubitem.get('val')
else:
raise Exception('Unknown att ' + subitem.get('att'))
if '다중 매체 정보:' + subtype not in sense:
sense['다중 매체 정보:' + subtype] = []
sense['다중 매체 정보:' + subtype].append(subsense)
else:
raise Exception('Unknown subitem tag ' + subitem.tag)
if '의미' not in rec:
rec['의미'] = {}
rec['의미'][sense['의미ID']] = sense
records.append(rec)
return records
def make_title(entryid, written, homonym='0'):
if int(homonym) > 0:
label = '%s (%03d)' % (written, int(homonym))
else:
label = written
label = label.replace('[','❲').replace(']','❳')
title = '%s (한국어기초사전 %s)' % (label, entryid)
return title
def format_record(rec, datetimestr):
assert('표제어' in rec)
assert('항목ID' in rec)
if '동형어 번호' in rec and int(rec['동형어 번호']) > 0:
title = make_title(rec['항목ID'], rec['표제어'], rec['동형어 번호'])
else:
title = make_title(rec['항목ID'], rec['표제어'])
lines = []
lines += ['{{#set:한국어기초사전:가져온 시각=%s}}' % datetimestr]
lines += ['{{#set:한국어기초사전:URL=%s}}' % (PERMLINK_FMT % rec['항목ID'])]
lines += ['{{#set:사전:원본 라이선스=CC BY-SA 2.0 KR}}']
lines += ['{{#set:사전:원본=한국어기초사전}}']
lines += ['{{#set:']
for k in sorted(rec.keys()):
v = rec[k]
if type(v) == str:
lines += ['|한국어기초사전:%s=%s' % (k, v)]
elif type(v) == list:
for subv in v:
if k == '활용':
if not subv['형태']:
continue
pron = subv['발음'] if '발음' in subv else ''
url = subv['발음 URL'] if '발음 URL' in subv else ''
subv = subv['형태'] + ';' + pron + ';' + url
elif k == '발음':
url = subv['URL'] if 'URL' in subv else ''
if not url and not subv['발음']:
continue
subv = subv['발음'] + ';' + url
lines += ['|한국어기초사전:%s=%s' % (k, subv)]
elif k == '의미':
pass
else:
raise Exception('No idea how to format ' + k)
lines += ['}}']
senses = rec['의미'] if '의미' in rec else {}
for senseid in sorted(senses.keys()):
sense = senses[senseid]
lines += ['{{#subobject:%s' % senseid]
for k in sorted(sense.keys()):
v = sense[k]
if type(v) == str:
lines += ['|한국어기초사전:%s=%s' % (k, v)]
elif k.startswith('관련어'):
for subv in v:
s = make_title(subv['항목ID'], subv['표제어'], subv['동형어 번호'])
lines += ['|한국어기초사전:%s=%s' % (k, s)]
elif k.startswith('용례'):
for subv in v:
lines += ['|한국어기초사전:%s=%s' % (k, subv)]
elif k.startswith('다중 매체 정보'):
for subv in v:
s = '%s;%s' % (subv['레이블'], subv['URL'])
lines += ['|한국어기초사전:%s=%s' % (k, s)]
else:
raise Exception('No idea how to format ' + k)
lines += ['}}']
content = '\n'.join(lines)
return title, content
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.stderr.write('Usage: %s <filename>...\n' % sys.argv[0])
sys.exit(1)
filenames = sys.argv[1:]
# get creationDate from xml
xml = ET.parse(filenames[0])
root = xml.getroot()
s = root.find('GlobalInformation/feat[@att="creationDate"]').get('val')
tz = tzlocal.get_localzone()
now = datetime.datetime.strptime(s, '%Y/%m/%d %H:%M:%S')
dt = tz.localize(now)
datetimestr = dt.isoformat()
records = []
for filename in filenames:
records += build_records_from_xml(filename)
botsession = bot.Bot()
botsession.login()
magic = 'IMPORT 한국어기초사전'
new_page_prefix = '{{사전 항목}}'
for i, record in zip(range(0, len(records)), records):
title, content = format_record(record, datetimestr)
botsession.insert_text(title, magic, content, new_page_prefix)
if i % 100 == 0:
print('Progress: %d/%d -- %s' % (i, len(records), title))