Returns the given Sentence object as an XML-string (plain bytestring, UTF-8 encoded). The tab delimiter is used as indendation for nested elements. The id can be used as a unique identifier per sentence for chunk id's and anchors. For example: "I eat pizza with a fork." =>
(sentence, tab="\t", id="")
| 1183 | _UID_SEPARATOR = "." |
| 1184 | |
| 1185 | def parse_xml(sentence, tab="\t", id=""): |
| 1186 | """ Returns the given Sentence object as an XML-string (plain bytestring, UTF-8 encoded). |
| 1187 | The tab delimiter is used as indendation for nested elements. |
| 1188 | The id can be used as a unique identifier per sentence for chunk id's and anchors. |
| 1189 | For example: "I eat pizza with a fork." => |
| 1190 | |
| 1191 | <sentence token="word, part-of-speech, chunk, preposition, relation, anchor, lemma" language="en"> |
| 1192 | <chunk type="NP" relation="SBJ" of="1"> |
| 1193 | <word type="PRP" lemma="i">I</word> |
| 1194 | </chunk> |
| 1195 | <chunk type="VP" relation="VP" id="1" anchor="A1"> |
| 1196 | <word type="VBP" lemma="eat">eat</word> |
| 1197 | </chunk> |
| 1198 | <chunk type="NP" relation="OBJ" of="1"> |
| 1199 | <word type="NN" lemma="pizza">pizza</word> |
| 1200 | </chunk> |
| 1201 | <chunk type="PNP" of="A1"> |
| 1202 | <chunk type="PP"> |
| 1203 | <word type="IN" lemma="with">with</word> |
| 1204 | </chunk> |
| 1205 | <chunk type="NP"> |
| 1206 | <word type="DT" lemma="a">a</word> |
| 1207 | <word type="NN" lemma="fork">fork</word> |
| 1208 | </chunk> |
| 1209 | </chunk> |
| 1210 | <chink> |
| 1211 | <word type="." lemma=".">.</word> |
| 1212 | </chink> |
| 1213 | </sentence> |
| 1214 | """ |
| 1215 | uid = lambda *parts: "".join([str(id), _UID_SEPARATOR ]+[str(x) for x in parts]).lstrip(_UID_SEPARATOR) |
| 1216 | push = lambda indent: indent+tab # push() increases the indentation. |
| 1217 | pop = lambda indent: indent[:-len(tab)] # pop() decreases the indentation. |
| 1218 | indent = tab |
| 1219 | xml = [] |
| 1220 | # Start the sentence element: |
| 1221 | # <sentence token="word, part-of-speech, chunk, preposition, relation, anchor, lemma"> |
| 1222 | xml.append('<%s%s %s="%s" %s="%s">' % ( |
| 1223 | XML_SENTENCE, |
| 1224 | XML_ID and " %s=\"%s\"" % (XML_ID, str(id)) or "", |
| 1225 | XML_TOKEN, ", ".join(sentence.token), |
| 1226 | XML_LANGUAGE, sentence.language |
| 1227 | )) |
| 1228 | # Collect chunks that are PNP anchors and assign id. |
| 1229 | anchors = {} |
| 1230 | for chunk in sentence.chunks: |
| 1231 | if chunk.attachments: |
| 1232 | anchors[chunk.start] = len(anchors) + 1 |
| 1233 | # Traverse all words in the sentence. |
| 1234 | for word in sentence.words: |
| 1235 | chunk = word.chunk |
| 1236 | pnp = word.chunk and word.chunk.pnp or None |
| 1237 | # Start the PNP element if the chunk is the first chunk in PNP: |
| 1238 | # <chunk type="PNP" of="A1"> |
| 1239 | if pnp and pnp.start == chunk.start: |
| 1240 | a = pnp.anchor and ' %s="%s"' % (XML_OF, uid("A", anchors.get(pnp.anchor.start, ""))) or "" |
| 1241 | xml.append(indent + '<%s %s="PNP"%s>' % (XML_CHUNK, XML_TYPE, a)) |
| 1242 | indent = push(indent) |
no test coverage detected
searching dependent graphs…