Python LMF library
 All Classes Namespaces Files Functions Variables
lemma.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """! @package morphology
4 """
5 
6 from core.form import Form
7 from core.form_representation import FormRepresentation
8 
9 class Lemma(Form):
10  """! "Lemma is a Form subclass representing a form chosen by convention to designate the Lexical Entry. The lemma is usually equivalent to one of the inflected forms, the root, stem or compound phrase." (LMF).
11  """
12  def __init__(self):
13  """! @brief Constructor.
14  Lemma instance is owned by LexicalEntry.
15  @return A Lemma instance.
16  """
17  # Initialize Form attribute 'form_representation'
18  self.__new__()
19  self.hyphenation = None
20  self.lexeme = None
21 
22  def __del__(self):
23  """! @brief Destructor.
24  """
25  pass
26 
27  def set_lexeme(self, lexeme):
28  """! @brief Set lexeme.
29  @param lexeme The lexeme to set.
30  @return Lemma instance.
31  """
32  self.lexeme = lexeme
33  return self
34 
35  def get_lexeme(self):
36  """! @brief Get lexeme.
37  @return Lemma attribute 'lexeme'.
38  """
39  return self.lexeme
40 
42  """! @brief Create a form representation.
43  @return FormRepresentation instance.
44  """
45  return FormRepresentation()
46 
47  def add_form_representation(self, form_representation):
48  """! @brief Add a form representation to the lemma.
49  @param form_representation The FormRepresentation instance to add to the lemma.
50  @return Lemma instance.
51  """
52  self.form_representation.append(form_representation)
53  return self
54 
55  def find_form_representations(self, type):
56  """! @brief Find variant forms.
57  This attribute is owned by FormRepresentation.
58  @param type The type to consider to retrieve the variant form.
59  @return A Python list of found FormRepresentation attributes 'variantForm'.
60  """
61  found_forms = []
62  for form_representation in self.get_form_representations():
63  if form_representation.get_type() == type and form_representation.get_variantForm() is not None:
64  found_forms.append(form_representation.get_variantForm())
65  return found_forms
66 
68  """! @brief Get all form representations maintained by the lemma.
69  @return A Python list of form representations.
70  """
71  return self.form_representation
72 
73  def get_form_representation(self, index):
74  """! @brief Get a given form representation maintained by the lemma.
75  @param index The index of the wanted form representation.
76  @return The wanted FormRepresentation instance.
77  """
78  try:
79  return self.form_representation[index]
80  except IndexError:
81  raise
82 
83  def set_variant_form(self, variant_form, type="unspecified"):
84  """! @brief Set variant form and type.
85  These attributes are owned by FormRepresentation.
86  @param variant_form Variant form.
87  @param type Type of variant, in range 'type_variant_range' defined in 'common/range.py'.
88  @return Lemma instance.
89  """
90  form_representation = None
91  # Set first FormRepresentation instance that has no variant form nor type
92  for repr in self.get_form_representations():
93  if repr.get_variantForm() is None and repr.get_type() is None:
94  form_representation = repr
95  break
96  if form_representation is None:
97  # Create a FormRepresentation instance
98  form_representation = self.create_form_representation()
99  self.add_form_representation(form_representation)
100  form_representation.set_variantForm(variant_form).set_type(type)
101  return self
102 
103  def get_variant_forms(self, type="unspecified"):
104  """! @brief Get all variant forms of specified type.
105  This attribute is owned by FormRepresentation.
106  @return A Python list of FormRepresentation attributes 'variantForm' if type matches.
107  """
108  variant_forms = []
109  for repr in self.get_form_representations():
110  if repr.get_type() == type and repr.get_variantForm() is not None:
111  variant_forms.append(repr.get_variantForm())
112  return variant_forms
113 
114  def set_variant_comment(self, comment, language=None):
115  """! @brief Set variant comment and language.
116  These attributes are owned by FormRepresentation.
117  @param comment Variant comment.
118  @param language Language of comment.
119  @return Lemma instance.
120  """
121  form_representation = None
122  # Set first FormRepresentation instance that has no comment nor language
123  for repr in self.get_form_representations():
124  if repr.get_comment() is None and repr.get_language() is None:
125  form_representation = repr
126  break
127  if form_representation is None:
128  # Create a FormRepresentation instance
129  form_representation = self.create_form_representation()
130  self.add_form_representation(form_representation)
131  form_representation.set_comment(comment, language)
132  return self
133 
134  def set_tone(self, tone):
135  """! @brief Set tone.
136  This attribute is owned by FormRepresentation.
137  @param tone The tone to set.
138  @return Lemma instance.
139  """
140  form_representation = None
141  # Set first FormRepresentation instance that has no tone
142  for repr in self.get_form_representations():
143  if repr.get_tone() is None:
144  form_representation = repr
145  break
146  if form_representation is None:
147  # Create a FormRepresentation instance
148  form_representation = self.create_form_representation()
149  self.add_form_representation(form_representation)
150  form_representation.set_tone(tone)
151  return self
152 
153  def get_tones(self):
154  """! @brief Get all tones.
155  This attribute is owned by FormRepresentation.
156  @return A Python list of FormRepresentation attributes 'tone'.
157  """
158  tones = []
159  for repr in self.get_form_representations():
160  if repr.get_tone() is not None:
161  tones.append(repr.get_tone())
162  return tones
163 
164  def set_geographical_variant(self, geographical_variant):
165  """! @brief Set geographical variant.
166  This attribute is owned by FormRepresentation.
167  @param geographical_variant The geographical variant to set.
168  @return Lemma instance.
169  """
170  form_representation = None
171  # Set first FormRepresentation instance that has no geographical variant
172  for repr in self.get_form_representations():
173  if repr.get_geographicalVariant() is None:
174  form_representation = repr
175  break
176  if form_representation is None:
177  # Create a FormRepresentation instance
178  form_representation = self.create_form_representation()
179  self.add_form_representation(form_representation)
180  form_representation.set_geographicalVariant(geographical_variant)
181  return self
182 
183  def set_phonetic_form(self, phonetic_form, script_name=None):
184  """! @brief Set phonetic form.
185  This attribute is owned by FormRepresentation.
186  @param phonetic_form The phonetic form to set.
187  @param script_name The name of the script used to write the phonetic form, e.g. pinyin.
188  @return Lemma instance.
189  """
190  form_representation = None
191  # Set first FormRepresentation instance that has no phonetic form and that uses the same script name
192  for repr in self.get_form_representations():
193  if repr.get_phoneticForm() is None and repr.get_scriptName() == script_name:
194  form_representation = repr
195  break
196  if form_representation is None:
197  # Create a FormRepresentation instance
198  form_representation = self.create_form_representation()
199  self.add_form_representation(form_representation)
200  form_representation.set_phoneticForm(phonetic_form)
201  if script_name is not None:
202  form_representation.set_scriptName(script_name)
203  return self
204 
205  def get_phonetic_forms(self, script_name=None):
206  """! @brief Get all phonetic forms.
207  This attribute is owned by FormRepresentation.
208  @param script_name If provided, get only phonetic forms that are written using this script.
209  @return A Python list of FormRepresentation attributes 'phoneticForm'.
210  """
211  phonetic_forms = []
212  for repr in self.get_form_representations():
213  if repr.get_phoneticForm() is not None and (script_name is None or repr.get_scriptName() == script_name):
214  phonetic_forms.append(repr.get_phoneticForm())
215  return phonetic_forms
216 
217  def set_contextual_variation(self, contextual_variation):
218  """! @brief Set contextual variation.
219  This attribute is owned by FormRepresentation.
220  @param contextual_variation The contextual variation to set.
221  @return Lemma instance.
222  """
223  form_representation = None
224  # Set first FormRepresentation instance that has no contextual variation
225  for repr in self.get_form_representations():
226  if repr.get_contextualVariation() is None:
227  form_representation = repr
228  break
229  if form_representation is None:
230  # Create a FormRepresentation instance
231  form_representation = self.create_form_representation()
232  self.add_form_representation(form_representation)
233  form_representation.set_contextualVariation(contextual_variation)
234  return self
235 
237  """! @brief Get all contextual variations.
238  This attribute is owned by FormRepresentation.
239  @return A Python list of FormRepresentation attributes 'contextualVariation'.
240  """
241  contextual_variations = []
242  for repr in self.get_form_representations():
243  if repr.get_contextualVariation() is not None:
244  contextual_variations.append(repr.get_contextualVariation())
245  return contextual_variations
246 
247  def set_spelling_variant(self, spelling_variant):
248  """! @brief Set spelling variant.
249  This attribute is owned by FormRepresentation.
250  @param spelling_variant The spelling variant to set.
251  @return Lemma instance.
252  """
253  form_representation = None
254  # Set first FormRepresentation instance that has no spelling variant
255  for repr in self.get_form_representations():
256  if repr.get_spellingVariant() is None:
257  form_representation = repr
258  break
259  if form_representation is None:
260  # Create a FormRepresentation instance
261  form_representation = self.create_form_representation()
262  self.add_form_representation(form_representation)
263  form_representation.set_spellingVariant(spelling_variant)
264  return self
265 
267  """! @brief Get all spelling variants.
268  This attribute is owned by FormRepresentation.
269  @return A Python list of FormRepresentation attributes 'spellingVariant'.
270  """
271  spelling_variants = []
272  for repr in self.get_form_representations():
273  if repr.get_spellingVariant() is not None:
274  spelling_variants.append(repr.get_spellingVariant())
275  return spelling_variants
276 
277  def set_citation_form(self, citation_form, script_name=None):
278  """! @brief Set citation form.
279  This attribute is owned by FormRepresentation.
280  @param citation_form The citation form to set.
281  @param script_name The name of the script used to write the citation form, e.g. devanagari.
282  @return Lemma instance.
283  """
284  form_representation = None
285  # Set first FormRepresentation instance that has no citation form and that uses the same script name
286  for repr in self.get_form_representations():
287  if repr.get_citationForm() is None and repr.get_scriptName() == script_name:
288  form_representation = repr
289  break
290  if form_representation is None:
291  # Create a FormRepresentation instance
292  form_representation = self.create_form_representation()
293  self.add_form_representation(form_representation)
294  form_representation.set_citationForm(citation_form)
295  if script_name is not None:
296  form_representation.set_scriptName(script_name)
297  return self
298 
299  def get_citation_forms(self, script_name=None):
300  """! @brief Get all citation forms.
301  This attribute is owned by FormRepresentation.
302  @param script_name If provided, get only citation forms that are written using this script.
303  @return A Python list of FormRepresentation attributes 'citationForm'.
304  """
305  citation_forms = []
306  for repr in self.get_form_representations():
307  if repr.get_citationForm() is not None and (script_name is None or repr.get_scriptName() == script_name):
308  citation_forms.append(repr.get_citationForm())
309  return citation_forms
310 
311  def set_dialect(self, dialect):
312  """! @brief Set dialect.
313  This attribute is owned by FormRepresentation.
314  @param dialect The dialect to set.
315  @return Lemma instance.
316  """
317  form_representation = None
318  # Set first FormRepresentation instance that has no dialect
319  for repr in self.get_form_representations():
320  if repr.get_dialect() is None:
321  form_representation = repr
322  break
323  if form_representation is None:
324  # Create a FormRepresentation instance
325  form_representation = self.create_form_representation()
326  self.add_form_representation(form_representation)
327  form_representation.set_dialect(dialect)
328  return self
329 
330  def set_transliteration(self, transliteration):
331  """! @brief Set transliteration.
332  This attribute is owned by FormRepresentation.
333  @param transliteration The transliteration to set.
334  @return Lemma instance.
335  """
336  form_representation = None
337  # Set first FormRepresentation instance that has no dialect
338  for repr in self.get_form_representations():
339  if repr.get_transliteration() is None:
340  form_representation = repr
341  break
342  if form_representation is None:
343  # Create a FormRepresentation instance
344  form_representation = self.create_form_representation()
345  self.add_form_representation(form_representation)
346  form_representation.set_transliteration(transliteration)
347  return self
348 
350  """! @brief Get all transliterations.
351  This attribute is owned by FormRepresentation.
352  @return A Python list of FormRepresentation attributes 'transliteration'.
353  """
354  transliterations = []
355  for repr in self.get_form_representations():
356  if repr.get_transliteration() is not None:
357  transliterations.append(repr.get_transliteration())
358  return transliterations
359 
360  def set_script_name(self, script_name):
361  """! @brief Set script name.
362  This attribute is owned by FormRepresentation.
363  @param script_name The script name to set.
364  @return Lemma instance.
365  """
366  form_representation = None
367  # Set first FormRepresentation instance that has no dialect
368  for repr in self.get_form_representations():
369  if repr.get_scriptName() is None:
370  form_representation = repr
371  break
372  if form_representation is None:
373  # Create a FormRepresentation instance
374  form_representation = self.create_form_representation()
375  self.add_form_representation(form_representation)
376  form_representation.set_scriptName(script_name)
377  return self
378 
379  def set_audio(self, media_type, file_name, author, quality, start_position, duration, external_reference, audio_file_format):
380  """! @brief Set audio resource.
381  Attributes 'mediaType', 'fileName', 'author', 'quality', 'startPosition', 'durationOfEffectiveSpeech', 'externalReference', 'audioFileFormat' are owned by Material/Audio, which is owned by FormRepresentation.
382  @param media_type The media type to set.
383  @param file_name Name of the audio file.
384  @param author Author of the recording.
385  @param quality Quality of the recording, in range 'quality_range' defined in 'common/range.py'.
386  @param start_position Start position of the form in the recording, in format 'Thh:mm:ss,msms', e.g. "T00:05:00".
387  @param duration Duration of the effcetive speech, in format 'PThhHmmMssS', e.g. "PT00:05:00".
388  @param external_reference Reference of the audio file, if not directly provided.
389  @param audio_file_format Format of the audio file, e.g. "wav".
390  @return Lemma instance.
391  """
392  # Create a FormRepresentation instance
393  form_representation = self.create_form_representation()
394  self.add_form_representation(form_representation)
395  form_representation.set_audio(media_type, file_name, author, quality, start_position, duration, external_reference, audio_file_format)
396  return self
def create_form_representation
Create a form representation.
Definition: lemma.py:41
def get_variant_forms
Get all variant forms of specified type.
Definition: lemma.py:103
def find_form_representations
Find variant forms.
Definition: lemma.py:55
def get_spelling_variants
Get all spelling variants.
Definition: lemma.py:266
def set_audio
Set audio resource.
Definition: lemma.py:379
def set_variant_form
Set variant form and type.
Definition: lemma.py:83
def set_geographical_variant
Set geographical variant.
Definition: lemma.py:164
def get_contextual_variations
Get all contextual variations.
Definition: lemma.py:236
def get_form_representation
Get a given form representation maintained by the lemma.
Definition: lemma.py:73
def set_phonetic_form
Set phonetic form.
Definition: lemma.py:183
def get_citation_forms
Get all citation forms.
Definition: lemma.py:299
def set_script_name
Set script name.
Definition: lemma.py:360
def set_spelling_variant
Set spelling variant.
Definition: lemma.py:247
def set_contextual_variation
Set contextual variation.
Definition: lemma.py:217
def add_form_representation
Add a form representation to the lemma.
Definition: lemma.py:47
def get_form_representations
Get all form representations maintained by the lemma.
Definition: lemma.py:67
"Lemma is a Form subclass representing a form chosen by convention to designate the Lexical Entry...
Definition: lemma.py:9
def set_citation_form
Set citation form.
Definition: lemma.py:277
def get_phonetic_forms
Get all phonetic forms.
Definition: lemma.py:205
def get_transliterations
Get all transliterations.
Definition: lemma.py:349
def set_variant_comment
Set variant comment and language.
Definition: lemma.py:114
def set_transliteration
Set transliteration.
Definition: lemma.py:330