Python LMF library
 All Classes Namespaces Files Functions Variables
tex.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.tex import lmf_to_tex, partOfSpeech_tex
8 from config.mdf import mdf_semanticRelation, pd_grammaticalNumber, pd_person, pd_anymacy, pd_clusivity
9 from utils.io import open_read, open_write, EOL, ENCODING
10 from utils.error_handling import OutputError, Warning
11 from common.defs import VERNACULAR, ENGLISH, NATIONAL, REGIONAL
12 
13 # To define languages and fonts
14 import config
15 
16 def file_read(filename):
17  """! @brief Read file contents.
18  @param filename The name of the file with full path containing information to read, for instance the LaTeX header of the document: 'user/config/header.tex'.
19  @return A Python string containing read information.
20  """
21  contents = ""
22  if filename is not None:
23  file = open_read(filename)
24  contents = file.read()
25  file.close()
26  return contents
27 
28 def insert_references(lexical_entry):
29  """! @brief Insert references to paradigms.
30  @param lexical_entry The targeted Lexical Entry LMF instance.
31  @return A string representing the references in LaTeX format.
32  """
33  text = ""
34  part_of_speech = lexical_entry.get_partOfSpeech()
35  spelling_variant = None
36  if len(lexical_entry.get_spelling_variants()) != 0:
37  spelling_variant = lexical_entry.get_spelling_variants()[0]
38  if spelling_variant is None:
39  # If current entry is a subentry, then take the spelling variant of the main entry
40  main_entry = lexical_entry.get_main_entry()
41  if main_entry is not None:
42  if len(main_entry.get_spelling_variants()) != 0:
43  spelling_variant = main_entry.get_spelling_variants()[0]
44  if spelling_variant is not None:
45  if part_of_speech == "transitive verb":
46  text += "\\hfill\\break See: \\ref{" + spelling_variant + ".vt}" + EOL
47  text += "\\ref{" + spelling_variant + ".vt.eng}" + EOL
48  elif part_of_speech == "intransitive verb":
49  text += "\\hfill\\break See: \\ref{" + spelling_variant + ".vi}" + EOL
50  text += "\\ref{" + spelling_variant + ".vi.eng}" + EOL
51  elif part_of_speech == "reflexive verb":
52  text += "\\hfill\\break See: \\ref{" + spelling_variant + ".vr}" + EOL
53  text += "\\ref{" + spelling_variant + ".vr.eng}" + EOL
54  return text
55 
56 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):
57  """! @brief Write a LaTeX file.
58  Note that the lexicon must already be ordered at this point. Here, parameters 'items' and 'sort_order' are only used to define chapters.
59  @param object The LMF instance to convert into LaTeX output format.
60  @param filename The name of the LaTeX file to write with full path, for instance 'user/output.tex'.
61  @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.
62  @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.
63  @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.
64  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
65  @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.
66  @param sort_order Default value is 'None', which means that the LaTeX output is alphabetically ordered.
67  @param paradigms A Python list of LaTeX filenames with full path containing the paradigms in LaTeX format. Default value is an empty list.
68  @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.
69  @param title A Python string containing the title of the LaTeX document. Default value is None.
70  @param tex_language A Python string giving the default language to set in LaTeX.
71  """
72  import string, os
73  # Define font
74  if font is None:
75  font = config.xml.font
76  tex_file = open_write(filename)
77  # Add file header if any
78  tex_file.write(file_read(preamble))
79  # Continue the header if needed
80  if title is not None:
81  tex_file.write("\\title{" + title + "}" + EOL)
82  if tex_language is not None:
83  tex_file.write("\setdefaultlanguage{" + tex_language + "}" + EOL)
84  # Insert LaTeX commands to create a document
85  tex_file.write(EOL + "\\begin{document}" + EOL)
86  tex_file.write("\\maketitle" + EOL)
87  tex_file.write("\\newpage" + EOL)
88  # Add introduction if any
89  if introduction is not None:
90  tex_file.write("\\markboth{INTRODUCTION}{}" + EOL * 2)
91  tex_file.write(file_read(introduction))
92  # Add command for small caps
93  tex_file.write(EOL + "\\def\\mytextsc{\\bgroup\\obeyspaces\\mytextscaux}" + EOL)
94  tex_file.write("\\def\\mytextscaux#1{\\mytextscauxii #1\\relax\\relax\\egroup}" + EOL)
95  tex_file.write("\\def\\mytextscauxii#1{%" + EOL)
96  tex_file.write("\\ifx\\relax#1\\else \\ifcat#1\\@sptoken{} \\expandafter\\expandafter\\expandafter\\mytextscauxii\\else" + EOL)
97  tex_file.write("\\ifnum`#1=\\uccode`#1 {\\normalsize #1}\\else {\\footnotesize \\uppercase{#1}}\\fi \\expandafter\\expandafter\\expandafter\\mytextscauxii\\expandafter\\fi\\fi}" + EOL * 2)
98  # Configure space indent
99  tex_file.write("\\setlength\\parindent{0cm}" + EOL)
100  # Insert data path configuration
101  # Unix-style paths
102  audio_path = config.xml.audio_path
103  graphic_path = os.path.abspath('.')
104  if os.name != 'posix':
105  # Windows-style paths
106  audio_path = audio_path.replace("\\", "/")
107  graphic_path = graphic_path.replace("\\", "/")
108  tex_file.write(EOL + "\\addmediapath{" + audio_path.rstrip("/") + "}" + EOL)
109  tex_file.write("\\addmediapath{" + audio_path + "mp3}" + EOL)
110  tex_file.write("\\addmediapath{" + audio_path + "wav}" + EOL)
111  tex_file.write("\\graphicspath{{" + graphic_path + "/pylmflib/output/img/}}" + EOL * 2)
112  # Configure 2 columns
113  tex_file.write("\\newpage" + EOL)
114  tex_file.write("\\begin{multicols}{2}" + EOL * 2)
115  if sort_order is None:
116  # Lowercase and uppercase letters must have the same rank
117  sort_order = dict([(c, ord(c)) for c in string.lowercase])
118  up = dict([(c, ord(c) + 32) for c in string.uppercase])
119  sort_order.update(up)
120  sort_order.update({'':0, ' ':0})
121  # For each element to write, get the corresponding LMF value
122  if object.__class__.__name__ == "LexicalResource":
123  for lexicon in object.get_lexicons():
124  previous_character = ''
125  current_character = ''
126  # Lexicon is already ordered
127  for lexical_entry in lexicon.get_lexical_entries():
128  # Consider only main entries (subentries and components will be written as parts of the main entry)
129  if lexical_entry.find_related_forms("main entry") == [] and lexical_entry.get_independentWord() is not False:
130  # Check if current element is a lexeme starting with a different character than previous lexeme
131  try:
132  current_character = items(lexical_entry)[0]
133  if sort_order[items(lexical_entry)[0:1]]:
134  current_character = items(lexical_entry)[0:1]
135  if sort_order[items(lexical_entry)[0:2]]:
136  current_character = items(lexical_entry)[0:2]
137  except IndexError:
138  pass
139  except KeyError:
140  pass
141  except TypeError:
142  pass
143  try:
144  if ( (type(sort_order) is not type(dict())) and ((previous_character == '') or (sort_order(current_character) != sort_order(previous_character))) ) \
145  or ( (type(sort_order) is type(dict())) and (int(sort_order[current_character]) != int(sort_order[previous_character])) ):
146  # Do not consider special characters
147  previous_character = current_character
148  tex_file.write("\\newpage" + EOL)
149  title = ''
150  if type(sort_order) is not type(dict()):
151  title += ' ' + font[NATIONAL](current_character)
152  else:
153  for key,value in sorted(sort_order.items(), key=lambda x: x[1]):
154  if int(value) == int(sort_order[current_character]):
155  title += ' ' + font[VERNACULAR](key)
156  tex_file.write("\\section*{\\centering-" + handle_reserved(title) + " -}" + EOL)
157  #tex_file.write("\\pdfbookmark[1]{" + title + " }{" + title + " }" + EOL)
158  tex_file.write(lmf2tex(lexical_entry, font))
159  if len(paradigms) != 0:
160  tex_file.write(insert_references(lexical_entry))
161  tex_file.write("\\lhead{\\firstmark}" + EOL)
162  tex_file.write("\\rhead{\\botmark}" + EOL)
163  # Separate lexical entries from each others with a blank line
164  tex_file.write(EOL)
165  # Handle subentries
166  for related_form in lexical_entry.get_related_forms("subentry"):
167  if related_form.get_lexical_entry() is not None:
168  tex_file.write(lmf2tex(related_form.get_lexical_entry(), font))
169  if len(paradigms) != 0:
170  tex_file.write(insert_references(related_form.get_lexical_entry()))
171  # Separate sub-entries from each others with a blank line
172  tex_file.write(EOL)
173  except KeyError:
174  print Warning("Cannot sort item %s" % items(lexical_entry).encode(ENCODING))
175  except IndexError:
176  # Item is an empty string
177  pass
178  else:
179  raise OutputError(object, "Object to write must be a Lexical Resource.")
180  # Insert LaTeX commands to finish the document properly
181  tex_file.write("\end{multicols}" + EOL)
182  # Insert paradigms if any
183  for filename in paradigms:
184  tex_file.write(EOL)
185  tex_file.write("\\newpage" + EOL)
186  tex_file.write("\markboth{paradigms}{}" + EOL)
187  tex_file.write(file_read(filename))
188  tex_file.write(EOL)
189  # Insert other tables if any
190  for filename in tables:
191  tex_file.write(EOL)
192  tex_file.write("\\newpage" + EOL)
193  tex_file.write(file_read(filename))
194  tex_file.write(EOL)
195  tex_file.write("\end{document}" + EOL)
196  tex_file.close()
197 
198 ## Functions to process LaTeX layout
199 
200 def handle_font(text):
201  """Replace '{xxx}' by '\ipa{xxx}' in 'un', 'xn', 'gn', 'dn', 'en'.
202  """
203  import re
204  pattern = r"([^\\\|]*){([^}]*)}(.*)"
205  while re.match(pattern, text):
206  text = re.sub(pattern, r"\1" + r"\\ipa{" + r"\2" + "}" + r"\3", text)
207  return text
208 
209 def handle_reserved(text):
210  """ Handle reserved characters $ & % # _ ^ except \ { }.
211  """
212  if text.find("$") != -1:
213  text = text.replace('$', '\$')
214  # In some LaTeX commands, '$' must not be replaced by '\$' => marked as '\\dollar' in this case
215  if text.find("\\dollar") != -1:
216  text = text.replace("\\dollar", '$')
217  if text.find("& ") != -1:
218  text = text.replace('& ', '\& ')
219  if text.find("%") != -1:
220  text = text.replace('%', '\%')
221  if text.find("#") != -1:
222  text = text.replace('#', '\#')
223  if text.find("_") != -1:
224  text = text.replace('_', "\string_")
225  if text.find("^") != -1:
226  text = text.replace('^', "U+005E")
227  return text
228 
229 def handle_fi(text):
230  """Replace 'fi:xxx' and '|fi{xxx}' by \\textit{xxx}.
231  """
232  import re
233  if text.find("fi:") != -1:
234  pattern = r"(\w*)fi:([^\s\.,)]*)(\w*)"
235  text = re.sub(pattern, r"\1" + r"\\textit{" + r"\2" + "}" + r"\3", text)
236  if text.find("|fi{") != -1:
237  pattern = r"(\w*)\|fi{([^}]*)}(\w*)"
238  text = re.sub(pattern, r"\1" + r"\\textit{" + r"\2" + "}" + r"\3", text)
239  return text
240 
241 def handle_fv(text, font):
242  """Replace 'fv:xxx' and '|fv{xxx}' by font[VERNACULAR](xxx).
243  """
244  import re
245  if text.find("fv:") != -1:
246  #pattern = r"(.*[ }])?fv:([^\s\.,)]*)(.*)"
247  pattern = r"(\w*)fv:([^\s\.,)]*)(\w*)"
248  text = re.sub(pattern, r"\1" + r"%s" % font[VERNACULAR](r"\2").replace('\\', r'\\').replace(r'\\2', r"\2") + r"\3", text)
249  if text.find("|fv{") != -1:
250  pattern = r"(\w*)\|fv{([^}]*)}(\w*)"
251  text = re.sub(pattern, r"\1" + r"%s" % font[VERNACULAR](r"\2").replace('\\', r'\\').replace(r'\\2', r"\2") + r"\3", text)
252  return text
253 
254 def handle_fn(text, font):
255  """Replace 'fn:xxx' and '|fn{xxx}' by font[NATIONAL](xxx).
256  """
257  import re
258  if text.find("fn:") != -1:
259  pattern = r"(\w*)fn:([^\s\.,)]*)(\w*)"
260  text = re.sub(pattern, r"\1" + r"%s" % font[NATIONAL](r"\2").replace('\\', r'\\').replace(r'\\2', r"\2") + r"\3", text)
261  if text.find("|fn{") != -1:
262  pattern = r"(\w*)\|fn{([^}]*)}(\w*)"
263  text = re.sub(pattern, r"\1" + r"%s" % font[NATIONAL](r"\2").replace('\\', r'\\').replace(r'\\2', r"\2") + r"\3", text)
264  return text
265 
266 def handle_pinyin(text):
267  """Replace '@xxx' by '\\textcolor{gray}{xxx}' in 'lx', 'dv', 'xv' fields (already in API).
268  """
269  import re
270  if text.find("@") != -1:
271  text = re.sub(r"(\w*)@(\w*)", r"\1" + r"\\textcolor{gray}{" + r"\2" + "}", text)
272  return text
273 
274 def handle_caps(text):
275  """Handle small caps.
276  Replace '°xxx' by '\textsc{xxx}' in translated examples.
277  """
278  import re
279  if text.encode("utf8").find("°") != -1:
280  # LaTeX does not support '#' nor '_' characters inside '\mytextsc' command
281  text = re.sub(r"(\w*)°([^\s\.,)+/:\#\_]*)(\w*)", r"\1" + r"\\textsc{" + r"\2" + "}" + r"\3", text.encode("utf8")).decode("utf8")
282  return text
283 
284 def handle_quotes(text):
285  """Hanlde quotation marks.
286  Replace each "xxx" by ``xxx".
287  """
288  import re
289  pattern = r"""^([^\"]*)\"([^\"]*)\".*"""
290  result = re.match(pattern, text)
291  end = text
292  text = ""
293  while result:
294  text += result.group(1) + r"``" + result.group(2) + "\""
295  end = end.replace(result.group(1) + "\"" + result.group(2) + "\"", "")
296  result = re.match(pattern, end)
297  text += end
298  return text
299 
300 ## Functions to process LaTeX fields (output)
301 
302 def format_uid(lexical_entry, font):
303  """! @brief Transform unique identifier of a lexical entry in ASCII format.
304  @param lexical_entry The targeted Lexical Entry LMF instance.
305  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
306  @return A string representing the unique identifier in LaTeX format.
307  """
308  # LaTeX does not handle '\' (backslash), '{' (left brace) and '{' (right brace) characters in links
309  text = lexical_entry.get_id()
310  if text.find("\\") != -1:
311  text = text.replace('\\', u"£")
312  if text.find("{") != -1:
313  text = text.replace('{', '\{')
314  if text.find("}") != -1:
315  text = text.replace('}', '\}')
316  return text
317 
318 def format_link(lexical_entry, font):
319  """! @brief Display hyperlink to a lexical entry in LaTeX format.
320  @param lexical_entry The targeted Lexical Entry LMF instance.
321  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
322  @return A string representing the hyperlink in LaTeX format.
323  """
324  result = "\\hyperlink{" + format_uid(lexical_entry, font) + "}{" + font[VERNACULAR](lexical_entry.get_lexeme())
325  if lexical_entry.get_homonymNumber() is not None:
326  result += " \\textsubscript{" + str(lexical_entry.get_homonymNumber()) + "}"
327  result += "}"
328  return result
329 
330 def format_lexeme(lexical_entry, font):
331  """! @brief 'lx', 'hm' and 'lc' fields are flipped if 'lc' field has data.
332  @param lexical_entry The current Lexical Entry LMF instance.
333  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
334  @return A string representing lexeme in LaTeX format.
335  """
336  result = ""
337  lexeme = font[VERNACULAR](lexical_entry.get_lexeme())
338  if lexical_entry.is_subentry():
339  result += "\\subparagraph{\\dollar\\blacksquare\\dollar "
340  else:
341  result += "\\paragraph{\\hspace{-0.5cm} "
342  if lexical_entry.get_homonymNumber() is not None:
343  # Add homonym number to lexeme
344  lexeme += " \\textsubscript{" + str(lexical_entry.get_homonymNumber()) + "}"
345  if lexical_entry.get_contextual_variations() is not None and len(lexical_entry.get_contextual_variations()) != 0:
346  # Format contextual variations
347  for var in lexical_entry.get_contextual_variations():
348  result += " " + font[VERNACULAR](var)
349  result += " (from: " + lexeme + ")."
350  else:
351  # Format lexeme
352  result += lexeme
353  result += "} \\hypertarget{" + format_uid(lexical_entry, font) + "}{}" + EOL
354  if not lexical_entry.is_subentry():
355  result += "\markboth{" + lexeme + "}{}" + EOL
356  return result
357 
358 def format_audio(lexical_entry, font):
359  """! @brief Embed sound file into PDF.
360  @param lexical_entry The current Lexical Entry LMF instance.
361  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
362  @return A string embedding sound in LaTeX format.
363  """
364  import os
365  from os.path import basename, isfile
366  # To access options
367  from pylmflib import options
368  global options
369  result = ""
370  if not options.audio:
371  return result
372  for form_representation in lexical_entry.get_form_representations():
373  if form_representation.get_audio() is not None:
374  # Embed local sound file
375  # \includemedia[<options>]{<poster text>}{<main Flash (SWF) file or URL | 3D (PRC, U3D) file>}
376  # To include audio file in PDF, replace WAV extension by MP3 extension and search in audio, MP3 and WAV folders
377  file_name = form_representation.get_audio().get_fileName().replace(".wav", ".mp3")
378  file_path = []
379  if os.name == 'posix':
380  # Unix-style paths
381  file_path.append(config.xml.audio_path + file_name)
382  file_path.append(config.xml.audio_path + "mp3/" + file_name)
383  file_path.append(config.xml.audio_path + "wav/" + file_name)
384  else:
385  # Windows-style paths
386  audio_path = config.xml.audio_path.replace("/", "\\")
387  file_path.append(audio_path + file_name)
388  file_path.append(audio_path + "mp3\\" + file_name)
389  file_path.append(audio_path + "wav\\" + file_name)
390  exist = False
391  for audio_file in file_path:
392  if isfile(audio_file):
393  exist = True
394  break
395  if not exist:
396  print Warning("Sound file '%s' encountered for lexeme '%s' does not exist" % (file_name.encode(ENCODING), lexical_entry.get_lexeme().encode(ENCODING)))
397  return result
398  file_name = file_name.replace('-', '\string-')
399  result += "\includemedia[" + EOL +\
400  "\taddresource=" + file_name + "," + EOL +\
401  "\tflashvars={" + EOL +\
402  "\t\tsource=" + file_name + EOL +\
403  "\t\t&autoPlay=true" + EOL +\
404  "\t\t&autoRewind=true" + EOL +\
405  "\t\t&loop=false" + EOL +\
406  "\t\t&hideBar=true" + EOL +\
407  "\t\t&volume=1.0" + EOL +\
408  "\t\t&balance=0.0" + EOL +\
409  "}]{\includegraphics[scale=0.5]{sound.jpg}}{APlayer.swf}"
410  # \mediabutton[<options>]{<normal button text or graphic>}
411  result += " \\hspace{0.1cm}" + EOL
412  return result
413 
414 def format_part_of_speech(lexical_entry, font, mapping=partOfSpeech_tex, language=None):
415  """! @brief Display part of speech in LaTeX format.
416  @param lexical_entry The current Lexical Entry LMF instance.
417  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
418  @param mapping A Python dictionary giving the mapping between LMF part of speech LexicalEntry attribute value and LaTeX layout.
419  @param language Language to consider to display part of speech.
420  @return A string representing part of speech in LaTeX format.
421  """
422  result = ""
423  if lexical_entry.get_partOfSpeech() is not None:
424  try:
425  if language is None:
426  result += "\\textit{" + mapping[lexical_entry.get_partOfSpeech()] + "}. "
427  else:
428  result += "\\textit{" + mapping[(language, lexical_entry.get_partOfSpeech())] + "}. "
429  except KeyError:
430  print Warning("Part of speech value '%s' encountered for lexeme '%s' is not defined in configuration" % (lexical_entry.get_partOfSpeech().encode(ENCODING), lexical_entry.get_lexeme().encode(ENCODING)))
431  return result
432 
433 def format_definitions(sense, font, languages=None):
434  """! @brief Glosses are supplanted by definitions.
435  @param sense The current Sense LMF instance.
436  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
437  @param languages A list of languages to consider for definitions and glosses (all by default).
438  @return A string representing glosses and definitions in LaTeX format.
439  """
440  result = ""
441  if languages is None:
442  languages = [config.xml.vernacular, config.xml.English, config.xml.national, config.xml.regional]
443  for language in languages:
444  if len(sense.find_definitions(language)) != 0:
445  for definition in sense.find_definitions(language):
446  if language == config.xml.vernacular:
447  result += font[VERNACULAR](definition) + ". "
448  elif language == config.xml.national:
449  result += font[NATIONAL](handle_font(definition)) + ". "
450  elif language == config.xml.regional:
451  result += "\\textit{[Regnl: " + font[REGIONAL](definition) + "]}. "
452  else:
453  result += definition + ". "
454  elif len(sense.find_glosses(language)) != 0:
455  for gloss in sense.find_glosses(language):
456  if language == config.xml.vernacular:
457  result += font[VERNACULAR](gloss) + ". "
458  elif language == config.xml.national:
459  result += font[NATIONAL](handle_font(gloss)) + ". "
460  elif language == config.xml.regional:
461  result += "\\textit{[Regnl: " + font[REGIONAL](gloss) + "]}. "
462  else:
463  result += gloss + ". "
464  if len(sense.get_translations(language)) != 0:
465  for translation in sense.get_translations(language):
466  if language == config.xml.national:
467  result += font[NATIONAL](translation) + ". "
468  elif language == config.xml.regional:
469  result += "\\textbf{rr:}\\textit{[Regnl: " + translation + "]}. "
470  else:
471  result += translation + ". "
472  return result
473 
474 def format_lt(sense, font):
475  """! @brief Display 'lt' in LaTeX format.
476  @param sense The current Sense LMF instance.
477  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
478  @return A string representing 'lt' in LaTeX format.
479  """
480  # return "\\textit{Lit:} " + u"\u2018" + sense.get_lt() + u"\u2019" + ". "
481  return ""
482 
483 def format_sc(sense, font):
484  """! @brief Display 'sc' in LaTeX format.
485  @param sense The current Sense LMF instance.
486  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
487  @return A string representing 'sc' in LaTeX format.
488  """
489  # return "\\textit{\uline{" + sense.get_sc() + "}}. "
490  return ""
491 
492 def format_rf(sense, font):
493  """! @brief Display 'rf' in LaTeX format.
494  @param lexical_entry The current Sense LMF instance.
495  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
496  @return A string representing 'rf' in LaTeX format.
497  """
498  # return "\\textit{Ref:} " + sense.get_rf() + " "
499  return ""
500 
501 def format_examples(sense, font, languages=None):
502  """! @brief Display examples in LaTeX format.
503  @param sense The current Sense LMF instance.
504  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
505  @param languages A list of languages to consider for examples (all by default).
506  @return A string representing examples in LaTeX format.
507  """
508  result = ""
509  if languages is None:
510  languages = [config.xml.vernacular, config.xml.English, config.xml.national, config.xml.regional]
511  for context in sense.get_contexts():
512  tmp = ""
513  for language in languages:
514  for example in context.find_written_forms(language):
515  if language == config.xml.vernacular:
516  tmp += "\\sn " + font[VERNACULAR](example) + EOL
517  elif language == config.xml.national:
518  tmp += "\\trans \\textit{" + font[NATIONAL](handle_font(example)) + "}" + EOL
519  elif language == config.xml.regional:
520  tmp += "\\trans \\textit{[" + font[REGIONAL](example) + "]}" + EOL
521  else: # language == config.xml.English
522  tmp += "\\trans " + example + EOL
523  # LaTeX does not support empty examples
524  if len(tmp) != 0:
525  result += "\\begin{exe}" + EOL + tmp + "\\end{exe}" + EOL
526  return result
527 
528 def format_usage_notes(sense, font):
529  """! @brief Display usage notes in LaTeX format.
530  @param sense The current Sense LMF instance.
531  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
532  @return A string representing usage notes in LaTeX format.
533  """
534  result = ""
535  for usage in sense.find_usage_notes(language=config.xml.vernacular):
536  result += "\\textit{VerUsage:} " + font[VERNACULAR](usage) + " "
537  for usage in sense.find_usage_notes(language=config.xml.English):
538  result += "\\textit{Usage:} " + usage + " "
539  for usage in sense.find_usage_notes(language=config.xml.national):
540  result += "\\textit{" + font[NATIONAL](handle_font(usage)) + "} "
541  for usage in sense.find_usage_notes(language=config.xml.regional):
542  result += "\\textit{[" + font[REGIONAL](usage) + "]} "
543  return result
544 
546  """! @brief Display encyclopedic informations in LaTeX format.
547  @param sense The current Sense LMF instance.
548  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
549  @return A string representing encyclopedic informations in LaTeX format.
550  """
551  result = ""
552  for information in sense.find_encyclopedic_informations(language=config.xml.vernacular):
553  result += font[VERNACULAR](information) + " "
554  for information in sense.find_encyclopedic_informations(language=config.xml.English):
555  result += information + " "
556  for information in sense.find_encyclopedic_informations(language=config.xml.national):
557  result += font[NATIONAL](handle_font(information)) + " "
558  for information in sense.find_encyclopedic_informations(language=config.xml.regional):
559  result += "\\textit{[" + font[REGIONAL](information) + "]} "
560  return result
561 
562 def format_restrictions(sense, font):
563  """! @brief Display restrictions in LaTeX format.
564  @param sense The current Sense LMF instance.
565  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
566  @return A string representing restrictions in LaTeX format.
567  """
568  result = ""
569  for restriction in sense.find_restrictions(language=config.xml.vernacular):
570  result += "\\textit{VerRestrict:} " + font[VERNACULAR](restriction) + " "
571  for restriction in sense.find_restrictions(language=config.xml.English):
572  result += "\\textit{Restrict:} " + restriction + " "
573  for restriction in sense.find_restrictions(language=config.xml.national):
574  result += "\\textit{" + font[NATIONAL](restriction) + "} "
575  for restriction in sense.find_restrictions(language=config.xml.regional):
576  result += "\\textit{[" + font[REGIONAL](restriction) + "]} "
577  return result
578 
579 def format_lexical_functions(lexical_entry, font):
580  """! @brief Display lexical functions in LaTeX format.
581  @param lexical_entry The current Lexical Entry LMF instance.
582  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
583  @return A string representing lexical functions in LaTeX format.
584  """
585  result = ""
586  # result += "\\textit{" + lexical_entry.get_lf() + ": }"
587  # result += lexical_entry.get_le() + " "
588  # result += lexical_entry.get_ln() + " "
589  # result += lexical_entry.get_lr() + " "
590  return result
591 
592 def format_related_forms(lexical_entry, font):
593  """! @brief Display related forms in LaTeX format.
594  @param lexical_entry The current Lexical Entry LMF instance.
595  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
596  @return A string representing related forms in LaTeX format.
597  """
598  result = ""
599  for related_form in lexical_entry.get_related_forms(mdf_semanticRelation["sy"]):
600  result += "\\textit{Syn:} "
601  if related_form.get_lexical_entry() is not None:
602  result += format_link(related_form.get_lexical_entry(), font)
603  else:
604  result += font[VERNACULAR](related_form.get_lexeme())
605  result += ". "
606  for related_form in lexical_entry.get_related_forms(mdf_semanticRelation["an"]):
607  result += "\\textit{Ant:} "
608  if related_form.get_lexical_entry() is not None:
609  result += format_link(related_form.get_lexical_entry(), font)
610  else:
611  result += font[VERNACULAR](related_form.get_lexeme())
612  result += ". "
613  for morphology in lexical_entry.get_morphologies():
614  result += "\\textit{Morph:} " + font[VERNACULAR](morphology) + ". "
615  for related_form in lexical_entry.get_related_forms(mdf_semanticRelation["cf"]):
616  result += "\\textit{See:} "
617  if related_form.get_lexical_entry() is not None:
618  result += format_link(related_form.get_lexical_entry(), font)
619  else:
620  result += font[VERNACULAR](related_form.get_lexeme())
621  result += " "
622  # ce
623  # cn
624  # cr
625  # result += "\\textit{See main entry:} " + font[VERNACULAR](lexical_entry.get_mn()) + ". "
626  return result
627 
628 def format_variant_forms(lexical_entry, font):
629  """! @brief Display variant forms in LaTeX format.
630  @param lexical_entry The current Lexical Entry LMF instance.
631  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
632  @return A string representing variant forms in LaTeX format.
633  """
634  result = ""
635  for form_representation in lexical_entry.get_form_representations():
636  if form_representation.get_variantForm() is not None:
637  result += "\\textit{Variant:} " + font[VERNACULAR](form_representation.get_variantForm()) + " "
638  if form_representation.get_comment(config.xml.English) is not None:
639  result += "(" + form_representation.get_comment(config.xml.English) + ") "
640  if form_representation.get_comment(config.xml.national) is not None:
641  result += "(" + font[NATIONAL](form_representation.get_comment(config.xml.national)) + ") "
642  if form_representation.get_comment(config.xml.regional) is not None:
643  result += "(" + font[REGIONAL](form_representation.get_comment(config.xml.regional)) + ") "
644  return result
645 
646 def format_borrowed_word(lexical_entry, font):
647  """! @brief Display borrowed word in LaTeX format.
648  @param lexical_entry The current Lexical Entry LMF instance.
649  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
650  @return A string representing borrowed word in LaTeX format.
651  """
652  result = ""
653  if lexical_entry.get_borrowed_word() is not None:
654  result += "\\textit{From:} " + lexical_entry.get_borrowed_word()
655  if lexical_entry.get_written_form() is not None:
656  result += " " + lexical_entry.get_written_form()
657  result += ". "
658  return result
659 
660 def format_etymology(lexical_entry, font):
661  """! @brief Display etymology in LaTeX format.
662  @param lexical_entry The current Lexical Entry LMF instance.
663  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
664  @return A string representing etymology in LaTeX format.
665  """
666  result = ""
667  if lexical_entry.get_etymology() is not None:
668  result += "\\textit{Etym:} \\textbf{" + lexical_entry.get_etymology() + "} "
669  if lexical_entry.get_etymology_gloss() is not None:
670  result += u"\u2018" + lexical_entry.get_etymology_gloss() + u"\u2019" + ". "
671  return result
672 
673 def format_paradigms(lexical_entry, font):
674  """! @brief Display all paradigms in LaTeX format.
675  @param lexical_entry The current Lexical Entry LMF instance.
676  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
677  @return A string representing all paradigms in LaTeX format.
678  """
679  result = ""
680  for paradigm in lexical_entry.find_paradigms():
681  result += "\\textit{Prdm:} \\textbf{" + paradigm + "}. "
682  for paradigm in lexical_entry.find_paradigms(grammatical_number=pd_grammaticalNumber["sg"]):
683  result += "\\textit{Sg:} \\textbf{" + paradigm + "} "
684  for paradigm in lexical_entry.find_paradigms(grammatical_number=pd_grammaticalNumber["pl"]):
685  result += "\\textit{Pl:} \\textbf{" + paradigm + "} "
686  # result += "\\textit{Redup:} \\textbf{" + lexical_entry.get_rd() + "} "
687  for paradigm in lexical_entry.find_paradigms(person=pd_person[1], grammatical_number=pd_grammaticalNumber['s']):
688  result += "\\textit{1s:} \\textbf{" + paradigm + "} "
689  for paradigm in lexical_entry.find_paradigms(person=pd_person[2], grammatical_number=pd_grammaticalNumber['s']):
690  result += "\\textit{2s:} \\textbf{" + paradigm + "} "
691  for paradigm in lexical_entry.find_paradigms(person=pd_person[3], grammatical_number=pd_grammaticalNumber['s']):
692  result += "\\textit{3s:} \\textbf{" + paradigm + "} "
693  for paradigm in lexical_entry.find_paradigms(anymacy=pd_anymacy[4], grammatical_number=pd_grammaticalNumber['s']):
694  result += "\\textit{3sn:} \\textbf{" + paradigm + "} "
695  for paradigm in lexical_entry.find_paradigms(person=pd_person[1], grammatical_number=pd_grammaticalNumber['d']):
696  result += "\\textit{1d:} \\textbf{" + paradigm + "} "
697  for paradigm in lexical_entry.find_paradigms(person=pd_person[2], grammatical_number=pd_grammaticalNumber['d']):
698  result += "\\textit{2d:} \\textbf{" + paradigm + "} "
699  for paradigm in lexical_entry.find_paradigms(person=pd_person[3], grammatical_number=pd_grammaticalNumber['d']):
700  result += "\\textit{3d:} \\textbf{" + paradigm + "} "
701  for paradigm in lexical_entry.find_paradigms(anymacy=pd_anymacy[4], grammatical_number=pd_grammaticalNumber['d']):
702  result += "\\textit{3dn:} \\textbf{" + paradigm + "} "
703  for paradigm in lexical_entry.find_paradigms(person=pd_person[1], grammatical_number=pd_grammaticalNumber['p']):
704  result += "\\textit{1p:} \\textbf{" + paradigm + "} "
705  for paradigm in lexical_entry.find_paradigms(person=pd_person[1], grammatical_number=pd_grammaticalNumber['p'], clusivity=pd_clusivity['e']):
706  result += "\\textit{1px:} \\textbf{" + paradigm + "} "
707  for paradigm in lexical_entry.find_paradigms(person=pd_person[1], grammatical_number=pd_grammaticalNumber['p'], clusivity=pd_clusivity['i']):
708  result += "\\textit{1pi:} \\textbf{" + paradigm + "} "
709  for paradigm in lexical_entry.find_paradigms(person=pd_person[2], grammatical_number=pd_grammaticalNumber['p']):
710  result += "\\textit{2p:} \\textbf{" + paradigm + "} "
711  for paradigm in lexical_entry.find_paradigms(person=pd_person[3], grammatical_number=pd_grammaticalNumber['p']):
712  result += "\\textit{3p:} \\textbf{" + paradigm + "} "
713  for paradigm in lexical_entry.find_paradigms(anymacy=pd_anymacy[4], grammatical_number=pd_grammaticalNumber['p']):
714  result += "\\textit{3pn:} \\textbf{" + paradigm + "} "
715  return result
716 
717 def format_table(lexical_entry, font):
718  """! @brief Display a table in LaTeX format.
719  @param lexical_entry The current Lexical Entry LMF instance.
720  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
721  @return A string representing a table in LaTeX format.
722  """
723  return ""
724 
725 def format_semantic_domains(lexical_entry, font):
726  """! @brief Display semantic domains in LaTeX format.
727  @param lexical_entry The current Lexical Entry LMF instance.
728  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
729  @return A string representing semantic domains in LaTeX format.
730  """
731  result = ""
732  for semantic_domain in lexical_entry.get_semantic_domains():
733  result += "\\textit{SD:} " + semantic_domain + ". "
734  # is
735  # result += "\\textit{Semantics:} " + lexical_entry.get_is() + ". "
736  # th
737  # result += "\\textit{Thes:} " + lexical_entry.get_th() + ". "
738  return result
739 
740 def format_bibliography(lexical_entry, font):
741  """! @brief Display bibliography in LaTeX format.
742  @param lexical_entry The current Lexical Entry LMF instance.
743  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
744  @return A string representing bibliography in LaTeX format.
745  """
746  result = ""
747  if lexical_entry.get_bibliography() is not None:
748  result += "\\textit{Read:} " + lexical_entry.get_bibliography() + ". "
749  return result
750 
751 def format_picture(lexical_entry, font):
752  """! @brief Display a picture in LaTeX format.
753  @param lexical_entry The current Lexical Entry LMF instance.
754  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
755  @return A string representing a picture in LaTeX format.
756  """
757  # return "(" + lexical_entry.get_pc() + ") "
758  return ""
759 
760 def format_notes(lexical_entry, font):
761  """! @brief Display all notes in LaTeX format.
762  @param lexical_entry The current Lexical Entry LMF instance.
763  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
764  @return A string representing all notes in LaTeX format.
765  """
766  result = ""
767  for note in lexical_entry.find_notes(type="general"):
768  result += "\\textit{[Note: " + note + "]} "
769  for note in lexical_entry.find_notes(type="phonology"):
770  result += "\\textit{[Phon: " + note + "]} "
771  for note in lexical_entry.find_notes(type="grammar"):
772  result += "\\textit{[Gram: " + note + "]} "
773  for note in lexical_entry.find_notes(type="discourse"):
774  result += "\\textit{[Disc: " + note + "]} "
775  for note in lexical_entry.find_notes(type="anthropology"):
776  result += "\\textit{[Ant: " + note + "]} "
777  for note in lexical_entry.find_notes(type="sociolinguistics"):
778  result += "\\textit{[Socio: " + note + "]} "
779  for note in lexical_entry.find_notes(type="question"):
780  result += "\\textit{[Ques: " + note + "]} "
781  return result
782 
783 def format_source(lexical_entry, font):
784  """! @brief Display source in LaTeX format.
785  @param lexical_entry The current Lexical Entry LMF instance.
786  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
787  @return A string representing source in LaTeX format.
788  """
789  # return "\\textit{Source:} " + lexical_entry.get_so() + ". "
790  return ""
791 
792 def format_status(lexical_entry, font):
793  """! @brief Display status in LaTeX format.
794  @param lexical_entry The current Lexical Entry LMF instance.
795  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
796  @return A string representing status in LaTeX format.
797  """
798  result = ""
799  if lexical_entry.get_status() is not None:
800  result += "\\textit{Status:} " + lexical_entry.get_status()
801  return result
802 
803 def format_date(lexical_entry, font):
804  """! @brief Do not display date in LaTeX format.
805  @param lexical_entry The current Lexical Entry LMF instance.
806  @param font A Python dictionary giving the vernacular, national, regional fonts to apply to a text in LaTeX format.
807  @return An empty string.
808  """
809  return ""
def format_notes
Display all notes in LaTeX format.
Definition: tex.py:760
def format_audio
Embed sound file into PDF.
Definition: tex.py:358
def format_restrictions
Display restrictions in LaTeX format.
Definition: tex.py:562
def format_variant_forms
Display variant forms in LaTeX format.
Definition: tex.py:628
def format_link
Display hyperlink to a lexical entry in LaTeX format.
Definition: tex.py:318
def format_status
Display status in LaTeX format.
Definition: tex.py:792
def open_read
Open file in read mode (automatically decode file in unicode).
Definition: io.py:36
def format_bibliography
Display bibliography in LaTeX format.
Definition: tex.py:740
def format_lexical_functions
Display lexical functions in LaTeX format.
Definition: tex.py:579
def file_read
Read file contents.
Definition: tex.py:16
def format_picture
Display a picture in LaTeX format.
Definition: tex.py:751
def format_etymology
Display etymology in LaTeX format.
Definition: tex.py:660
def format_uid
Functions to process LaTeX fields (output)
Definition: tex.py:302
def format_lexeme
'lx', 'hm' and 'lc' fields are flipped if 'lc' field has data.
Definition: tex.py:330
def format_usage_notes
Display usage notes in LaTeX format.
Definition: tex.py:528
def format_examples
Display examples in LaTeX format.
Definition: tex.py:501
def format_encyclopedic_informations
Display encyclopedic informations in LaTeX format.
Definition: tex.py:545
def format_related_forms
Display related forms in LaTeX format.
Definition: tex.py:592
def format_paradigms
Display all paradigms in LaTeX format.
Definition: tex.py:673
def open_write
Open file in write mode (automatically decode file in unicode).
Definition: io.py:47
def insert_references
Insert references to paradigms.
Definition: tex.py:28
def format_source
Display source in LaTeX format.
Definition: tex.py:783
def format_rf
Display 'rf' in LaTeX format.
Definition: tex.py:492
def tex_write
Write a LaTeX file.
Definition: tex.py:56
def handle_font
Functions to process LaTeX layout.
Definition: tex.py:200
def format_part_of_speech
Display part of speech in LaTeX format.
Definition: tex.py:414
def format_date
Do not display date in LaTeX format.
Definition: tex.py:803
def format_definitions
Glosses are supplanted by definitions.
Definition: tex.py:433
def format_semantic_domains
Display semantic domains in LaTeX format.
Definition: tex.py:725
def format_lt
Display 'lt' in LaTeX format.
Definition: tex.py:474
def format_table
Display a table in LaTeX format.
Definition: tex.py:717
def format_sc
Display 'sc' in LaTeX format.
Definition: tex.py:483
def format_borrowed_word
Display borrowed word in LaTeX format.
Definition: tex.py:646