1 """
2 @author: Eleftherios Avramidis
3 """
4
5 from xml.dom import minidom
6 from sentence.parallelsentence import ParallelSentence
7 from xml.sax.saxutils import escape
8 from sentence.dataset import DataSet
9
10
13 raise NotImplementedError( "Should have implemented this" )
14
16 raise NotImplementedError( "Should have implemented this" )
17
19 raise NotImplementedError( "Should have implemented this" )
20
21
24 """
25 Constructor
26 """
27 if isinstance (data, minidom.Document):
28 self.object_xml = data
29 elif isinstance(data, list):
30 self.object_xml = self.get_document_xml(data)
31 elif isinstance(data, DataSet):
32 self.object_xml = self.get_document_xml(data.get_parallelsentences())
33 else:
34 pass
35
37 raise NotImplementedError( "Should have implemented this" )
38
39
42
44 """
45 Creates an XML for the document an populates that with the (parallel) sentences of the given object.
46 Resulting XML object gets stored as a variable.
47 @param parallelsentences: a list of ParallelSentence objects
48 """
49 doc_xml = minidom.Document( )
50 jcml = doc_xml.createElement("jcml")
51
52 i=0
53
54
55 for ps in parallelsentences:
56 parallelsentence_xml = self.get_parallelsentence_xml(ps, doc_xml)
57 jcml.appendChild(parallelsentence_xml)
58
59
60 i += 1
61
62 doc_xml.appendChild(jcml)
63 return doc_xml
64
65
67 file_object = open(filename, 'w')
68
69 prettyxml = self.object_xml.toprettyxml("\t","\n", "utf-8")
70 file_object.write(prettyxml)
71
72
73 file_object.close()
74
75
76
77
79 """
80 classdocs
81 """
82
83
84
85
86
87
88
115
116
117
118
119
120
121
122
124 """
125 Helper function that fetches the text and the attributes of a sentence
126 and wraps them up into a minidom XML sentenceect
127 """
128
129 sentence_xml = doc_xml.createElement(tag)
130
131 for attribute_key in sentence.get_attributes().keys():
132
133
134
135 sentence_xml.setAttribute(attribute_key, escape(sentence.get_attribute(attribute_key)))
136
137
138
139 textnode = escape(sentence.get_string().strip())
140 sentence_xml.appendChild(doc_xml.createTextNode(textnode))
141
142 return sentence_xml
143