Python LMF library
 All Classes Namespaces Files Functions Variables
xml_lmf.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """! @package input
4 """
5 
6 from utils.xml_format import parse_xml
7 from utils.error_handling import InputError
8 
9 def compute_name(object_name):
10  """! @brief Compute attribute/module name from object name as follows: 'ObjectName' attribute/module name is 'object_name'.
11  @param object_name String containing name of the object, e.g. 'LexicalEntry'.
12  @return The corresponding attribute/module name, e.g. 'lexical_entry'.
13  """
14  name = ''
15  for c in object_name:
16  # Detect first letter of a word: it is an uppercase letter
17  if c.isupper():
18  # Add an underscore separator between words if needed
19  if name != '':
20  name += '_'
21  name += c.lower()
22  return name
23 
24 def factory(object_name, attributes):
25  """! @brief This function is an object factory. Indeed, from an object name and its attributes, it creates a Python object and sets its attributes.
26  @param object_name A Python string containing the object name, for instance 'LexicalEntry'.
27  @param attributes A Python dictionary containing pairs of attribute name (as a Python string) and value, for instance {'partOfSpeech': 'n'}.
28  """
29  # Compute module name from object name
30  module_name = compute_name(object_name)
31  # Find the package in which the object class is defined, in order to be able to import the correct Python module
32  import sys, os, glob
33  running_path = sys.path[0]
34  if os.name == 'posix':
35  # Unix-style path
36  separator = '/'
37  else:
38  # Windows-style path
39  separator = '\\'
40  full_path = glob.glob(running_path + separator + ".." + separator + ".." + separator + "pylmflib" + separator + "*" + separator + module_name + ".py")
41  if len(full_path) < 1:
42  # No file with this name exists
43  raise InputError(module_name + ".py", "No file named '%s' exists in the library. It is not allowed, so please solve this issue by renaming files correctly." % (module_name + ".py"))
44  elif len(full_path) > 1:
45  # Several files with this name exist
46  raise InputError(module_name + ".py", "Several files named '%s' exist in the library. It is not allowed, so please solve this issue by renaming files correctly. Here is the list of found files with this name: %s" % ((module_name + ".py"), str(full_path)))
47  # Retrieve package name from full path
48  package_name = full_path[0].split(separator)[-2]
49  # Import object module: "package.module"
50  object_module = __import__(package_name + "." + module_name)
51  # Retrieve object class from module
52  object_class = getattr(object_module, object_name)
53  # Create an instance of it
54  instance = object_class()
55  # Set class attributes
56  for attribute in attributes.iteritems():
57  setattr(instance, attribute[0], attribute[1])
58  return instance
59 
60 def xml_lmf_read(filename):
61  """! @brief Read an XML LMF file.
62  @param filename The name of the XML LMF file to read with full path, for instance 'user/input.xml'.
63  @return A Lexical Resource instance containing all lexicons.
64  """
65  root = parse_xml(filename)
66  # Create an object instance corresponding to the XML root element
67  root_instance = factory(root.tag, root.attrib)
68  # Parse XML sub-elements and create instance childs
69  get_sub_elements(root_instance, root)
70  return root_instance
71 
72 def get_sub_elements(instance, element):
73  """! @brief This function recursively parses the given XML element and creates corresponding LMF instances with their attributes.
74  @param instance An LMF object instance.
75  @param element An XML element.
76  """
77  for sub_element in element:
78  # XML elements "feat" are modelized by LMF class attributes
79  if sub_element.tag == "feat":
80  # "feat" elements have 2 XML attributes: one for LMF attribute name ("att"), a second for LMF attribute value ("val")
81  setattr(instance, sub_element.attrib["att"], sub_element.attrib["val"])
82  elif sub_element.tag == "a":
83  # "a" elements are HTML links => do not consider them
84  pass
85  else:
86  # Create LMF instances corresponding to XML sub-elements
87  sub_instance = factory(sub_element.tag, sub_element.attrib)
88  # Root LMF object must own the child objects
89  attr_name = compute_name(sub_element.tag)
90  attr_value = getattr(instance, attr_name)
91  if type(attr_value) is list:
92  # If this attribute is a list, append the new value to the list
93  attr_value.append(sub_instance)
94  else:
95  # Simply set the value
96  setattr(instance, attr_name, sub_instance)
97  # Repeat the same operation recursively
98  get_sub_elements(sub_instance, sub_element)
def xml_lmf_read
Read an XML LMF file.
Definition: xml_lmf.py:60
def get_sub_elements
This function recursively parses the given XML element and creates corresponding LMF instances with t...
Definition: xml_lmf.py:72
def factory
This function is an object factory.
Definition: xml_lmf.py:24
def parse_xml
Parse an XML file.
Definition: xml_format.py:35
def compute_name
Compute attribute/module name from object name as follows: 'ObjectName' attribute/module name is 'obj...
Definition: xml_lmf.py:9