-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.py
117 lines (96 loc) · 4.32 KB
/
entry.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
'''
import meaning
import structs
class Entry:
""" This class contains the entries that belong together on one page.
On Wiktionaries that are still on first character capitalization, this
means both [[Kind]] and [[kind]].
Terms in different languages can be described. Usually there is one entry
for each language.
"""
def __init__(self, entrylang, meaning=""):
""" Constructor
Called with one parameter:
- the language of this entry
and can optionally be initialized with a first meaning
"""
self.entrylang = entrylang
# a dictionary containing the meanings for this term grouped by part of
# speech:
self.meanings = {}
if meaning:
self.addMeaning(meaning)
# we don't want to shuffle the order of the parts of speech, so we keep
# a list to keep the order in which they were encountered:
self.posorder = []
def addMeaning(self, meaning):
""" Lets you add another meaning to this entry """
# fetch the term, in order to be able to determine its part of speech
# in the next step
term = meaning.term
self.meanings.setdefault(term.pos, []).append(meaning)
# we only need each part of speech once in our list where we keep track
# of the order
if term.pos not in self.posorder:
self.posorder.append(term.pos)
def getMeanings(self):
""" Returns a dictionary containing all the meaning objects for this
entry
"""
return self.meanings
def wikiWrap(self, wikilang):
""" Returns a string for this entry in a format ready for Wiktionary
"""
entry = structs.wiktionaryformats[wikilang]['langheader'].replace(
'%%langname%%', langnames[wikilang][self.entrylang]).replace(
'%%ISOLangcode%%', self.entrylang) + '\n'
for pos in self.posorder:
meanings = self.meanings[pos]
entry += structs.wiktionaryformats[wikilang]['posheader'][pos]
entry += '\n'
if wikilang == 'en':
entry += meanings[0].term.wikiWrapAsExample(wikilang) + '\n\n'
for meaning in meanings:
entry += '#%s %s\n' % (meaning.getLabel(),
meaning.definition)
entry += meaning.wikiWrapExamples()
entry += '\n'
if wikilang == 'nl':
for meaning in meanings:
term = meaning.term
entry += meaning.getLabel() + term.wikiWrapAsExample(
wikilang) + '; %s\n' % meaning.definition
entry += meaning.wikiWrapExamples()
entry += '\n'
if meaning.hasSynonyms():
entry += '%s\n' % (
structs.wiktionaryformats[wikilang]['synonymsheader'])
for meaning in meanings:
entry += "*%s'''%s''': %s" % (meaning.getLabel(),
meaning.getConciseDef(),
meaning.wikiWrapSynonyms(
wikilang))
entry += '\n'
if meaning.hasTranslations():
entry += '%s\n' % (
structs.wiktionaryformats[wikilang]['translationsheader'])
for meaning in meanings:
entry += "%s'''%s'''\n%s\n\n" % (
meaning.getLabel(), meaning.getConciseDef(),
meaning.wikiWrapTranslations(wikilang, self.entrylang))
entry += '\n'
return entry
def showContents(self, indentation):
""" Prints the contents of all the subobjects contained in this entry.
Every subobject is indented a little further on the screen.
The primary purpose is to help keep your sanity while debugging.
"""
print ' ' * indentation + 'entrylang = %s' % self.entrylang
print ' ' * indentation + 'posorder:' + repr(self.posorder)
meaningkeys = self.meanings.keys()
for meaningkey in meaningkeys:
for meaning in self.meanings[meaningkey]:
meaning.showContents(indentation + 2)