-
Notifications
You must be signed in to change notification settings - Fork 0
/
meaning.py
480 lines (437 loc) · 21.1 KB
/
meaning.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/python
# -*- coding: utf-8 -*-
import term
import structs
import re
class Meaning:
""" This class contains one meaning for a word or an expression.
"""
def __init__(self, term, definition='', etymology='',
synonyms={'remark': '',
'synonyms': [{'remark': '', 'synonym': ''}]},
translations=None, label='', concisedef='', examples=[]):
""" Constructor
Generally called with one parameter:
- The Term object we are describing
- definition (string) for this term is optional
- etymology (string) is optional
- synonyms (optional)
- translations (dictionary of Term objects, ISO639 is the key) is
optional
"""
self.term = term
self.definition = definition
self.concisedef = concisedef
self.etymology = etymology
self.synonyms = synonyms
# A structure, possibly containing the following items:
# {'remark' : 'this remark concerns all the synonyms for this meaning',
# 'synonyms' : [
# {'remark': 'concerns this particular synonym',
# 'synonym': Term object containing the synonym
# },
# ]
self.examples = examples
self.label = label
if translations:
self.translations = translations
else:
# a dictionary containing lists with translations to the different
# languages. Each translation is again a dictionary as follows:
# {'remark': '', 'trans': Term object}
self.translations = {}
# a remark applying to all the translations for this meaning
self.translationsremark = ''
# a dictionary containing remarks applying to a specific language
self.translationsremarks = {}
self.label = label
def setDefinition(self, definition):
""" Provide a definition """
self.definition = definition
def getDefinition(self):
""" Returns the definition """
return self.definition
def setEtymology(self, etymology):
""" Provide the etymology """
self.etymology = etymology
def getEtymology(self):
""" Returns the etymology """
return self.etymology
def setSynonyms(self, synonyms):
""" Provide the synonyms """
self.synonyms = synonyms
def getSynonyms(self):
""" Returns the list of synonym Term objects """
return self.synonyms
def parseSynonyms(self, synonymswikiline):
synsremark = ''
synonyms = []
openparenthesis = synonymswikiline.lower().find('(see')
if openparenthesis != -1:
closeparenthesis = synonymswikiline.find(')', openparenthesis)
synsremark = synonymswikiline[openparenthesis:closeparenthesis + 1]
synonymswikiline = synonymswikiline[:openparenthesis - 1] + \
synonymswikiline[closeparenthesis + 1:]
for synonym in synonymswikiline.split(','):
synremark = ''
openparenthesis = synonym.lower().find('(')
if openparenthesis != -1:
closeparenthesis = synonym.find(')', openparenthesis)
synremark = synonym[openparenthesis:closeparenthesis + 1]
synonym = synonym[:openparenthesis - 1] + \
synonym[closeparenthesis + 2:]
synonym = synonym.replace(
',', '').replace("[", '').replace(']', '').strip()
synonyms.append({'synonym': synonym, 'remark': synremark})
self.synonyms = {'remark': synsremark, 'synonyms': synonyms}
def parseTranslations(self, translationswikiline):
'''
This function will parse one line in wiki format
Typically this is the translation towards one language.
'''
# There can be many translations for a language, each one can have
# remark a gender and a number.
# There can also be a remark for the group of translations for a given
# language. And there can be a remark applying to all the translations
# (That has to be detected and stored on a higher level though.
# It is also possible that the translation for a given language is not
# parseable. In that case the entire line should go into the remark.
translationsremark = translationremark = ''
translations = [] # a list of translations for a given language
colon = translationswikiline.find(':')
if colon != -1:
# Split in lang and the rest of the line which should be a list of
# translations
lang = translationswikiline[:colon].replace(
'*', '').replace('[', '').replace(']', '').replace(
'{', '').replace('}', '').strip().lower()
trans = translationswikiline[colon + 1:]
# Look up lang and convert to an ISO abbreviation
isolang = ''
if lang in structs.langnames:
isolang = lang
elif lang in structs.invertedlangnames:
isolang = structs.invertedlangnames[lang]
# We need to prepare the line a bit to make it more easily parseable
# All the commas found between '' '' are converted to simple spaces
# Also }}, {{ has to be converted to }} {{
trans = "''".join([[i[1], re.sub(',', ' ', i[1])][i[0] % 2 == 1]
for i in enumerate(trans.split("''"))])
trans = re.sub(r"(}}.*),(.*{{)", '}} {{', trans)
# Now split up the translations (we got rid of extraneous commas)
for translation in trans.split(','):
translation = translation.strip()
# Find what is contained inside parentheses
m = re.search(r'(\(.*\))', translation)
if m:
# Only when the parentheses don't occur
# between [[ ]]
if translation[m.end(1) + 1:m.end(1) + 2] != ']':
translationremark = m.group(1).replace(
'(', '').replace(')', '')
translation = translation.replace(m.group(1), '')
number = 1
masculine = feminine = neutral = common = diminutive = False
partconsumed = False
for part in translation.split(' '):
part = part.strip()
colon = part.find(':')
if colon != -1:
colon2 = part.find(':', colon + 1)
pipe = part.find('|')
if colon2 != -1 and pipe != -1:
# We found a link to another language Wiktionary
# This contains no interesting information to store
# If the target Wiktionary uses them, we'll create
# them upon output
pass
else:
translationremark = part.replace(
"'", '').replace('(', '').replace(
')', '').replace(':', '')
partconsumed = True
cleanpart = part.replace("'", '').lower()
delim = ''
# XXX The following 3 tests look wrong:
# find() returns either -1 if the substring is not found,
# or the position of the substring in the string.
# since bool(-1) = True, cleanpart.find(',') will always
# be False, unless cleanpart[0] is ','
#
# the test "',' in cleanpart" might be the one to use.
if cleanpart.find(','):
delim = ','
if cleanpart.find(';'):
delim = ';'
if cleanpart.find('/'):
delim = '/'
if 0 <= part.find("'") <= 2 or '{' in part:
if delim == '':
delim = '|'
cleanpart += '|'
for maybegender in cleanpart.split(delim):
maybegender = maybegender.strip()
if maybegender == 'm' or maybegender == '{{m}}':
masculine = True
partconsumed = True
if maybegender == 'f' or maybegender == '{{f}}':
feminine = True
partconsumed = True
if maybegender == 'n' or maybegender == '{{n}}':
neutral = True
partconsumed = True
if maybegender == 'c' or maybegender == '{{c}}':
common = True
partconsumed = True
if maybegender == 'p' or maybegender == 'pl' or \
maybegender == 'plural' or \
maybegender == '{{p}}':
number = 2
partconsumed = True
if maybegender[:3] == 'dim' or \
maybegender == '{{dim}}':
diminutive = True
partconsumed = True
# print 'consumed: ', partconsumed
if not partconsumed:
# This must be our term
termweareworkingon = part.replace(
"[", '').replace("]", '').lower()
if '#' in termweareworkingon and \
'|' in termweareworkingon:
termweareworkingon = termweareworkingon.split(
'#')[0]
# Now we have enough information to create a term
# object for this translation and add it to our list
addedflag = False
if masculine:
thistrans = {'remark': translationremark,
'trans': term.Term(isolang,
termweareworkingon,
gender='m',
number=number,
diminutive=diminutive,
wikiline=translation)}
translations.append(thistrans)
addedflag = True
if feminine:
thistrans = {'remark': translationremark,
'trans': term.Term(isolang,
termweareworkingon,
gender='f',
number=number,
diminutive=diminutive,
wikiline=translation)}
translations.append(thistrans)
addedflag = True
if neutral:
thistrans = {'remark': translationremark,
'trans': term.Term(isolang,
termweareworkingon,
gender='n',
number=number,
diminutive=diminutive,
wikiline=translation)}
translations.append(thistrans)
addedflag = True
if common:
thistrans = {'remark': translationremark,
'trans': term.Term(isolang,
termweareworkingon,
gender='c',
number=number,
diminutive=diminutive,
wikiline=translation)}
translations.append(thistrans)
addedflag = True
# if it wasn't added by now, it's a term which has no gender
# indication
if not addedflag:
thistrans = {'remark': translationremark,
'trans': term.Term(isolang,
termweareworkingon,
number=number,
diminutive=diminutive)}
translations.append(thistrans)
if not isolang:
print ("This line doesn't seem to contain an indication of the "
"language: %s" % translationswikiline)
self.translations[isolang] = {'remark': translationsremark,
'alltrans': translations}
def hasSynonyms(self):
""" Returns True if there are synonyms else False """
return bool(self.synonyms)
def setTranslations(self, translations):
""" Provide the translations """
self.translations = translations
def getTranslations(self):
""" Returns the translations dictionary containing translation
Term objects for this meaning
"""
return self.translations
def addTranslation(self, translation):
""" Add a translation Term object to the dictionary for this meaning
The lang property of the Term object will be used as the key of the
dictionary
"""
self.translations.setdefault(translation.lang, []).append(translation)
def addTranslations(self, *translations):
""" This method calls addTranslation as often as necessary to add
all the translations it receives
"""
for translation in translations:
self.addTranslation(translation)
def hasTranslations(self):
""" Returns True if there are translations else False """
return bool(self.translations)
def setLabel(self, label):
self.label = label.replace('<!--', '').replace('-->', '')
def getLabel(self):
if self.label:
return u'<!--%s-->' % self.label
def setConciseDef(self, concisedef):
self.concisedef = concisedef
def getConciseDef(self):
if self.concisedef:
return self.concisedef
def getExamples(self):
""" Returns the list of example strings for this meaning
"""
return self.examples
def addExample(self, example):
""" Add a translation Term object to the dictionary for this meaning
The lang property of the Term object will be used as the key of the
dictionary
"""
self.examples.append(example)
def addExamples(self, *examples):
""" This method calls addExample as often as necessary to add
all the examples it receives
"""
for example in examples:
self.addExample(example)
def hasExamples(self):
""" Returns True if there are examples
Returns False if there are no examples
"""
if not self.examples:
return 0
else:
return 1
def wikiWrapSynonyms(self, wikilang):
""" Returns a string with all the synonyms in a format ready for
Wiktionary
"""
first = 1
wrappedsynonyms = ''
for synonym in self.synonyms:
if first == 0:
wrappedsynonyms += ', '
else:
first = 0
wrappedsynonyms += synonym.wikiWrapForList(
wikilang)
return wrappedsynonyms + '\n'
def wikiWrapTranslations(self, wikilang, entrylang):
""" Returns a string with all the translations in a format
ready for Wiktionary
The behavior changes with the circumstances.
For an entry in the same language as the Wiktionary the full list of
translations is contained in the output, excluding the local language
itself
- This list of translations has to end up in a table with two columns
- The first column of this table contains languages with names
from A to M, the second contains N to Z
- If a column in this list remains empty a html comment is put in that
column
For an entry in a foreign language only the translation towards the
local language is output.
"""
if wikilang == entrylang:
# When treating an entry of the same lang as the Wiktionary, we
# want to output the translations in such a way that they end up
# sorted alphabetically on the language name in the language of the
# current Wiktionary
alllanguages = self.translations.keys()
alllanguages.sort(sortonname(langnames[wikilang]))
wrappedtranslations = '%s\n' % (
structs.wiktionaryformats[wikilang]['transbefore'])
alreadydone = 0
for language in alllanguages:
if language == wikilang:
# don't output translation for the wikilang itself
continue
# split translations into two column table
if not alreadydone and \
langnames[wikilang][language][0:1].upper() > 'M':
wrappedtranslations += structs.wiktionaryformats[
wikilang]['transinbetween'] + '\n'
alreadydone = 1
# Indicating the language according to the wikiformats
# dictionary
wrappedtranslations += structs.wiktionaryformats[
wikilang]['translang'].replace(
'%%langname%%',
langnames[wikilang][language]).replace(
'%%ISOLangcode%%', language) + ': '
first = 1
for translation in self.translations[language]:
termweareworkingon = translation.term
if first == 0:
wrappedtranslations += ', '
else:
first = 0
wrappedtranslations += translation.wikiWrapAsTranslation(
wikilang)
wrappedtranslations += '\n'
if not alreadydone:
wrappedtranslations += structs.wiktionaryformats[
wikilang]['transinbetween'] + '\n' + \
structs.wiktionaryformats[wikilang]['transnoNtoZ'] + '\n'
alreadydone = 1
wrappedtranslations += structs.wiktionaryformats[
wikilang]['transafter'] + '\n'
else:
# For the other entries we want to output the translation in the
# language of the Wiktionary
wrappedtranslations = structs.wiktionaryformats[
wikilang]['translang'].replace('%%langname%%',
langnames[
wikilang][wikilang]).replace(
'%%ISOLangcode%%',
wikilang) + ': '
first = True
for translation in self.translations[wikilang]:
termweareworkingon = translation.term
if not first:
wrappedtranslations += ', '
else:
first = False
wrappedtranslations += translation.wikiWrapAsTranslation(
wikilang)
return wrappedtranslations
def showContents(self, indentation):
""" Prints the contents of this meaning.
Every subobject is indented a little further on the screen.
The primary purpose is to help keep one's sanity while debugging.
"""
print ' ' * indentation + 'term: '
self.term.showContents(indentation + 2)
print ' ' * indentation + 'definition = %s' % self.definition
print ' ' * indentation + 'etymology = %s' % self.etymology
print ' ' * indentation + 'Synonyms:'
for synonym in self.synonyms:
synonym.showContents(indentation + 2)
print ' ' * indentation + 'Translations:'
translationkeys = self.translations.keys()
for translationkey in translationkeys:
for translation in self.translations[translationkey]:
translation.showContents(indentation + 2)
def wikiWrapExamples(self):
""" Returns a string with all the examples in a format ready for
Wiktionary
"""
wrappedexamples = ''
for example in self.examples:
wrappedexamples += "#:'''%s'''\n" % example
return wrappedexamples