Python LMF library
 All Classes Namespaces Files Functions Variables
related_form.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 from common.range import semanticRelation_range
9 from utils.attr import check_attr_type, check_attr_range
10 
11 class RelatedForm(Form):
12  """! "Related Form is a Form subclass representing a word form or a morph that can be related to the Lexical Entry. There is no asumption that the Related Form is associated with the Sense class in the Lexical Entry." (LMF)
13  """
14  def __init__(self, lexeme=None):
15  """! @brief Constructor.
16  RelatedForm instances are owned by LexicalEntry.
17  @param lexeme Related lexeme. If not provided, default value is None.
18  @return A RelatedForm instance.
19  """
20  # Initialize Form attribute 'form_representation'
21  self.__new__()
22  self.semanticRelation = None
23  # Related LexicalEntry lexeme
24  self.targets = lexeme
25  ## Pointer to an existing LexicalEntry
26  # There is one LexicalEntry pointer per RelatedForm instance
27  self.__lexical_entry = None
28 
29  def __del__(self):
30  """! @brief Destructor.
31  """
32  # Decrement the reference count on pointed objects
33  self.__lexical_entry = None
34 
35  def set_semanticRelation(self, semantic_relation):
36  """! @brief Set semantic relation.
37  @param semantic_relation The semantic relation to set.
38  @return RelatedForm instance.
39  """
40  error_msg = "Semantic relation value '%s' is not allowed" % semantic_relation
41  # Check semantic relation type
42  check_attr_type(semantic_relation, [str, unicode], error_msg)
43  # Check range of semantic relation
44  value = check_attr_range(semantic_relation, semanticRelation_range, error_msg)
45  self.semanticRelation = value
46  return self
47 
49  """! @brief Get semantic relation.
50  @return RelatedForm attribute 'semanticRelation'.
51  """
52  return self.semanticRelation
53 
54  def get_lexeme(self):
55  """! @brief Get related LexicalEntry lexeme.
56  @return RelatedForm attribute 'targets'.
57  """
58  return self.targets
59 
60  def set_lexical_entry(self, lexical_entry):
61  """! @brief Set pointer to the related lexical entry instance. This function can only be called once the full dictionary has been parsed.
62  @param lexical_entry The related LexicalEntry.
63  @return RelatedForm instance.
64  """
65  self.__lexical_entry = lexical_entry
66  return self
67 
68  def get_lexical_entry(self):
69  """! @brief Get related LexicalEntry.
70  @return RelatedForm private attribute '__lexical_entry'.
71  """
72  return self.__lexical_entry
73 
74  def create_and_add_form_representation(self, written_form=None, language=None):
75  """! @brief Create and add a form representation to the related form.
76  @param written_form The written form to set.
77  @param language Language used for the written form.
78  @return RelatedForm instance.
79  """
80  self.form_representation.append(FormRepresentation().set_writtenForm(written_form).set_language(language))
81  return self
82 
83  def find_written_forms(self, language):
84  """! @brief Find written forms.
85  This attribite is owned by FormRepresentation.
86  @param language The language to consider to retrieve the written forms.
87  @return A Python list of found FormRepresentation attributes 'writtenForm'.
88  """
89  found_written_forms = []
90  for form_representation in self.form_representation:
91  if form_representation.get_language() == language:
92  found_written_forms.append(form_representation.get_writtenForm())
93  return found_written_forms
def check_attr_type
Check that attribute value is of specified type.
Definition: attr.py:9
def check_attr_range
Check that attribute value is in specified range.
Definition: attr.py:23