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
304
305
306
307
308
309
|
import tempfile as tmp
import re
import sys
import msgpack
from listSection import listInfoSection
from template import template
"""
Extract words from the Wiktionnary archive
"""
template_second = ['link', 'bd', 'pc', 'nom w pc', 'w', 'smcp', 'lien', 'ws',
'in', 'siècle2', 'fchim', 'nobr', 'wp', 'r',
'clé de tri', 'contexte', 'emploi', 'l', 'polytonique',
'pron-API', 'registre', 'scmp', 'siècle', 'x',
]
template_second_lambda_trd = {
'refnec': (lambda x: '(Référence nécessaire : ' + x + ')'),
'refnéc': (lambda x: '(Référence nécessaire : ' + x + ')'),
}
template_second_lambda_snd = {
'term': (lambda x: '(' + x.title() + ')'),
'terme': (lambda x: '(' + x.title() + ')'),
'ex': (lambda x: '^{' + x if x else 'e' + '}'),
'exp': (lambda x: '^{' + x if x else 'e' + '}'),
'e': (lambda x: '^{' + x if x else 'e' + '}'),
'er': (lambda x: '^{' + x if x else 'er' + '}'),
'ère': (lambda x: '^{' + x if x else 'ère' + '}'),
'ème': (lambda x: '^{' + x if x else 'ème' + '}'),
'Ier': (lambda x: '^{' + x if x else 'Ier' + '}'),
'III': (lambda x: '^{' + x if x else 'III' + '}'),
'III': (lambda x: '^{' + x if x else 'III' + '}'),
'small': (lambda x: '_{' + x if x else '' + '}'),
'indice': (lambda x: '_{' + x if x else '' + '}'),
'graphie': (lambda x: '«' + x if x else '»'),
'petites capitales': (lambda x: x.upper()),
'isbn': (lambda x: 'cf. ISBN ' + x),
'OCLC': (lambda x: 'cf. OCLC ' + x),
'variante de': (lambda x: 'Variante de ' + x),
'variante de': (lambda x: 'Variante de ' + x),
'variante ortho de': (lambda x: 'Variante orthographique de ' + x),
'variante ortho de': (lambda x: 'Variante orthographique de ' + x),
'variante ortho de': (lambda x: 'Variante orthographique de ' + x),
'variante orthographique de': (lambda x: 'Variante orthographique de ' + x),
'sic !': (lambda x: '^{sic ' + x + '}'),
'sic': (lambda x: '^{sic ' + x + '}'),
'incise': (lambda x: '_' + x + '_'),
'n°': (lambda x: 'n°' + x),
'superlatif de': (lambda x: 'Superlatif de' + x),
'vérifier': (lambda x: '(À vérifier : ' + x + ')'),
}
dictMatch = {x['match']: i for (i, x) in enumerate(listInfoSection)}
interdit = " :"
def transclusion(trans, info):
trans = trans[2:-2]
while '{{' in trans:
l0 = trans.rfind('{{')
l1 = trans.find('}}', l0)
if l1 == -1:
break
else:
l1 += 2
t = trans[l0:l1]
t = transclusion(t, info)
trans = trans[:l0] + t + trans[l1:]
s = list(map(lambda x: x.strip(), trans.split('|')))
if s[0] in template:
return template[s[0]]
if s[0].lower() in template_second:
return s[1] if len(s) > 1 else title
if s[0].lower().startswith('citation'):
cit = s[0].split('/')
if len(cit) == 4:
return 'Par ' + cit[1] + ', ' + cit[2] + ', ' + cit[3]
if len(cit) == 3:
return 'Par ' + cit[1] + ', ' + cit[2]
if len(cit) == 5:
return cit[1] + '/' + cit[2] + '/' + cit[3] + ', ' + cit[4]
if len(cit) <= 2:
return ''
else:
return '/'.join(c[1:])
if s[0].lower() in template_second_lambda_snd:
return template_second_lambda_snd[s[0].lower()](s[1] if len(s) > 1 else '')
if s[0].lower() in template_second_lambda_trd:
return template_second_lambda_trd[s[0].lower()](s[2] if len(s) > 2 else '')
with open('wiki_err.log', 'a') as err:
print(s[0], file=err)
# print("Incompréhension de la transclusion {} du mot {}".format(trans,
# info['mot']), file=err)
return ''
def extract(f, w):
infoFin = []
toRead = True
goBack = 0
while toRead:
toRead = False
info = {'mot': w,
'cat-gram': None,
'def': [],
'API': None,
'infos': [],
'genre': '',
'accord': None}
# State 0 // Initialisation !
while line := f.readline():
if line.startswith('=== ') or line.startswith('==={'):
if re.match('^=== *{{ *S\\|([^|]+)|.*$', line):
try:
r = re.match('^=== *{{ *S\\|([^|]+)|.*$', line)
r = r.groups()
nat = r[0].strip()
if nat in dictMatch.keys():
info['cat-gram'] = nat
toRead = True
break
except e:
with open('wiki_err.log', 'a') as err:
print("^[1] Problème à l'initialisation du mot {}:"
" {}".format(info['mot'], e), file=err)
print('line: [{}]'.format(line, e), file=err)
e = sys.exc_info()[0]
print("Erreur :", e, file=err)
if not toRead:
break
# State 1
while line := f.readline():
if line.startswith('{{fr-'):
e = line.find('}}')
if e == -1:
continue
ex = line[:e]
try:
infos = list(map(lambda x: x.strip(), ex.split('|')))
info['infos'] = infos
info['accord'] = infos[0]
if len(infos) > 1:
info['API'] = infos[1]
except e:
err = sys.exc_info()[0]
print(ex)
print("Erreur :", e)
print("Erreur :", err)
if line.rstrip().startswith("'''"):
if '{{pron' in line:
p0 = line.find('{{pron')
p1 = line.find('}}', p0)
if p1 > 0:
p1 += 2
p = line[p0:p1]
p = p.split('|')
info['API'] = p[1]
if '{{m}}' in line:
info['genre'] = 'mas'
elif '{{f}}' in line:
info['genre'] = 'fem'
if line.startswith('# '):
info['def'].append({'def': wikiToMd(line[2:], info)})
elif line.startswith('#* '):
if not info['def']:
with open('wiki_err.log', 'a') as err:
print("Exemple sans définition pour le mot {}".format(
info['mot']), file=err)
elif 'ex' in info['def'][-1]:
info['def'][-1]['ex'].append(wikiToMd(line[3:], info))
else:
info['def'][-1]['ex'] = [wikiToMd(line[3:], info)]
elif line.startswith('#') and not line.startswith('##'):
info['def'].append({'def': wikiToMd(line[1:], info)})
if line.startswith('==='):
goBack = len(line)
break
if goBack:
tf.seek(tf.tell() - goBack)
goBack = 0
toRead = True
infoFin.append(info)
return infoFin
def wikiToMd(line, info):
line = line.strip()
# 3 Étapes:
# - Links [...]
# - Style ''ita'' / '''bold'''
# - Template / Transclusion {{info}} = (Informatique)
# Template
while '{{' in line:
l0 = line.rfind('{{')
l1 = line.find('}}', l0)
if l1 == -1:
break
else:
l1 += 2
trans = line[l0:l1]
trans = transclusion(trans, info)
line = line[:l0] + trans + line[l1:]
# Links !
while '[[' in line:
link0 = line.rfind('[[')
link1 = line.find(']]', link0)
if link1 == -1:
break
else:
link1 += 2
link = line[link0:link1]
link = link[2:-2].split('|')
line = line[:link0] + (link[1] if len(link) > 1 else link[0]) + line[link1:]
# Style
line = line.replace("'''", '*').replace("''", '')
return line
with open("./fr_wiktionary_all.xml", 'r') as f:
title = ""
isFr = False
hasForbidden = False
hasText = False
tf = None
dict_ = dict()
for line in f:
if "</page>" in line and tf:
tf.seek(0)
i = extract(tf, title)
for w in i:
if w['mot'] == 'président':
print("What we exstract from it:")
print(i)
dict_[title] = i
tf.close()
tf = None
hasForbidden = False
hasText = False
isFr = False
title = ""
elif "</page>" in line:
tf = None
hasForbidden = False
hasText = False
isFr = False
title = ""
if "<title>" in line:
title = line[line.find('>') + 1:]
title = title[:title.find('<')]
for c in interdit:
if c in title:
hasForbidden = True
if not hasForbidden and "<text xml:space=\"preserve\">" in line:
hasText = True
if not hasForbidden and "== {{langue|fr}}" in line and hasText:
isFr = True
if tf:
print("Erreur tf encore ouvert !")
exit(-1)
tf = tmp.NamedTemporaryFile(mode="w+t")
# print(title)
elif not hasForbidden and "== {{langue|" in line:
isFr = False
if not hasForbidden and isFr and tf:
tf.write(line)
print("Will save the result")
with open('result_all.pack', 'wb') as f:
to_w = msgpack.packb(dict_)
f.write(to_w)
|