Python LMF library
 All Classes Namespaces Files Functions Variables
doc.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 """! @package output
5 """
6 
7 from config.doc import lmf_to_doc
8 from utils.error_handling import OutputError
9 from utils.io import open_read
10 
11 from docx import Document
12 from docx.shared import Cm
13 
14 def file_read(filename):
15  """! @brief Read file contents.
16  @param filename The name of the file with full path containing information to read, for instance the text introduction of the document: 'user/config/introduction.txt'.
17  @return A Python string containing read information.
18  """
19  contents = ""
20  if filename is not None:
21  file = open_read(filename)
22  contents = file.read()
23  file.close()
24  return contents
25 
26 def doc_write(object, filename, introduction=None, lmf2doc=lmf_to_doc, items=lambda lexical_entry: lexical_entry.get_lexeme(), sort_order=None, paradigms=False, reverse=False):
27  """! @brief Write a document file.
28  @param object The LMF instance to convert into document output format.
29  @param filename The name of the document file to write with full path, for instance 'user/output.doc'.
30  @param introduction The name of the text file with full path containing the introduction of the document, for instance 'user/config/introduction.txt'. Default value is None.
31  @param lmf2doc A function giving the mapping from LMF representation information that must be written to docx commands, in a defined order. Default value is 'lmf_to_doc' function defined in 'pylmflib/config/doc.py'. Please refer to it as an example.
32  @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.
33  @param sort_order Python list. Default value is 'None', which means that the document output is alphabetically ordered.
34  @param paradigms A boolean value to introduce paradigms in document or not.
35  @param reverse A boolean value to set if a reverse dictionary is wanted.
36  """
37  import string
38  if sort_order is None:
39  # Lowercase and uppercase letters must have the same rank
40  sort_order = dict([(c, ord(c)) for c in string.lowercase])
41  up = dict([(c, ord(c) + 32) for c in string.uppercase])
42  sort_order.update(up)
43  sort_order.update({'':0, ' ':0})
44  document = Document()
45  # Adjust margins to 1 cm
46  section = document.sections[0]
47  section.top_margin = Cm(1)
48  section.bottom_margin = Cm(1)
49  section.left_margin = Cm(1)
50  section.right_margin = Cm(1)
51  # Parse LMF values
52  if object.__class__.__name__ == "LexicalResource":
53  for lexicon in object.get_lexicons():
54  # Document title
55  document.add_heading(lexicon.get_id(), 0)
56  # Plain paragraph
57  document.add_paragraph(lexicon.get_label())
58  # Page break
59  document.add_page_break()
60  # Introduction
61  if introduction is not None:
62  document.add_paragraph(file_read(introduction))
63  # Text body
64  lmf2doc(lexicon, document, items, sort_order, paradigms, reverse)
65  else:
66  raise OutputError(object, "Object to write must be a Lexical Resource.")
67  document.save(filename)
def open_read
Open file in read mode (automatically decode file in unicode).
Definition: io.py:36
def doc_write
Write a document file.
Definition: doc.py:26
def file_read
Read file contents.
Definition: doc.py:14