예제 #1
0
def write_result(element, filename, encoding=ENCODING):
    """! @brief Write an XML element into a pretty XML output file.
    @param element An XML element.
    @param filename The name of the XML file to write with full path, for instance 'output.xml'.
    @param encoding Encoding mode. Default value is 'utf-8'.
    """
    unicode_str = prettify(element, encoding=encoding)
    output_file = open_write(filename, encoding=encoding)
    output_file.write(unicode_str.decode(encoding))
    output_file.close()
예제 #2
0
def write_result(element, filename, encoding=ENCODING):
    """! @brief Write an XML element into a pretty XML output file.
    @param element An XML element.
    @param filename The name of the XML file to write with full path, for instance 'output.xml'.
    @param encoding Encoding mode. Default value is 'utf-8'.
    """
    unicode_str = prettify(element, encoding=encoding)
    output_file = open_write(filename, encoding=encoding)
    output_file.write(unicode_str.decode(encoding))
    output_file.close()
예제 #3
0
 def test_open_write(self):
     import sys, os
     utest_path = sys.path[0] + '/'
     test_filename = utest_path + "test_file"
     test_string = "Test string."
     # Test open and write file
     test_file = open_write(test_filename)
     test_file.write(test_string)
     test_file.close()
     # Read file
     test_file = open_file(test_filename, 'r')
     self.assertEqual(test_file.readline(), test_string)
     test_file.close()
     # Remove test file
     os.remove(test_filename)
예제 #4
0
 def test_open_write(self):
     import sys, os
     utest_path = sys.path[0] + '/'
     test_filename = utest_path + "test_file"
     test_string = "Test string."
     # Test open and write file
     test_file = open_write(test_filename)
     test_file.write(test_string)
     test_file.close()
     # Read file
     test_file = open_file(test_filename, 'r')
     self.assertEqual(test_file.readline(), test_string)
     test_file.close()
     # Remove test file
     os.remove(test_filename)
예제 #5
0
파일: mdf.py 프로젝트: buret/pylmflib
def mdf_write(object, filename, lmf2mdf=lmf_mdf, order=mdf_order):
    """! @brief Write an MDF file.
    @param object The LMF instance to convert into MDF output format.
    @param filename The name of the MDF file to write with full path, for instance 'user/output.txt'.
    @param lmf2mdf A Python dictionary describing the mapping between LMF representation and MDF markers. Default value is 'lmf_mdf' dictionary defined in 'pylmflib/config/mdf.py'. Please refer to it as an example.
    @param order A Python list defining the order in which MDF markers must be written, for instance ["lx", "ps"]. Default value is 'mdf_order' list defined in 'pylmflib/config/mdf.py'.
    """
    mdf_file = open_write(filename)
    # For each MDF marker, get the corresponding LMF value
    if object.__class__.__name__ == "Lexicon":
        for lexical_entry in object.get_lexical_entries():
            for marker in order:
                if type(marker) is list:
                    # Parse group of markers
                    parse_list(mdf_file, lmf2mdf, marker, lexical_entry)
                else:
                    value = lmf2mdf[marker](lexical_entry)
                    write_line(mdf_file, marker, value)
            # Separate lexical entries from each others with a blank line
            mdf_file.write(EOL)
    else:
        raise OutputError(object, "Object to write must be a Lexicon.")
    mdf_file.close()
예제 #6
0
def mdf_write(object, filename, lmf2mdf=lmf_mdf, order=mdf_order):
    """! @brief Write an MDF file.
    @param object The LMF instance to convert into MDF output format.
    @param filename The name of the MDF file to write with full path, for instance 'user/output.txt'.
    @param lmf2mdf A Python dictionary describing the mapping between LMF representation and MDF markers. Default value is 'lmf_mdf' dictionary defined in 'pylmflib/config/mdf.py'. Please refer to it as an example.
    @param order A Python list defining the order in which MDF markers must be written, for instance ["lx", "ps"]. Default value is 'mdf_order' list defined in 'pylmflib/config/mdf.py'.
    """
    mdf_file = open_write(filename)
    # For each MDF marker, get the corresponding LMF value
    if object.__class__.__name__ == "Lexicon":
        for lexical_entry in object.get_lexical_entries():
            for marker in order:
                if type(marker) is list:
                    # Parse group of markers
                    parse_list(mdf_file, lmf2mdf, marker, lexical_entry)
                else:
                    value = lmf2mdf[marker](lexical_entry)
                    write_line(mdf_file, marker, value)
            # Separate lexical entries from each others with a blank line
            mdf_file.write(EOL)
    else:
        raise OutputError(object, "Object to write must be a Lexicon.")
    mdf_file.close()
