Python LMF library
 All Classes Namespaces Files Functions Variables
form_representation.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """! @package core
4 """
5 
6 from core.representation import Representation
7 from resources.audio import Audio
8 from utils.attr import check_attr_type, check_attr_range
9 from common.range import type_variant_range
10 
11 class FormRepresentation(Representation):
12  """! "Form Representation is a class representing one variant orthography of a Form." (LMF)
13  """
14  def __init__(self):
15  """! @brief Constructor.
16  FormRepresentation instances are owned by Form.
17  @return A FormRepresentation instance.
18  """
19  # Initialize Representation attributes: 'comment', 'writtenForm', 'language' and 'scriptName'
20  self.__new__()
21  self.variantForm = None
22  self.type = None
23  self.transliteration = None
24  self.tone = None
25  self.geographicalVariant = None
26  self.phoneticForm = None
27  self.contextualVariation = None
28  self.spellingVariant = None
29  self.citationForm = None
30  self.dialect = None
31  ## Audio instance is owned by FormRepresentation
32  # There is zero or one Audio instance per FormRepresentation
33  self.audio = None
34  # Speaker id
35  self.targets = None
36  ## Pointers to an existing Speaker
37  # There is zero to many pointers per FormRepresentation instance
38  self.__speaker = []
39 
40  def __del__(self):
41  """! @brief Destructor.
42  Release Audio instances.
43  """
44  if self.audio is not None:
45  del self.audio
46  # Decrement the reference count on pointed objects
47  del self.__speaker[:]
48 
49  def get_speakers(self):
50  """! @brief Get speakers.
51  @return FormRepresentation private attribute '__speaker', a Python list of Speaker instances.
52  """
53  return self.__speaker
54 
55  def set_writtenForm(self, written_form, script_name=None):
56  """! @brief Set written form and script.
57  @param written_form The written form to set.
58  @param script_name Script used for the written form.
59  @return FormRepresentation instance.
60  """
61  error_msg = "Written form value '%s' is not allowed" % written_form
62  check_attr_type(written_form, [str, unicode], error_msg)
63  self.writtenForm = written_form
64  if script_name is not None:
65  self.set_scriptName(script_name)
66  return self
67 
68  def get_writtenForm(self, script_name=None):
69  """! @brief Get written form.
70  @param script_name If this argument is given, get written form only if written using this script.
71  @return The filtered Representation attribute 'writtenForm'.
72  """
73  if script_name is None or script_name == self.get_scriptName():
74  return self.writtenForm
75 
76  def set_variantForm(self, variant_form):
77  """! @brief Set variant form.
78  @param variant_form The variant form to set.
79  @return FormRepresentation instance.
80  """
81  error_msg = "Variant form value '%s' is not allowed" % variant_form
82  check_attr_type(variant_form, [str, unicode], error_msg)
83  self.variantForm = variant_form
84  return self
85 
86  def get_variantForm(self):
87  """! @brief Get variant form.
88  @return FormRepresentation attribute 'variantForm'.
89  """
90  return self.variantForm
91 
92  def set_type(self, type):
93  """! @brief Set variant type.
94  @param type Type of variant, in range 'type_variant_range' defined in 'common/range.py'.
95  @return FormRepresentation instance.
96  """
97  error_msg = "Variant type value '%s' is not allowed" % type
98  value = None
99  if type is not None:
100  # Check value
101  check_attr_type(type, [str, unicode], error_msg)
102  value = check_attr_range(type, type_variant_range, error_msg)
103  self.type = value
104  return self
105 
106  def get_type(self):
107  """! @brief Get variant type.
108  @return FormRepresentation attribute 'type'.
109  """
110  return self.type
111 
112  def set_comment(self, comment, language=None):
113  """! @brief Set variant form comment.
114  @param comment Comment about the variant form.
115  @param language Language used for the comment.
116  @return FormRepresentation instance.
117  """
118  error_msg = "Variant form comment value '%s' is not allowed" % comment
119  # Check attribute type
120  check_attr_type(comment, [str, unicode], error_msg)
121  self.comment = comment
122  if language is not None:
123  self.set_language(language)
124  return self
125 
126  def get_comment(self, language=None):
127  """! @brief Get variant form comment.
128  @param language If this argument is given, get comment only if written in this language.
129  @return The filtered Representation attribute 'comment'.
130  """
131  if language is None:
132  return self.comment
133  if self.get_language() == language:
134  return self.comment
135 
136  def set_language(self, language):
137  """! @brief Set language used for comment.
138  @param language Language used for the comment.
139  @return FormRepresentation instance.
140  """
141  error_msg = "Language value '%s' is not allowed" % language
142  # Check value
143  check_attr_type(language, [str, unicode], error_msg)
144  self.language = language
145  return self
146 
147  def get_language(self):
148  """! @brief Get language used for comment.
149  @return Representation attribute 'language'.
150  """
151  return self.language
152 
153  def set_tone(self, tone):
154  """! @brief Set tone.
155  @param tone The tone to set.
156  @return FormRepresentation instance.
157  """
158  error_msg = "Tone value '%s' is not allowed" % tone
159  check_attr_type(tone, [str, unicode], error_msg)
160  self.tone = tone
161  return self
162 
163  def get_tone(self):
164  """! @brief Get tone.
165  @return FormRepresentation attribute 'tone'.
166  """
167  return self.tone
168 
169  def set_geographicalVariant(self, geographical_variant):
170  """! @brief Set geographical variant.
171  @param geographical_variant The geographical variant to set.
172  @return FormRepresentation instance.
173  """
174  error_msg = "Geographical variant value '%s' is not allowed" % geographical_variant
175  check_attr_type(geographical_variant, [str, unicode], error_msg)
176  self.geographicalVariant = geographical_variant
177  return self
178 
180  """! @brief Get geographical variant.
181  @return FormRepresentation attribute 'geographicalVariant'.
182  """
183  return self.geographicalVariant
184 
185  def set_phoneticForm(self, phonetic_form):
186  """! @brief Set phonetic form.
187  @param phonetic_form The phonetic form to set.
188  @return FormRepresentation instance.
189  """
190  error_msg = "Phonetic form value '%s' is not allowed" % phonetic_form
191  check_attr_type(phonetic_form, [str, unicode], error_msg)
192  self.phoneticForm = phonetic_form
193  return self
194 
195  def get_phoneticForm(self):
196  """! @brief Get phonetic form.
197  @return FormRepresentation attribute 'phoneticForm'.
198  """
199  return self.phoneticForm
200 
201  def set_contextualVariation(self, contextual_variation):
202  """! @brief Set contextual variation.
203  @param contextualVariation The contextual variation to set.
204  @return FormRepresentation instance.
205  """
206  error_msg = "Contextual variation value '%s' is not allowed" % contextual_variation
207  check_attr_type(contextual_variation, [str, unicode], error_msg)
208  self.contextualVariation = contextual_variation
209  return self
210 
212  """! @brief Get contextual variation.
213  @return FormRepresentation attribute 'contextualVariation'.
214  """
215  return self.contextualVariation
216 
217  def set_spellingVariant(self, spelling_variant):
218  """! @brief Set spelling variant.
219  @param spelling_variant The spelling variant to set.
220  @return FormRepresentation instance.
221  """
222  error_msg = "Spelling variant value '%s' is not allowed" % spelling_variant
223  check_attr_type(spelling_variant, [str, unicode], error_msg)
224  self.spellingVariant = spelling_variant
225  return self
226 
228  """! @brief Get spelling variant.
229  @return FormRepresentation attribute 'spellingVariant'.
230  """
231  return self.spellingVariant
232 
233  def set_citationForm(self, citation_form):
234  """! @brief Set citation form.
235  @param citation_form The citation form to set.
236  @return FormRepresentation instance.
237  """
238  error_msg = "Citation form value '%s' is not allowed" % citation_form
239  check_attr_type(citation_form, [str, unicode], error_msg)
240  self.citationForm = citation_form
241  return self
242 
243  def get_citationForm(self):
244  """! @brief Get citation form.
245  @return FormRepresentation attribute 'citationForm'.
246  """
247  return self.citationForm
248 
249  def set_dialect(self, dialect):
250  """! @brief Set dialect.
251  @param dialect The dialect to set.
252  @return FormRepresentation instance.
253  """
254  error_msg = "Dialect value '%s' is not allowed" % dialect
255  check_attr_type(dialect, [str, unicode], error_msg)
256  self.dialect = dialect
257  return self
258 
259  def get_dialect(self):
260  """! @brief Get dialect.
261  @return FormRepresentation attribute 'dialect'.
262  """
263  return self.dialect
264 
265  def set_transliteration(self, transliteration):
266  """! @brief Set transliteration.
267  @param transliteration The transliteration to set.
268  @return FormRepresentation instance.
269  """
270  error_msg = "Transliteration value '%s' is not allowed" % transliteration
271  check_attr_type(transliteration, [str, unicode], error_msg)
272  self.transliteration = transliteration
273  return self
274 
276  """! @brief Get transliteration.
277  @return FormRepresentation attribute 'transliteration'.
278  """
279  return self.transliteration
280 
281  def set_scriptName(self, script_name):
282  """! @brief Set script name.
283  @param script_name The script name to set.
284  @return FormRepresentation instance.
285  """
286  error_msg = "Script name value '%s' is not allowed" % script_name
287  check_attr_type(script_name, [str, unicode], error_msg)
288  self.scriptName = script_name
289  return self
290 
291  def get_scriptName(self):
292  """! @brief Get script name.
293  @return Representation attribute 'scriptName'.
294  """
295  return self.scriptName
296 
297  def create_audio(self):
298  """! @brief Create an Audio instance.
299  @return Audio instance.
300  """
301  return Audio()
302 
303  def get_audio(self):
304  """! @brief Get the audio resource maintained by the form representation.
305  @return Audio instance.
306  """
307  return self.audio
308 
309  def set_audio(self, media_type, file_name, author, quality, start_position, duration, external_reference, audio_file_format):
310  """! @brief Set audio resource.
311  Attributes 'mediaType', 'fileName', 'author', 'quality', 'startPosition', 'durationOfEffectiveSpeech', 'externalReference', 'audioFileFormat' are owned by Material/Audio.
312  @param media_type The media type to set.
313  @param file_name Name of the audio file.
314  @param author Author of the recording.
315  @param quality Quality of the recording, in range 'quality_range' defined in 'common/range.py'.
316  @param start_position Start position of the form in the recording, in format 'Thh:mm:ss,msms', e.g. "T00:05:00".
317  @param duration Duration of the effcetive speech, in format 'PThhHmmMssS', e.g. "PT00:05:00".
318  @param external_reference Reference of the audio file, if not directly provided.
319  @param audio_file_format Format of the audio file, e.g. "wav".
320  @return FormRepresentation instance.
321  """
322  # Create an Audio instance
323  self.audio = self.create_audio()
324  # Set all attributes
325  if media_type is not None:
326  self.audio.set_mediaType(media_type)
327  if file_name is not None:
328  self.audio.set_fileName(file_name)
329  if author is not None:
330  self.audio.set_author(author)
331  if quality is not None:
332  self.audio.set_quality(quality)
333  if start_position is not None:
334  self.audio.set_startPosition(start_position)
335  if duration is not None:
336  self.audio.set_durationOfEffectiveSpeech(duration)
337  if external_reference is not None:
338  self.audio.set_externalReference(external_reference)
339  if audio_file_format is not None:
340  self.audio.set_audioFileFormat(audio_file_format)
341  return self
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
audio
Audio instance is owned by FormRepresentation There is zero or one Audio instance per FormRepresentat...
"Form Representation is a class representing one variant orthography of a Form." (LMF) ...
__speaker
Pointers to an existing Speaker There is zero to many pointers per FormRepresentation instance...
def get_audio
Get the audio resource maintained by the form representation.