Python LMF library
 All Classes Namespaces Files Functions Variables
context.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """! @package mrd
4 """
5 
6 from common.range import type_example_range
7 from utils.attr import check_attr_type, check_attr_range
8 from core.text_representation import TextRepresentation
9 
10 class Context():
11  """! "Context is a class representing a text string that provides authentic context for the use of the word form managed by the Lemma. This class is to be distinguished from Sense Example." (LMF)
12  """
13  def __init__(self, speakerID=None):
14  """! @brief Constructor.
15  Context instances are owned by Sense.
16  @param speakerID Related speaker identifier. If not provided, default value is None.
17  @return A Context instance.
18  """
19  self.language = None
20  self.type = None
21  ## TextRepresentation instances are owned by Context
22  # There is zero to many TextRepresentation instances per Context
24  # Speaker id
25  self.targets = speakerID
26  ## Pointer to an existing Speaker
27  # There is zero or one Speaker pointer per Context instance
28  self.__speaker = None
29 
30  def __del__(self):
31  """! @brief Destructor.
32  Release TextRepresentation instances.
33  """
34  for text_representation in self.text_representation:
35  del text_representation
36  del self.text_representation[:]
37  # Decrement the reference count on pointed objects
38  self.__speaker = None
39 
40  def set_type(self, type):
41  """! @brief Set context type.
42  @param type Type of text representations, in range 'type_example_range' defined in 'common/range.py'.
43  @return Context instance.
44  """
45  error_msg = "Context type value '%s' is not allowed" % type
46  value = None
47  if type is not None:
48  check_attr_type(type, [str, unicode], error_msg)
49  value = check_attr_range(type, type_example_range, error_msg)
50  self.type = value
51  return self
52 
53  def get_type(self):
54  """! @brief Get context type.
55  @return Context attribute 'type'.
56  """
57  return self.type
58 
60  """! @brief Create a text representation.
61  @return TextRepresentation instance.
62  """
63  return TextRepresentation()
64 
65  def add_text_representation(self, text_representation):
66  """! @brief Add a text representation to the context.
67  @param text_representation The TextRepresentation instance to add to the context.
68  @return Context instance.
69  """
70  self.text_representation.append(text_representation)
71  return self
72 
74  """! @brief Get all text representations maintained by the context.
75  @return A Python list of text representations.
76  """
77  return self.text_representation
78 
80  """! @brief Get the previously registered TextRepresentation instance.
81  @return The last element of Context attribute 'text_representation'.
82  """
83  if len(self.get_text_representations()) >= 1:
84  return self.get_text_representations()[-1]
85 
86  def find_written_forms(self, language=None, script_name=None):
87  """! @brief Find written forms.
88  This attribute is owned by TextRepresentation.
89  @param language If given, the language to consider to retrieve the written form.
90  @param script_name If given, the script to consider to retrieve the written form.
91  @return A Python list of found TextRepresentation attributes 'writtenForm'.
92  """
93  found_forms = []
94  for repr in self.get_text_representations():
95  if (language is None or repr.get_language() == language) and repr.get_writtenForm() is not None \
96  and (script_name is None or repr.get_scriptName() == script_name):
97  found_forms.append(repr.get_writtenForm())
98  return found_forms
99 
100  def get_comments(self):
101  """! @brief Get comments.
102  This attribute is owned by TextRepresentation.
103  @return A Python list of found TextRepresentation attributes 'comment'.
104  """
105  found_comments = []
106  for repr in self.get_text_representations():
107  if repr.get_comment() is not None:
108  found_comments.append(repr.get_comment())
109  return found_comments
110 
111  def set_written_form(self, written_form, language=None, script_name=None):
112  """! @brief Set text representation written form, language and script.
113  Attributes 'writtenForm', 'language' and 'scriptName' are owned by TextRepresentation.
114  @param written_form The written form to set.
115  @param language Language of the written form.
116  @param script_name The name of the script used to write the form, e.g. devanagari.
117  @return Context instance.
118  """
119  # Create a TextRepresentation instance, set it, and add it to the list
120  repr = self.create_text_representation().set_writtenForm(written_form)
121  if language is not None:
122  repr.set_language(language)
123  if script_name is not None:
124  repr.set_scriptName(script_name)
125  self.add_text_representation(repr)
126  return self
127 
128  def set_comment(self, comment):
129  """! @brief Set text representation comment.
130  Attribute 'comment' is owned by TextRepresentation.
131  @param comment The comment to set.
132  @return Context instance.
133  """
134  # Retrieve the previsouly created text representation
135  repr = self.get_last_text_representation()
136  # Check if there is a comment already
137  if repr is None or repr.get_comment() is not None:
138  # Create a TextRepresentation instance and add it to the list
139  repr = self.create_text_representation()
140  self.add_text_representation(repr)
141  repr.set_comment(comment)
142  return self
143 
144  def get_speakerID(self):
145  """! @brief Get related speaker identifier.
146  @return Context attribute 'targets'.
147  """
148  return self.targets
149 
150  def get_speaker(self):
151  """! @brief Get speaker.
152  @return Context private attribute '__speaker'.
153  """
154  return self.__speaker
def check_attr_type
Check that attribute value is of specified type.
Definition: attr.py:9
def set_type
Set context type.
Definition: context.py:40
def get_text_representations
Get all text representations maintained by the context.
Definition: context.py:73
def create_text_representation
Create a text representation.
Definition: context.py:59
def check_attr_range
Check that attribute value is in specified range.
Definition: attr.py:23
__speaker
Pointer to an existing Speaker There is zero or one Speaker pointer per Context instance.
Definition: context.py:28
def set_written_form
Set text representation written form, language and script.
Definition: context.py:111
def set_comment
Set text representation comment.
Definition: context.py:128
def get_type
Get context type.
Definition: context.py:53
def get_speakerID
Get related speaker identifier.
Definition: context.py:144
def add_text_representation
Add a text representation to the context.
Definition: context.py:65
def find_written_forms
Find written forms.
Definition: context.py:86
text_representation
TextRepresentation instances are owned by Context There is zero to many TextRepresentation instances ...
Definition: context.py:23
def get_last_text_representation
Get the previously registered TextRepresentation instance.
Definition: context.py:79
"Context is a class representing a text string that provides authentic context for the use of the wor...
Definition: context.py:10