-
Notifications
You must be signed in to change notification settings - Fork 30
/
validate_odenet.py
420 lines (345 loc) · 17.4 KB
/
validate_odenet.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# OdeNet-Validierung
# angelehnt an https://github.com/globalwordnet/english-wordnet/blob/master/scripts/validate.py
# Importe
#from odenet import *
from xml.etree import ElementTree as ET
from lxml import etree
import re
g_wordDict = None
########## Zugriff auf das Wordnet #######
def get_wordnet_lexicon_local(wnfile):
loc_wn = open(wnfile,"r",encoding="utf-8")
wntree = ET.parse(loc_wn)
wnroot = wntree.getroot()
lexicon = wnroot.find('Lexicon')
return lexicon
########## LexEntries ######
def get_word_dict(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
wordDict = {}
for synset in lexicon.findall('./Synset'):
wordDict[synset.attrib['id']] = []
for lexentry in lexicon.findall('./LexicalEntry'):
for sense in lexentry.findall('./Sense'):
lemma = lexentry.find('Lemma').attrib['writtenForm']
wordDict[sense.attrib['synset']].append(lemma)
return wordDict
def check_word_id(word_id, wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for lexentry in lexicon.iter('LexicalEntry'):
lemma = lexentry.find('Lemma')
lemma_value = lemma.attrib['writtenForm']
lemma_id = lexentry.attrib['id']
if lemma_id == word_id:
pos = lemma.attrib['partOfSpeech']
senses = []
for sense in lexentry.iter('Sense'):
sense_id = sense.attrib['id']
synset_id = sense.attrib['synset']
senses.append([sense_id,synset_id])
# print("LEMMA ID: " + lemma_id + "LEMMA: " + lemma_value + "\nPOS: " + pos + "\nSENSE ID: " + sense_id)
print("LEMMA ID:\t" + lemma_id + "\tLEMMA:\t" + lemma_value + "\tPOS:\t" + pos + "\tSENSE:\t" + sense_id)
def find_all_lexentries(word_to_check, wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for lexentry in lexicon.iter('LexicalEntry'):
lemma = lexentry.find('Lemma')
lemma_value = lemma.attrib['writtenForm']
lemma_id = lexentry.attrib['id']
word_id_list = []
if lemma_value == word_to_check:
word_id_list.append(lemma_id)
for wid in word_id_list:
check_word_id(wid, wordnet)
# Prüfung, ob es für ein Lemma mehrere LexEntries gibt
def find_duplicate_lexentries(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
lemma_list = []
for lexentry in lexicon.iter('LexicalEntry'):
lemma = lexentry.find('Lemma')
lemma_value = lemma.attrib['writtenForm']
lemma_id = lexentry.attrib['id']
if lemma_value in lemma_list:
find_all_lexentries(lemma_value,wordnet)
else:
lemma_list.append(lemma_value)
# print("Es gibt doppelte Einträge für " + str(len(lemma_list)) + " Lemmata!")
# Prüfung, ob die Lex-ID mit der Sense-ID übereinstimmt
def find_inconsistent_lexids(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for lexentry in lexicon.iter('LexicalEntry'):
lemma_id_full = lexentry.attrib['id']
lemma_id = lemma_id_full[:-2]
for sense in lexentry.iter('Sense'):
sense_id = sense.attrib['id']
sense_w_id = sense_id.split('_')[0]
if sense_w_id != lemma_id:
print ("LexIDs inkonsistent in: " + lemma_id + ": " + sense_w_id)
# Prüfung, ob eine Synset-ID im LexEntry überhaupt existiert
# und ob eine Synset-ID in einer Relation überhaupt existiert
def test_for_existance_of_synset(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
list_of_synsets = []
for synset in lexicon.iter('Synset'):
list_of_synsets.append(synset.attrib['id'])
for lexentry in lexicon.iter('LexicalEntry'):
lemma_id = lexentry.attrib['id']
for sense in lexentry.iter('Sense'):
sense_synset = sense.attrib['synset']
if sense_synset not in list_of_synsets:
print("Synset " + sense_synset + " from LexEntry " + lemma_id + " is not defined!")
for synset in lexicon.iter('Synset'):
for relation in synset.iter('SynsetRelation'):
if relation.attrib['target'] not in list_of_synsets:
print("Synset " + synset.attrib['id'] + " has a relation to " + relation.attrib['target'] + ", but the target is not defined!")
########## POS ###########
def words_in_synset(id, wordnet):
return(g_wordDict[id])
# 1. LexEntries mit mehreren Senses, die POS haben, das anders als das POS des Lemmas ist
# 2. Alle Wörter mit Großbuchstaben, die POS v oder a haben
# 3. Wörter auf -lich und -isch, die kein Adjektiv sind.
def test_lexentries_pos(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
# out = open("out.txt","w",encoding="utf-8")
for lexentry in lexicon.iter('LexicalEntry'):
lemma = lexentry.find('Lemma')
lemma_value = lemma.attrib['writtenForm']
# pos = lemma.attrib['partOfSpeech']
try:
pos = lemma.attrib['partOfSpeech']
except:
print('problem with ' + lemma_value)
for sense in lexentry.iter('Sense'):
synset_id = sense.attrib['synset']
if not synset_id[-1] == pos:
words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
if re.match(r'^[A-ZÄÜÖ][a-zäöüß]+$',lemma_value):
if pos == 'v':
synset_id = sense.attrib['synset']
words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
# if re.match(r'^[a-zäöü]+$',lemma_value):
# if pos == 'n':
# synset_id = sense.attrib['synset']
# words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
# print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
if re.match(r'^[a-zäöü]+lich$',lemma_value):
if pos == 'n':
synset_id = sense.attrib['synset']
words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
elif pos == 'v':
synset_id = sense.attrib['synset']
words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
if re.match(r'^[a-zäöü]+isch$',lemma_value):
if pos == 'n':
synset_id = sense.attrib['synset']
words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
elif pos == 'v':
synset_id = sense.attrib['synset']
words = words_in_synset(synset_id,wordnet)
# out.write("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
print("-- " + lemma_value + " : " + pos + ": " + synset_id + " " + str(words) + " \n\n")
# out.close()
# Prüfung darauf, ob alle POS in einem Synset dieselben sind
# 1. stimmt der POS aus der ID mit dem aus dem Attribut überein?
def test_synsets_pos(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for synset in lexicon.iter('Synset'):
if synset.attrib['id'][-1] != synset.attrib['partOfSpeech']:
print("Multiple POS values in " + synset.attrib['id'])
# Prüfung darauf, ob POS in Synset und Relation Target übereinstimmen
# ('odenet-9996-n', '<SynsetRelation target="odenet-807-v" relType="hypernym"/>')
def test_relation_pos(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for synset in lexicon.iter('Synset'):
synset_pos = synset.attrib['partOfSpeech']
for relation in synset.iter('SynsetRelation'):
if relation.attrib['target'][-1] != synset_pos:
# print("Synset " + synset.attrib['id'] + ' has different POS values in relation targets: <SynsetRelation target="' + relation.attrib['target'] + '" relType="' + relation.attrib['relType'] + '"/>')
print("('" + synset.attrib['id'] + "', '" + '<SynsetRelation target="' + relation.attrib['target'] + '" relType="' + relation.attrib['relType'] + '"/>' + "')")
# Prüfung darauf, ob POS in OdeNet und PWN übereinstimmen --> TODO
########### SYNSET #############
# Prüfung auf Synsets ohne LexEntries
def test_for_empty_synsets(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for synset in lexicon.iter('Synset'):
wlist = words_in_synset(synset.attrib['id'],wordnet)
if len(wlist) == 0:
print("Synset " + synset.attrib['id'] + " has no words!")
# Prüfung auf valide ids
#valid_word_id = re.compile("^w[0-9]+$")
valid_word_id = re.compile("^omw-de-w[0-9]+-z$")
#omw-de-w1-z
#valid_sense_id = re.compile("^w[0-9]+_[0-9]+-[nvapx]$")
valid_sense_id = re.compile("^omw-de-w[0-9]+_[0-9]+-[nvapx]$")
#omw-de-w1_1-n
#valid_synset_id = re.compile("^odenet-[0-9]+-[nvapx]$")
valid_synset_id = re.compile("^omw-de-[0-9]+-[nvapx]$")
#omw-de-3899-n
def is_valid_word_id(xml_id):
return bool(valid_word_id.match(xml_id))
def is_valid_synset_id(xml_id):
return bool(valid_synset_id.match(xml_id))
def is_valid_sense_id(xml_id):
return bool(valid_sense_id.match(xml_id))
def test_for_valid_ids(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for lexentry in lexicon.iter('LexicalEntry'):
if not is_valid_word_id(lexentry.attrib['id']):
print(lexentry.attrib['id'] + " is not valid")
for sense in lexentry.iter('Sense'):
if not is_valid_sense_id(sense.attrib['id']):
print("Sense ID " + sense.attrib['id'] + " is not valid")
if not is_valid_synset_id(sense.attrib['synset']):
print("Synset ID " + sense.attrib['synset'] + " is not valid")
for synset in lexicon.iter('Synset'):
if not is_valid_synset_id(synset.attrib['id']):
print("Synset ID " + synset.attrib['id'] + " is not valid")
# Prüfen, ob ilis doppelt vergeben wurden
def test_for_duplicate_ilis(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
ili_list = []
for synset in lexicon.iter('Synset'):
synset_ili = synset.attrib['ili']
if len(synset_ili) > 0:
if synset_ili in ili_list:
print("Duplicated ILI: " + str(synset_ili))
else:
ili_list.append(synset_ili)
######### RELATIONEN ########
# 1. Prüfung, ob Relationen das Target fehlt
# 2. Prüfung auf Loops: Synsets, die als Target einer Relation sich selbst haben
def test_target_in_relation(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
for synset in lexicon.iter('Synset'):
id = synset.attrib['id']
for relation in synset.iter('SynsetRelation'):
try:
reltarget = relation.attrib['target']
except KeyError:
print(id + " is missing the target!")
if relation.attrib['target'] == id:
print(id + " has a relation to itself!")
# Prüfung auf Loops: Zwei Synsets zeigen gegenseitig auf sich mit derselben Relation
''' Z.B.:
<Synset id="odenet-10502-n" ili="i112933" partOfSpeech="n"><SynsetRelation target='odenet-10742-n' relType='hyponym'/></Synset>
<Synset id="odenet-10742-n" ili="i112931" partOfSpeech="n"><SynsetRelation target='odenet-10502-n' relType='hyponym'/></Synset>
'''
# 1. Ich erstelle eine Liste von allen Hypernym-Relationen und eine Liste von allen Hyponym-Relationen
def get_all_hyponym_hypernym_relations(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
hyponym_relations = []
hypernym_relations = []
for synset in lexicon.iter('Synset'):
synset_id = synset.attrib['id']
for relation in synset.iter('SynsetRelation'):
if relation.attrib['relType'] == 'hyponym':
hyponym_relations.append([synset_id,relation.attrib['target']])
elif relation.attrib['relType'] == 'hypernym':
hypernym_relations.append([synset_id,relation.attrib['target']])
return(hyponym_relations,hypernym_relations)
def test_for_loops_in_hypo_relations(hypo_list):
for relation in hypo_list:
if hypo_list.count(relation) > 1:
print("Duplicate relation: " + str(relation))
converse_relation = [relation[1],relation[0]]
if converse_relation in hypo_list:
print("Loop in relation " + str(relation))
# Prüfung auf fehlende symmetrische Relationen (Hypero-Hyponym)
def test_for_missing_symmetry(hyponym_relations,hypernym_relations):
for relation in hyponym_relations:
converse_relation = [relation[1],relation[0]]
if converse_relation not in hypernym_relations:
print("Asymmetric relation: " + str(relation))
for relation in hypernym_relations:
converse_relation = [relation[1],relation[0]]
if converse_relation not in hyponym_relations:
print("Asymmetric relation: " + str(relation))
def test_for_loops_in_relations(wordnet):
hyponym_relations,hypernym_relations = get_all_hyponym_hypernym_relations(wordnet)
test_for_loops_in_hypo_relations(hyponym_relations)
test_for_loops_in_hypo_relations(hypernym_relations)
test_for_missing_symmetry(hyponym_relations,hypernym_relations)
# höchste Synset-ID
# omw-de-29561-v
def highest_synset_id(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
top_id = 0
for synset in lexicon.iter('Synset'):
synset_id = synset.attrib['id']
odenet, de, number, pos = synset_id.split("-")
if int(number) > top_id:
top_id = int(number)
else:
top_id = top_id
print("Highest synset ID: " + str(top_id))
return(top_id)
# höchste LexEntry-ID
#omw-de-w9803-z
def highest_lex_id(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
top_id = 0
for lexentry in lexicon.iter('LexicalEntry'):
lex_id = lexentry.attrib['id']
odenet, de, number, pos = lex_id.split("-")
lex_number = number[1:]
if int(lex_number) > top_id:
top_id = int(lex_number)
else:
top_id = top_id
print("Highest lexentry ID: w" + str(top_id))
return(top_id)
# Test auf valides XML
def test_valid_xml(wordnet):
parser = etree.XMLParser(dtd_validation=True, no_network=False)
tree = etree.parse(wordnet, parser)
def test_necessary_conditions(wordnet):
lexicon = get_wordnet_lexicon_local(wordnet)
# g_wordDict = get_word_dict(wordnet)
print("Test for valid xml ...")
test_valid_xml(wordnet)
# print("Test for duplicate LexEntries ...")
# find_duplicate_lexentries(wordnet)
print("Test for POS in LexEntries ...")
test_lexentries_pos(wordnet)
print("Test for POS in Synsets ...")
test_synsets_pos(wordnet)
# print("Test for POS in Relations ...")
# test_relation_pos(wordnet)
print("Test for targets in relations ...")
test_target_in_relation(wordnet)
# print("Test for loops and missing symmetry in relations ...")
# test_for_loops_in_relations(wordnet)
print("Test for existance of synsets ...")
test_for_existance_of_synset(wordnet)
print("Test for duplicate ilis...")
test_for_duplicate_ilis(wordnet)
print("Test for synsets without words ...")
test_for_empty_synsets(wordnet)
print("Test for inconstent IDs in LexEntries ...")
find_inconsistent_lexids(wordnet)
print("Test for valid IDs")
test_for_valid_ids(wordnet)
highest_synset_id(wordnet)
highest_lex_id(wordnet)
#Main Program
g_wordDict = get_word_dict('odenet/wordnet/deWordNet.xml')
test_necessary_conditions('odenet/wordnet/deWordNet.xml')
#find_inconsistent_lexids(r'C:\Users\Melanie Siegel\Documents\05_Projekte\OdeNet\OdeNet_corrections\mini_odenet.xml')
#find_inconsistent_lexids('odenet/wordnet/deWordNet.xml')
#test_for_valid_ids('odenet/wordnet/deWordNet.xml')
#test_valid_xml('odenet/wordnet/deWordNet.xml')
#test_valid_xml(r'C:\Users\Melanie Siegel\Documents\05_Projekte\OdeNet\OdeNet_corrections\mini_odenet.xml')
#find_duplicate_lexentries('odenet/wordnet/deWordNet.xml')
#test_lexentries_pos('odenet/wordnet/deWordNet.xml')
#highest_synset_id('odenet/wordnet/deWordNet.xml')
#highest_lex_id('odenet/wordnet/deWordNet.xml')