Python LMF library
 All Classes Namespaces Files Functions Variables
tables.py
Go to the documentation of this file.
1 #/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 """! @package utils.tables
5 """
6 
7 # Get command line arguments
8 from optparse import OptionParser
9 parser = OptionParser()
10 parser.add_option("-i", "--input", dest="input", action="store", help="input MDF file")
11 parser.add_option("-e", "--output-eng", dest="output_eng", action="store", help="output LaTeX file in English")
12 parser.add_option("-f", "--output-fra", dest="output_fra", action="store", help="output LaTeX file in French")
13 options = parser.parse_args()[0]
14 
15 # Open input and output files
16 try:
17  in_file = open(options.input, "r", encoding='utf-8')
18  out_eng = open(options.output_eng, "w", encoding='utf-8')
19  out_fra = open(options.output_fra, "w", encoding='utf-8')
20 except TypeError:
21  in_file = open(options.input, "r")
22  out_eng = open(options.output_eng, "w")
23  out_fra = open(options.output_fra, "w")
24 
25 # Define EOL depending on operating system
26 import os
27 if os.name == 'posix':
28  # Unix-style end of line
29  EOL = '\n'
30 else:
31  # Windows-style end of line
32  EOL = '\r\n'
33 
34 title_eng = """Words for which no close equivalent could be found"""
35 introduction_eng = """The list that follows groups words for which no close equivalents could be found. These negative pieces of information contain hints about the consultants' Na vocabulary and its 'soft shoulders'."""
36 title_fra = """Mots dont aucun équivalent n'a été trouvé"""
37 introduction_fra = """Cette liste regroupe les mots dont aucun équivalent n'a été trouvé. Même s'il ne s'agit que d'informations négatives, elles éclairent les limites du vocabulaire na des consultants."""
38 
39 # Add section
40 out_eng.write("\\section*{\\centering " + title_eng + "}" + EOL)
41 out_fra.write("\\section*{\\centering " + title_fra + "}" + EOL)
42 # Insert explanation
43 out_eng.write(introduction_eng + EOL)
44 out_fra.write(introduction_fra + EOL)
45 # Begin table
46 out_eng.write("\\begin{center}" + EOL)
47 out_fra.write("\\begin{center}" + EOL)
48 out_eng.write("\\begin{longtable}{r|l}" + EOL)
49 out_fra.write("\\begin{longtable}{r|l}" + EOL)
50 
51 # Generate table
52 import re
53 pattern = r"^\\(\w{2,3}) ?(.*)$"
54 lx = ""
55 ge = ""
56 gn = ""
57 gf = ""
58 for line in in_file.readlines():
59  result = re.search(pattern, line)
60  if result:
61  if result.group(1) == "lx" and result.group(2) == "*":
62  lx = result.group(2)
63  elif result.group(1) == "ge":
64  if lx == "*":
65  ge = result.group(2).replace('_', "\string_")
66  elif result.group(1) == "gn":
67  if lx == "*":
68  gn = result.group(2).replace('_', "\string_")
69  elif result.group(1) == "gf":
70  if lx == "*":
71  gf = result.group(2).replace('_', "\string_")
72  out_eng.write(ge)
73  out_fra.write(gf)
74  out_eng.write(" & ")
75  out_fra.write(" & ")
76  out_eng.write("\\textcolor{brown}{\zh{" + gn + "}}")
77  out_fra.write("\\textcolor{brown}{\zh{" + gn + "}}")
78  out_eng.write(" \\\\" + EOL)
79  out_fra.write(" \\\\" + EOL)
80  # Reset loop variables
81  lx = ""
82  ge = ""
83  gn = ""
84  gf = ""
85 
86 # Do not forget to end tabular and close files
87 out_eng.write("\\end{longtable}")
88 out_fra.write("\\end{longtable}")
89 out_eng.write("\\end{center}")
90 out_fra.write("\\end{center}")
91 in_file.close()
92 out_eng.close()
93 out_fra.close()