예제 #7
0
파일: tex.py 프로젝트: buret/pylmflib
def tex_write(object, filename, preamble=None, introduction=None, lmf2tex=lmf_to_tex, font=None, items=lambda lexical_entry: lexical_entry.get_lexeme(), sort_order=None, paradigms=[], tables=[], title=None, tex_language=None):
    """! @brief Write a LaTeX file.
    Note that the lexicon must already be ordered at this point. Here, parameters 'items' and 'sort_order' are only used to define chapters.
    @param object The LMF instance to convert into LaTeX output format.
    @param filename The name of the LaTeX file to write with full path, for instance 'user/output.tex'.
    @param preamble The name of the LaTeX file with full path containing the LaTeX header of the document, for instance 'user/config/japhug.tex'. Default value is None.
    @param introduction The name of the LaTeX file with full path containing the LaTeX introduction of the document, for instance 'user/config/introduction.tex'. Default value is None.
    @param lmf2tex A function giving the mapping from LMF representation information that must be written to LaTeX commands, in a defined order. Default value is 'lmf_to_tex' function defined in 'pylmflib/config/tex.py'. Please refer to it as an example.
    @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
    @param items Lambda function giving the item to sort. Default value is 'lambda lexical_entry: lexical_entry.get_lexeme()', which means that the items to sort are lexemes.
    @param sort_order Default value is 'None', which means that the LaTeX output is alphabetically ordered.
    @param paradigms A Python list of LaTeX filenames with full path containing the paradigms in LaTeX format. Default value is an empty list.
    @param tables The name of the LaTeX file with full path containing some notes to add at the end of the LaTeX document, for instance 'user/config/conclusion.tex'. Default value is None.
    @param title A Python string containing the title of the LaTeX document. Default value is None.
    @param tex_language A Python string giving the default language to set in LaTeX.
    """
    import string, os
    # Define font
    if font is None:
        font = config.xml.font
    tex_file = open_write(filename)
    # Add file header if any
    tex_file.write(file_read(preamble))
    # Continue the header if needed
    if title is not None:
        tex_file.write("\\title{" + title + "}" + EOL)
    if tex_language is not None:
        tex_file.write("\setdefaultlanguage{" + tex_language + "}" + EOL)
    # Insert LaTeX commands to create a document
    tex_file.write(EOL + "\\begin{document}" + EOL)
    tex_file.write("\\maketitle" + EOL)
    tex_file.write("\\newpage" + EOL)
    # Add introduction if any
    if introduction is not None:
        tex_file.write("\\markboth{INTRODUCTION}{}" + EOL * 2)
    tex_file.write(file_read(introduction))
    # Add command for small caps
    tex_file.write(EOL + "\\def\\mytextsc{\\bgroup\\obeyspaces\\mytextscaux}" + EOL)
    tex_file.write("\\def\\mytextscaux#1{\\mytextscauxii #1\\relax\\relax\\egroup}" + EOL)
    tex_file.write("\\def\\mytextscauxii#1{%" + EOL)
    tex_file.write("\\ifx\\relax#1\\else \\ifcat#1\\@sptoken{} \\expandafter\\expandafter\\expandafter\\mytextscauxii\\else" + EOL)
    tex_file.write("\\ifnum`#1=\\uccode`#1 {\\normalsize #1}\\else {\\footnotesize \\uppercase{#1}}\\fi \\expandafter\\expandafter\\expandafter\\mytextscauxii\\expandafter\\fi\\fi}" + EOL * 2)
    # Configure space indent
    tex_file.write("\\setlength\\parindent{0cm}" + EOL)
    # Insert data path configuration
    # Unix-style paths
    audio_path = config.xml.audio_path
    graphic_path = os.path.abspath('.')
    if os.name != 'posix':
        # Windows-style paths
        audio_path = audio_path.replace("\\", "/")
        graphic_path = graphic_path.replace("\\", "/")
    tex_file.write(EOL + "\\addmediapath{" + audio_path.rstrip("/") + "}" + EOL)
    tex_file.write("\\addmediapath{" + audio_path + "mp3}" + EOL)
    tex_file.write("\\addmediapath{" + audio_path + "wav}" + EOL)
    tex_file.write("\\graphicspath{{" + graphic_path + "/pylmflib/output/img/}}" + EOL * 2)
    # Configure 2 columns
    tex_file.write("\\newpage" + EOL)
    tex_file.write("\\begin{multicols}{2}" + EOL * 2)
    if sort_order is None:
        # Lowercase and uppercase letters must have the same rank
        sort_order = dict([(c, ord(c)) for c in string.lowercase])
        up = dict([(c, ord(c) + 32) for c in string.uppercase])
        sort_order.update(up)
        sort_order.update({'':0, ' ':0})
    # For each element to write, get the corresponding LMF value
    if object.__class__.__name__ == "LexicalResource":
        for lexicon in object.get_lexicons():
            previous_character = ''
            current_character = ''
            # Lexicon is already ordered
            for lexical_entry in lexicon.get_lexical_entries():
                # Consider only main entries (subentries and components will be written as parts of the main entry)
                if lexical_entry.find_related_forms("main entry") == [] and lexical_entry.get_independentWord() is not False:
                    # Check if current element is a lexeme starting with a different character than previous lexeme
                    try:
                        current_character = items(lexical_entry)[0]
                        if sort_order[items(lexical_entry)[0:1]]:
                            current_character = items(lexical_entry)[0:1]
                        if sort_order[items(lexical_entry)[0:2]]:
                            current_character = items(lexical_entry)[0:2]
                    except IndexError:
                        pass
                    except KeyError:
                        pass
                    except TypeError:
                        pass
                    try:
                        if ( (type(sort_order) is not type(dict())) and ((previous_character == '') or (sort_order(current_character) != sort_order(previous_character))) ) \
                            or ( (type(sort_order) is type(dict())) and (int(sort_order[current_character]) != int(sort_order[previous_character])) ):
                            # Do not consider special characters
                            previous_character = current_character
                            tex_file.write("\\newpage" + EOL)
                            title = ''
                            if type(sort_order) is not type(dict()):
                                title += ' ' + font[NATIONAL](current_character)
                            else:
                                for key,value in sorted(sort_order.items(), key=lambda x: x[1]):
                                    if int(value) == int(sort_order[current_character]):
                                        title += ' ' + font[VERNACULAR](key)
                            tex_file.write("\\section*{\\centering-" + handle_reserved(title) + " -}" + EOL)
                            #tex_file.write("\\pdfbookmark[1]{" + title + " }{" + title + " }" + EOL)
                        tex_file.write(lmf2tex(lexical_entry, font))
                        if len(paradigms) != 0:
                            tex_file.write(insert_references(lexical_entry))
                        tex_file.write("\\lhead{\\firstmark}" + EOL)
                        tex_file.write("\\rhead{\\botmark}" + EOL)
                        # Separate lexical entries from each others with a blank line
                        tex_file.write(EOL)
                        # Handle subentries
                        for related_form in lexical_entry.get_related_forms("subentry"):
                            if related_form.get_lexical_entry() is not None:
                                tex_file.write(lmf2tex(related_form.get_lexical_entry(), font))
                                if len(paradigms) != 0:
                                    tex_file.write(insert_references(related_form.get_lexical_entry()))
                                # Separate sub-entries from each others with a blank line
                                tex_file.write(EOL)
                    except KeyError:
                        print Warning("Cannot sort item %s" % items(lexical_entry).encode(ENCODING))
                    except IndexError:
                        # Item is an empty string
                        pass
    else:
        raise OutputError(object, "Object to write must be a Lexical Resource.")
    # Insert LaTeX commands to finish the document properly
    tex_file.write("\end{multicols}" + EOL)
    # Insert paradigms if any
    for filename in paradigms:
        tex_file.write(EOL)
        tex_file.write("\\newpage" + EOL)
        tex_file.write("\markboth{paradigms}{}" + EOL)
        tex_file.write(file_read(filename))
        tex_file.write(EOL)
    # Insert other tables if any
    for filename in tables:
        tex_file.write(EOL)
        tex_file.write("\\newpage" + EOL)
        tex_file.write(file_read(filename))
        tex_file.write(EOL)
    tex_file.write("\end{document}" + EOL)
    tex_file.close()
