6 from utils.xml_format
import parse_xml
7 from utils.error_handling
import InputError
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'.
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'}.
33 running_path = sys.path[0]
34 if os.name ==
'posix':
40 full_path = glob.glob(running_path + separator +
".." + separator +
".." + separator +
"pylmflib" + separator +
"*" + separator + module_name +
".py")
41 if len(full_path) < 1:
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:
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)))
48 package_name = full_path[0].split(separator)[-2]
50 object_module = __import__(package_name +
"." + module_name)
52 object_class = getattr(object_module, object_name)
54 instance = object_class()
56 for attribute
in attributes.iteritems():
57 setattr(instance, attribute[0], attribute[1])
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.
67 root_instance =
factory(root.tag, root.attrib)
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.
77 for sub_element
in element:
79 if sub_element.tag ==
"feat":
81 setattr(instance, sub_element.attrib[
"att"], sub_element.attrib[
"val"])
82 elif sub_element.tag ==
"a":
87 sub_instance =
factory(sub_element.tag, sub_element.attrib)
90 attr_value = getattr(instance, attr_name)
91 if type(attr_value)
is list:
93 attr_value.append(sub_instance)
96 setattr(instance, attr_name, sub_instance)