def test_xml_lmf_write(self): import sys, os # Create LMF objects lexical_entry = LexicalEntry() lexical_entry.lemma = Lemma() lexical_entry.partOfSpeech = "toto" lexical_entry.status = "draft" lexical_entry.lemma.lexeme = "hello" # Write XML LMF file and test result utest_path = sys.path[0] + '/' xml_lmf_filename = utest_path + "lmf_output.xml" xml_lmf_write(lexical_entry, xml_lmf_filename) xml_lmf_file = open(xml_lmf_filename, "r") expected_lines = [ """<?xml version="1.0" encoding="utf-8"?>""" + EOL, """<LexicalEntry id="0">""" + EOL, """ <feat att="status" val="draft"/>""" + EOL, """ <Lemma>""" + EOL, """ <feat att="lexeme" val="hello"/>""" + EOL, """ </Lemma>""" + EOL, """ <feat att="partOfSpeech" val="toto"/>""" + EOL, """</LexicalEntry>""" + EOL ] self.assertListEqual(expected_lines, xml_lmf_file.readlines()) xml_lmf_file.close() del lexical_entry.lemma lexical_entry.lemma = None del lexical_entry # Remove XML LMF file os.remove(xml_lmf_filename)
def set_spelling_variant(self, spelling_variant): """! @brief Set spelling variant. Attribute 'spellingVariant' is owned by FormRepresentation, which is owned by Lemma. @param spelling_variant The spelling variant to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_spelling_variant(spelling_variant) return self
def set_contextual_variation(self, contextual_variation): """! @brief Set contextual variation. Attribute 'contextualVariation' is owned by FormRepresentation, which is owned by Lemma. @param contextual_variation The contextual variation to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_contextual_variation(contextual_variation) return self
def set_transliteration(self, transliteration): """! @brief Set transliteration. Attribute 'transliteration' is owned by FormRepresentation, which is owned by Lemma. @param transliteration The transliteration to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_transliteration(transliteration) return self
def set_tone(self, tone): """! @brief Set tone. Attribute 'tone' is owned by FormRepresentation, which is owned by Lemma. @param tone The tone to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_tone(tone) return self
def set_lexeme(self, lexeme): """! @brief Set lexeme. Attribute 'lexeme' is owned by Lemma. @param lexeme The lexeme to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_lexeme(lexeme) return self
def set_script_name(self, script_name): """! @brief Set script name. Attribute 'scriptName' is owned by FormRepresentation, which is owned by Lemma. @param script_name The script name to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_script_name(script_name) return self
def set_dialect(self, dialect): """! @brief Set dialect. Attribute 'dialect' is owned by FormRepresentation, which is owned by Lemma. @param dialect The dialect to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_dialect(dialect) return self
def set_geographical_variant(self, geographical_variant): """! @brief Set geographical variant. Attribute 'geographicalVariant' is owned by FormRepresentation, which is owned by Lemma. @param geographical_variant The geographical variant to set. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_geographical_variant(geographical_variant) return self
def set_citation_form(self, citation_form, script_name=None): """! @brief Set citation form. Attribute 'citationForm' is owned by FormRepresentation, which is owned by Lemma. @param citation_form The citation form to set. @param script_name The name of the script used to write the citation form, e.g. devanagari. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_citation_form(citation_form, script_name) return self
def set_phonetic_form(self, phonetic_form, script_name=None): """! @brief Set phonetic form. Attribute 'phoneticForm' is owned by FormRepresentation, which is owned by Lemma. @param phonetic_form The phonetic form to set. @param script_name The name of the script used to write the phonetic form, e.g. pinyin. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_phonetic_form(phonetic_form, script_name) return self
def set_variant_comment(self, comment, language=None): """! @brief Set variant comment and language. Attributes 'comment' and 'language' are owned by FormRepresentation, which is owned by Lemma. @param comment Variant comment. @param language Language of comment. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_variant_comment(comment, language) return self
def set_variant_form(self, variant_form, type="unspecified"): """! @brief Set variant form and type. Attributes 'variantForm' and 'type' are owned by FormRepresentation, which is owned by Lemma. @param variant_form Variant form. @param type Type of variant, in range 'type_variant_range' defined in 'common/range.py'. @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_variant_form(variant_form, type) return self
def set_audio(self, media_type="audio", file_name=None, author=None, quality=None, start_position="T00:00:00", duration=None, external_reference=None, audio_file_format=None): """! @brief Set audio resource. Attributes 'mediaType', 'fileName', 'author', 'quality', 'startPosition', 'durationOfEffectiveSpeech', 'externalReference', 'audioFileFormat' are owned by Material/Audio, which is owned by FormRepresentation, itself owend by Lemma. @param media_type The media type to set. @param file_name Name of the audio file. @param author Author of the recording. @param quality Quality of the recording, in range 'quality_range' defined in 'common/range.py'. @param start_position Start position of the form in the recording, in format 'Thh:mm:ss,msms', e.g. "T00:05:00". @param duration Duration of the effcetive speech, in format 'PThhHmmMssS', e.g. "PT00:05:00". @param external_reference Reference of the audio file, if not directly provided. @param audio_file_format Format of the audio file, e.g. "wav". @return LexicalEntry instance. """ # Create a Lemma instance if not yet created if self.lemma is None: self.lemma = Lemma() self.lemma.set_audio(media_type, file_name, author, quality, start_position, duration, external_reference, audio_file_format) return self
def test_mdf_write(self): import sys, os # Create LMF objects lexical_entry = LexicalEntry() lexical_entry.lemma = Lemma() lexical_entry.partOfSpeech = "toto" lexical_entry.status = "draft" lexical_entry.lemma.lexeme = "hello" lexicon = Lexicon() lexicon.add_lexical_entry(lexical_entry) # Write MDF file and test result utest_path = sys.path[0] + '/' mdf_filename = utest_path + "output.txt" mdf_write(lexicon, mdf_filename) mdf_file = open(mdf_filename, "r") expected_lines = [ "\\lx hello" + EOL, "\\ps toto" + EOL, "\\st draft" + EOL, EOL ] self.assertListEqual(expected_lines, mdf_file.readlines()) mdf_file.close() # Customize mapping lmf2mdf = dict({ "lx": lambda lexical_entry: lexical_entry.get_status(), "ps": lambda lexical_entry: lexical_entry.get_partOfSpeech(), "st": lambda lexical_entry: lexical_entry.get_lexeme() }) order = ["st", "lx", "ps"] # Write MDF file and test result mdf_write(lexicon, mdf_filename, lmf2mdf, order) mdf_file = open(mdf_filename, "r") expected_lines = [ "\\st hello" + EOL, "\\lx draft" + EOL, "\\ps toto" + EOL, EOL ] self.assertListEqual(expected_lines, mdf_file.readlines()) mdf_file.close() del lexical_entry.lemma lexical_entry.lemma = None del lexical_entry, lexicon # Remove MDF file os.remove(mdf_filename)
def test_build_sub_elements(self): # Create LMF objects and an empty XML element instance = LexicalEntry() instance.lemma = Lemma() instance.partOfSpeech = "toto" instance.status = "draft" instance.lemma.lexeme = "hello" element = Element("LexicalEntry") # Build sub-elements and test result build_sub_elements(instance, element) lemma = element.find("Lemma") lexeme = lemma.find("feat") self.assertEqual(lexeme.attrib["att"], "lexeme") self.assertEqual(lexeme.attrib["val"], "hello") [status, partOfSpeech] = element.findall("feat") self.assertEqual(partOfSpeech.attrib["att"], "partOfSpeech") self.assertEqual(partOfSpeech.attrib["val"], "toto") self.assertEqual(status.attrib["att"], "status") self.assertEqual(status.attrib["val"], "draft") del instance.lemma instance.lemma = None del instance, element
def test_doc_write(self): import sys, os # Create LMF objects lexical_entry = LexicalEntry() lexical_entry.lemma = Lemma() lexical_entry.partOfSpeech = "toto" lexical_entry.status = "draft" lexical_entry.lemma.lexeme = "hello" lexicon = Lexicon() lexicon.add_lexical_entry(lexical_entry) lexical_resource = LexicalResource() lexical_resource.add_lexicon(lexicon) # Write document file and test result utest_path = sys.path[0] + '/' doc_filename = utest_path + "output.docx" doc_write(lexical_resource, doc_filename) doc_file = open(doc_filename, "r") doc_file.readlines() doc_file.close() # Customize mapping def lmf2doc(lexicon, document, items, sort_order, paradigms, reverse): return "test" # Write document file and test result doc_write(lexical_resource, doc_filename, None, lmf2doc) doc_file = open(doc_filename, "r") doc_file.readlines() doc_file.close() del lexical_entry.lemma lexical_entry.lemma = None del lexical_entry, lexicon lexicon = None del lexical_resource # Remove document file os.remove(doc_filename)
def test_tex_write(self): import sys, os # Create LMF objects lexical_entry = LexicalEntry() lexical_entry.lemma = Lemma() lexical_entry.partOfSpeech = "toto" lexical_entry.status = "draft" lexical_entry.lemma.lexeme = "hello" lexicon = Lexicon() lexicon.add_lexical_entry(lexical_entry) lexical_resource = LexicalResource() lexical_resource.add_lexicon(lexicon) # Write LaTeX file and test result utest_path = sys.path[0] + '/' tex_filename = utest_path + "output.tex" tex_write(lexical_resource, tex_filename) tex_file = open(tex_filename, "r") begin_lines = [ EOL, "\\begin{document}" + EOL, "\\maketitle" + EOL, "\\newpage" + EOL, EOL, "\\def\\mytextsc{\\bgroup\\obeyspaces\\mytextscaux}" + EOL, "\\def\\mytextscaux#1{\\mytextscauxii #1\\relax\\relax\\egroup}" + EOL, "\\def\\mytextscauxii#1{%" + EOL, "\\ifx\\relax#1\\else \\ifcat#1\\@sptoken{} \\expandafter\\expandafter\\expandafter\\mytextscauxii\\else" + EOL, "\\ifnum`#1=\\uccode`#1 {\\normalsize #1}\\else {\\footnotesize \\uppercase{#1}}\\fi \\expandafter\\expandafter\\expandafter\\mytextscauxii\\expandafter\\fi\\fi}" + EOL, EOL, "\\setlength\\parindent{0cm}" + EOL, EOL, "\\addmediapath{.}" + EOL, "\\addmediapath{./mp3}" + EOL, "\\addmediapath{./wav}" + EOL, "\\graphicspath{{" + os.path.abspath('.') + "/pylmflib/output/img/}}" + EOL, EOL, "\\newpage" + EOL, "\\begin{multicols}{2}" + EOL, EOL ] end_lines = ["\end{multicols}" + EOL, "\end{document}" + EOL] expected_lines = [ "\\newpage" + EOL, "\\section*{\\centering- \\textbf{\ipa{H}} \\textbf{\ipa{h}} -}" + EOL, #"\\pdfbookmark[1]{\ipa{ H h }}{ H h }" + EOL, "\\paragraph{\\hspace{-0.5cm} \\textbf{\ipa{hello}}} \\hypertarget{01}{}" + EOL, "\markboth{\\textbf{\\ipa{hello}}}{}" + EOL, "\\textit{Status:} draft" + EOL, "\lhead{\\firstmark}" + EOL, "\\rhead{\\botmark}" + EOL, EOL ] self.assertListEqual(begin_lines + expected_lines + end_lines, tex_file.readlines()) tex_file.close() # Customize mapping my_lmf_tex = dict({ "Lemma.lexeme": lambda lexical_entry: "is " + lexical_entry.get_lexeme( ) + "." + EOL, "LexicalEntry.id": lambda lexical_entry: "The lexical entry " + str(lexical_entry. get_id()) + " ", "LexicalEntry.partOfSpeech": lambda lexical_entry: "Its grammatical category is " + lexical_entry.get_partOfSpeech() + "." + EOL, "LexicalEntry.status": lambda lexical_entry: "Warning: " + lexical_entry.get_status( ) + " version!" + EOL }) my_order = [ "LexicalEntry.id", "Lemma.lexeme", "LexicalEntry.partOfSpeech", "LexicalEntry.status" ] def lmf2tex(entry, font): result = "" for attribute in my_order: result += my_lmf_tex[attribute](entry) return result # Write LaTeX file and test result tex_write(lexical_resource, tex_filename, None, None, lmf2tex, font) tex_file = open(tex_filename, "r") expected_lines = [ "\\newpage" + EOL, "\\section*{\\centering- \\textbf{\ipa{H}} \\textbf{\ipa{h}} -}" + EOL, #"\\pdfbookmark[1]{\ipa{ H h }}{ H h }" + EOL, "The lexical entry 01 is hello." + EOL, "Its grammatical category is toto." + EOL, "Warning: draft version!" + EOL, "\lhead{\\firstmark}" + EOL, "\\rhead{\\botmark}" + EOL, EOL ] self.assertListEqual(begin_lines + expected_lines + end_lines, tex_file.readlines()) tex_file.close() del lexical_entry.lemma lexical_entry.lemma = None del lexical_entry, lexicon lexicon = None del lexical_resource # Remove LaTeX file os.remove(tex_filename)
def setUp(self): # Instantiate a Lemma object self.lemma = Lemma()