예제 #8
0
파일: tex.py 프로젝트: yuhsianglin/HimalCo
def tex_write(object,
              filename,
              preamble=None,
              introduction=None,
              lmf2tex=lmf_to_tex,
              font=None,
              items=lambda lexical_entry: lexical_entry.get_lexeme(),
              sort_order=None,
              paradigms=[],
              tables=[],
              title=None,
              tex_language=None):
    """! @brief Write a LaTeX file.
    Note that the lexicon must already be ordered at this point. Here, parameters 'items' and 'sort_order' are only used to define chapters.
    @param object The LMF instance to convert into LaTeX output format.
    @param filename The name of the LaTeX file to write with full path, for instance 'user/output.tex'.
    @param preamble The name of the LaTeX file with full path containing the LaTeX header of the document, for instance 'user/config/japhug.tex'. Default value is None.
    @param introduction The name of the LaTeX file with full path containing the LaTeX introduction of the document, for instance 'user/config/introduction.tex'. Default value is None.
    @param lmf2tex A function giving the mapping from LMF representation information that must be written to LaTeX commands, in a defined order. Default value is 'lmf_to_tex' function defined in 'pylmflib/config/tex.py'. Please refer to it as an example.
    @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
    @param items Lambda function giving the item to sort. Default value is 'lambda lexical_entry: lexical_entry.get_lexeme()', which means that the items to sort are lexemes.
    @param sort_order Default value is 'None', which means that the LaTeX output is alphabetically ordered.
    @param paradigms A Python list of LaTeX filenames with full path containing the paradigms in LaTeX format. Default value is an empty list.
    @param tables The name of the LaTeX file with full path containing some notes to add at the end of the LaTeX document, for instance 'user/config/conclusion.tex'. Default value is None.
    @param title A Python string containing the title of the LaTeX document. Default value is None.
    @param tex_language A Python string giving the default language to set in LaTeX.
    """
    import string, os
    # Define font
    if font is None:
        font = config.xml.font
    tex_file = open_write(filename)
    # Add file header if any
    tex_file.write(file_read(preamble))
    # Continue the header if needed
    if title is not None:
        tex_file.write("\\title{" + unicode(title, "utf-8") + "}" + EOL)
    if tex_language is not None:
        tex_file.write("\setdefaultlanguage{" + tex_language + "}" + EOL)
    # Insert LaTeX commands to create a document
    tex_file.write(EOL + "\\begin{document}" + EOL)
    tex_file.write("\\maketitle" + EOL)
    tex_file.write("\\newpage" + EOL)
    # Add introduction if any
    if introduction is not None:
        tex_file.write("\\markboth{INTRODUCTION}{}" + EOL * 2)
    tex_file.write(file_read(introduction))
    # Add command for small caps
    tex_file.write(EOL + "\\def\\mytextsc{\\bgroup\\obeyspaces\\mytextscaux}" +
                   EOL)
    tex_file.write(
        "\\def\\mytextscaux#1{\\mytextscauxii #1\\relax\\relax\\egroup}" + EOL)
    tex_file.write("\\def\\mytextscauxii#1{%" + EOL)
    tex_file.write(
        "\\ifx\\relax#1\\else \\ifcat#1\\@sptoken{} \\expandafter\\expandafter\\expandafter\\mytextscauxii\\else"
        + EOL)
    tex_file.write(
        "\\ifnum`#1=\\uccode`#1 {\\normalsize #1}\\else {\\footnotesize \\uppercase{#1}}\\fi \\expandafter\\expandafter\\expandafter\\mytextscauxii\\expandafter\\fi\\fi}"
        + EOL * 2)
    # Configure space indent
    tex_file.write("\\setlength\\parindent{0cm}" + EOL)
    # Insert data path configuration
    # Unix-style paths
    audio_path = config.xml.audio_path
    graphic_path = os.path.abspath('.')
    if os.name != 'posix':
        # Windows-style paths
        audio_path = audio_path.replace("\\", "/")
        graphic_path = graphic_path.replace("\\", "/")
    tex_file.write(EOL + "\\addmediapath{" + audio_path.rstrip("/") + "}" +
                   EOL)
    tex_file.write("\\addmediapath{" + audio_path + "mp3}" + EOL)
    tex_file.write("\\addmediapath{" + audio_path + "wav}" + EOL)
    tex_file.write("\\graphicspath{{" + graphic_path +
                   "/pylmflib/output/img/}}" + EOL * 2)
    # Configure 2 columns
    tex_file.write("\\newpage" + EOL)
    tex_file.write("\\begin{multicols}{2}" + EOL * 2)
    if sort_order is None:
        # Lowercase and uppercase letters must have the same rank
        sort_order = dict([(c, ord(c)) for c in string.lowercase])
        up = dict([(c, ord(c) + 32) for c in string.uppercase])
        sort_order.update(up)
        sort_order.update({'': 0, ' ': 0})
    # For each element to write, get the corresponding LMF value
    if object.__class__.__name__ == "LexicalResource":
        for lexicon in object.get_lexicons():
            previous_character = ''
            current_character = ''
            # Lexicon is already ordered
            for lexical_entry in lexicon.get_lexical_entries():
                # Consider only main entries (subentries and components will be written as parts of the main entry)
                if lexical_entry.find_related_forms(
                        "main entry"
                ) == [] and lexical_entry.get_independentWord() is not False:
                    # Check if current element is a lexeme starting with a different character than previous lexeme
                    try:
                        current_character = items(lexical_entry)[0]
                        if sort_order[items(lexical_entry)[0:1]]:
                            current_character = items(lexical_entry)[0:1]
                        if sort_order[items(lexical_entry)[0:2]]:
                            current_character = items(lexical_entry)[0:2]
                    except IndexError:
                        pass
                    except KeyError:
                        pass
                    except TypeError:
                        pass
                    try:
                        if ( (type(sort_order) is not type(dict())) and ((previous_character == '') or (sort_order(current_character) != sort_order(previous_character))) ) \
                            or ( (type(sort_order) is type(dict())) and (int(sort_order[current_character]) != int(sort_order[previous_character])) ):
                            # Do not consider special characters
                            previous_character = current_character
                            tex_file.write("\\newpage" + EOL)
                            title = ''
                            if type(sort_order) is not type(dict()):
                                title += ' ' + font[NATIONAL](
                                    current_character)
                            else:
                                for key, value in sorted(sort_order.items(),
                                                         key=lambda x: x[1]):
                                    if int(value) == int(
                                            sort_order[current_character]):
                                        title += ' ' + font[VERNACULAR](key)
                            tex_file.write("\\section*{\\centering-" +
                                           handle_reserved(title) + " -}" +
                                           EOL)
                            #tex_file.write("\\pdfbookmark[1]{" + title + " }{" + title + " }" + EOL)
                        tex_file.write(lmf2tex(lexical_entry, font))
                        if len(paradigms) != 0:
                            tex_file.write(insert_references(lexical_entry))
                        # tex_file.write("\\lhead{\\firstmark}" + EOL)
                        # tex_file.write("\\rhead{\\botmark}" + EOL)
                        # Separate lexical entries from each others with a blank line
                        tex_file.write(EOL)
                        # Handle subentries
                        for related_form in lexical_entry.get_related_forms(
                                "subentry"):
                            if related_form.get_lexical_entry() is not None:
                                tex_file.write(
                                    lmf2tex(related_form.get_lexical_entry(),
                                            font))
                                if len(paradigms) != 0:
                                    tex_file.write(
                                        insert_references(
                                            related_form.get_lexical_entry()))
                                # Separate sub-entries from each others with a blank line
                                tex_file.write(EOL)
                    except KeyError:
                        print Warning("Cannot sort item %s" %
                                      items(lexical_entry).encode(ENCODING))
                    except IndexError:
                        # Item is an empty string
                        pass
    else:
        raise OutputError(object,
                          "Object to write must be a Lexical Resource.")
    # Insert LaTeX commands to finish the document properly
    tex_file.write("\end{multicols}" + EOL)
    # Insert paradigms if any
    for filename in paradigms:
        tex_file.write(EOL)
        tex_file.write("\\newpage" + EOL)
        tex_file.write("\markboth{paradigms}{}" + EOL)
        tex_file.write(file_read(filename))
        tex_file.write(EOL)
    # Insert other tables if any
    for filename in tables:
        tex_file.write(EOL)
        tex_file.write("\\newpage" + EOL)
        tex_file.write(file_read(filename))
        tex_file.write(EOL)
    tex_file.write("\end{document}" + EOL)
    tex_file.close()