MCPcopy Index your code
hub / github.com/clips/pattern / _parse_tokens

Function _parse_tokens

pattern/text/tree.py:1395–1440  ·  view source on GitHub ↗

Parses tokens from elements in the given XML element. Returns a flat list of tokens, in which each token is [WORD, POS, CHUNK, PNP, RELATION, ANCHOR, LEMMA]. If a is encountered, traverses all of the chunks in the PNP.

(chunk, format=[WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA])

Source from the content-addressed store, hash-verified

1393 return TaggedString(string.strip(), tags=format, language=language)
1394
1395def _parse_tokens(chunk, format=[WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA]):
1396 """ Parses tokens from <word> elements in the given XML <chunk> element.
1397 Returns a flat list of tokens, in which each token is [WORD, POS, CHUNK, PNP, RELATION, ANCHOR, LEMMA].
1398 If a <chunk type="PNP"> is encountered, traverses all of the chunks in the PNP.
1399 """
1400 tokens = []
1401 # Only process <chunk> and <chink> elements,
1402 # text nodes in between return an empty list.
1403 if not (chunk.tag == XML_CHUNK or chunk.tag == XML_CHINK):
1404 return []
1405 type = chunk.get(XML_TYPE, "O")
1406 if type == "PNP":
1407 # For, <chunk type="PNP">, recurse all the child chunks inside the PNP.
1408 for ch in chunk:
1409 tokens.extend(_parse_tokens(ch, format))
1410 # Tag each of them as part of the PNP.
1411 if PNP in format:
1412 i = format.index(PNP)
1413 for j, token in enumerate(tokens):
1414 token[i] = (j==0 and "B-" or "I-") + "PNP"
1415 # Store attachments so we can construct anchor id's in parse_string().
1416 # This has to be done at the end, when all the chunks have been found.
1417 a = chunk.get(XML_OF).split(_UID_SEPARATOR)[-1]
1418 if a:
1419 _attachments.setdefault(a, [])
1420 _attachments[a].append(tokens)
1421 return tokens
1422 # For <chunk type-"VP" id="1">, the relation is VP-1.
1423 # For <chunk type="NP" relation="OBJ" of="1">, the relation is NP-OBJ-1.
1424 relation = _parse_relation(chunk, type)
1425 # Process all of the <word> elements in the chunk, for example:
1426 # <word type="NN" lemma="pizza">pizza</word> => [pizza, NN, I-NP, O, NP-OBJ-1, O, pizza]
1427 for word in filter(lambda n: n.tag == XML_WORD, chunk):
1428 tokens.append(_parse_token(word, chunk=type, relation=relation, format=format))
1429 # Add the IOB chunk tags:
1430 # words at the start of a chunk are marked with B-, words inside with I-.
1431 if CHUNK in format:
1432 i = format.index(CHUNK)
1433 for j, token in enumerate(tokens):
1434 token[i] = token[i] != "O" and ((j==0 and "B-" or "I-") + token[i]) or "O"
1435 # The chunk can be the anchor of one or more PNP chunks.
1436 # Store anchors so we can construct anchor id's in parse_string().
1437 a = chunk.get(XML_ANCHOR, "").split(_UID_SEPARATOR)[-1]
1438 if a:
1439 _anchors[a] = tokens
1440 return tokens
1441
1442def _parse_relation(chunk, type="O"):
1443 """ Returns a string of the roles and relations parsed from the given <chunk> element.

Callers 1

parse_stringFunction · 0.85

Calls 9

_parse_relationFunction · 0.85
_parse_tokenFunction · 0.85
filterFunction · 0.50
getMethod · 0.45
extendMethod · 0.45
indexMethod · 0.45
splitMethod · 0.45
setdefaultMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…