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

Function table

pattern/text/tree.py:1582–1623  ·  view source on GitHub ↗

Returns a string where the tags of tokens in the sentence are organized in outlined columns.

(sentence, fill=1, placeholder="-")

Source from the content-addressed store, hash-verified

1580### STDOUT TABLE ###################################################################################
1581
1582def table(sentence, fill=1, placeholder="-"):
1583 """ Returns a string where the tags of tokens in the sentence are organized in outlined columns.
1584 """
1585 tags = [WORD, POS, IOB, CHUNK, ROLE, REL, PNP, ANCHOR, LEMMA]
1586 tags += [tag for tag in sentence.token if tag not in tags]
1587 def format(token, tag):
1588 # Returns the token tag as a string.
1589 if tag == WORD : s = token.string
1590 elif tag == POS : s = token.type
1591 elif tag == IOB : s = token.chunk and (token.index == token.chunk.start and "B" or "I")
1592 elif tag == CHUNK : s = token.chunk and token.chunk.type
1593 elif tag == ROLE : s = token.chunk and token.chunk.role
1594 elif tag == REL : s = token.chunk and token.chunk.relation and str(token.chunk.relation)
1595 elif tag == PNP : s = token.chunk and token.chunk.pnp and token.chunk.pnp.type
1596 elif tag == ANCHOR : s = token.chunk and token.chunk.anchor_id
1597 elif tag == LEMMA : s = token.lemma
1598 else : s = token.custom_tags.get(tag)
1599 return s or placeholder
1600 def outline(column, fill=1, padding=3, align="left"):
1601 # Add spaces to each string in the column so they line out to the highest width.
1602 n = max([len(x) for x in column]+[fill])
1603 if align == "left" : return [x+" "*(n-len(x))+" "*padding for x in column]
1604 if align == "right" : return [" "*(n-len(x))+x+" "*padding for x in column]
1605
1606 # Gather the tags of the tokens in the sentece per column.
1607 # If the IOB-tag is I-, mark the chunk tag with "^".
1608 # Add the tag names as headers in each column.
1609 columns = [[format(token, tag) for token in sentence] for tag in tags]
1610 columns[3] = [columns[3][i]+(iob == "I" and " ^" or "") for i, iob in enumerate(columns[2])]
1611 del columns[2]
1612 for i, header in enumerate(['word', 'tag', 'chunk', 'role', 'id', 'pnp', 'anchor', 'lemma']+tags[9:]):
1613 columns[i].insert(0, "")
1614 columns[i].insert(0, header.upper())
1615 # The left column (the word itself) is outlined to the right,
1616 # and has extra spacing so that words across sentences line out nicely below each other.
1617 for i, column in enumerate(columns):
1618 columns[i] = outline(column, fill+10*(i==0), align=("left","right")[i==0])
1619 # Anchor column is useful in MBSP but not in pattern.en.
1620 if not MBSP:
1621 del columns[6]
1622 # Create a string with one row (i.e., one token) per line.
1623 return "\n".join(["".join([x[i] for x in columns]) for i in range(len(columns[0]))])
1624

Callers 1

pprintFunction · 0.90

Calls 4

formatFunction · 0.85
outlineFunction · 0.85
lenFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…