Returns a slash-formatted string from the given XML representation. The return value is a TokenString (for MBSP) or TaggedString (for Pattern).
(xml)
| 1344 | return s |
| 1345 | |
| 1346 | def parse_string(xml): |
| 1347 | """ Returns a slash-formatted string from the given XML representation. |
| 1348 | The return value is a TokenString (for MBSP) or TaggedString (for Pattern). |
| 1349 | """ |
| 1350 | string = "" |
| 1351 | # Traverse all the <sentence> elements in the XML. |
| 1352 | dom = XML(xml) |
| 1353 | for sentence in dom(XML_SENTENCE): |
| 1354 | _anchors.clear() # Populated by calling _parse_tokens(). |
| 1355 | _attachments.clear() # Populated by calling _parse_tokens(). |
| 1356 | # Parse the language from <sentence language="">. |
| 1357 | language = sentence.get(XML_LANGUAGE, "en") |
| 1358 | # Parse the token tag format from <sentence token="">. |
| 1359 | # This information is returned in TokenString.tags, |
| 1360 | # so the format and order of the token tags is retained when exporting/importing as XML. |
| 1361 | format = sentence.get(XML_TOKEN, [WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA]) |
| 1362 | format = not isinstance(format, basestring) and format or format.replace(" ","").split(",") |
| 1363 | # Traverse all <chunk> and <chink> elements in the sentence. |
| 1364 | # Find the <word> elements inside and create tokens. |
| 1365 | tokens = [] |
| 1366 | for chunk in sentence: |
| 1367 | tokens.extend(_parse_tokens(chunk, format)) |
| 1368 | # Attach PNP's to their anchors. |
| 1369 | # Keys in _anchors have linked anchor chunks (each chunk is a list of tokens). |
| 1370 | # The keys correspond to the keys in _attachments, which have linked PNP chunks. |
| 1371 | if ANCHOR in format: |
| 1372 | A, P, a, i = _anchors, _attachments, 1, format.index(ANCHOR) |
| 1373 | for id in sorted(A.keys()): |
| 1374 | for token in A[id]: |
| 1375 | token[i] += "-"+"-".join(["A"+str(a+p) for p in range(len(P[id]))]) |
| 1376 | token[i] = token[i].strip("O-") |
| 1377 | for p, pnp in enumerate(P[id]): |
| 1378 | for token in pnp: |
| 1379 | token[i] += "-"+"P"+str(a+p) |
| 1380 | token[i] = token[i].strip("O-") |
| 1381 | a += len(P[id]) |
| 1382 | # Collapse the tokens to string. |
| 1383 | # Separate multiple sentences with a new line. |
| 1384 | tokens = ["/".join([tag for tag in token]) for token in tokens] |
| 1385 | tokens = " ".join(tokens) |
| 1386 | string += tokens + "\n" |
| 1387 | # Return a TokenString, which is a unicode string that transforms easily |
| 1388 | # into a plain str, a list of tokens, or a Sentence. |
| 1389 | try: |
| 1390 | if MBSP: from mbsp import TokenString |
| 1391 | return TokenString(string.strip(), tags=format, language=language) |
| 1392 | except: |
| 1393 | return TaggedString(string.strip(), tags=format, language=language) |
| 1394 | |
| 1395 | def _parse_tokens(chunk, format=[WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA]): |
| 1396 | """ Parses tokens from <word> elements in the given XML <chunk> element. |
no test coverage detected
searching dependent graphs…