udapi.block.write.textmodetrees module

An ASCII pretty printer of dependency trees.

class udapi.block.write.textmodetrees.TextModeTrees(print_sent_id=True, print_text=True, add_empty_line=True, indent=1, minimize_cross=True, color='auto', attributes='form,upos,deprel', print_undef_as='_', print_doc_meta=True, print_comments=False, print_empty=True, mark='(ToDo|ToDoOrigText|Bug|Mark)', marked_only=False, hints=True, layout='classic', **kwargs)[source]

Bases: BaseWriter

An ASCII pretty printer of dependency trees.

# from the command line (visualize CoNLL-U files)
udapy write.TextModeTrees color=1 < file.conllu | less -R

In scenario (examples of other parameters):

write.TextModeTrees indent=2 print_sent_id=0 print_sentence=1 layout=align
write.TextModeTrees zones=en,cs attributes=form,lemma,upos minimize_cross=0

This block prints dependency trees in plain-text format. For example the following CoNLL-U file (with tabs instead of spaces):

1  I     I     PRON  PRP Number=Sing|Person=1 2  nsubj     _ _
2  saw   see   VERB  VBD Tense=Past           0  root      _ _
3  a     a     DET   DT  Definite=Ind         4  det       _ _
4  dog   dog   NOUN  NN  Number=Sing          2  dobj      _ _
5  today today NOUN  NN  Number=Sing          2  nmod:tmod _ SpaceAfter=No
6  ,     ,     PUNCT ,   _                    2  punct     _ _
7  which which DET   WDT PronType=Rel         10 nsubj     _ _
8  was   be    VERB  VBD Person=3|Tense=Past  10 cop       _ _
9  a     a     DET   DT  Definite=Ind         10 det       _ _
10 boxer boxer NOUN  NN  Number=Sing          4  acl:relcl _ SpaceAfter=No
11 .     .     PUNCT .   _                    2  punct     _ _

will be printed (with the default parameters plus hints=0) as:

─┮
 │ ╭─╼ I PRON nsubj
 ╰─┾ saw VERB root
   │                        ╭─╼ a DET det
   ├────────────────────────┾ dog NOUN dobj
   ├─╼ today NOUN nmod:tmod │
   ├─╼ , PUNCT punct        │
   │                        │ ╭─╼ which DET nsubj
   │                        │ ├─╼ was VERB cop
   │                        │ ├─╼ a DET det
   │                        ╰─┶ boxer NOUN acl:relcl
   ╰─╼ . PUNCT punct

With layout=compact, the output will be (note the nodes “today” and “,”):

─┮
 │ ╭─╼ I PRON nsubj
 ╰─┾ saw VERB root
   │   ╭─╼ a DET det
   ┡───┾ dog NOUN dobj
   ┡─╼ │ today NOUN nmod:tmod
   ┡─╼ │ , PUNCT punct
   │   │ ╭─╼ which DET nsubj
   │   │ ┢─╼ was VERB cop
   │   │ ┢─╼ a DET det
   │   ╰─┶ boxer NOUN acl:relcl
   ╰─╼ . PUNCT punct

With layout=align-words, the output will be:

─┮
 │ ╭─╼       I PRON nsubj
 ╰─┾         saw VERB root
   │   ╭─╼   a DET det
   ┡───┾     dog NOUN dobj
   ┡─╼ │     today NOUN nmod:tmod
   ┡─╼ │     , PUNCT punct
   │   │ ╭─╼ which DET nsubj
   │   │ ┢─╼ was VERB cop
   │   │ ┢─╼ a DET det
   │   ╰─┶   boxer NOUN acl:relcl
   ╰─╼       . PUNCT punct

And finally with layout=align:

─┮
 │ ╭─╼       I     PRON  nsubj
 ╰─┾         saw   VERB  root
   │   ╭─╼   a     DET   det
   ┡───┾     dog   NOUN  dobj
   ┡─╼ │     today NOUN  nmod:tmod
   ┡─╼ │     ,     PUNCT punct
   │   │ ╭─╼ which DET   nsubj
   │   │ ┢─╼ was   VERB  cop
   │   │ ┢─╼ a     DET   det
   │   ╰─┶   boxer NOUN  acl:relcl
   ╰─╼       .     PUNCT punct

Some non-projective trees cannot be printed witout crossing edges. TextModeTrees uses a special “bridge” symbol ─╪─ to mark this:

─┮
 │ ╭─╼ 1
 ├─╪───┮ 2
 ╰─┶ 3 │
       ╰─╼ 4

With color=auto (which is the default), if the output is printed to the console (not file or pipe), each node attribute is printed in different color. If a given node’s MISC contains any of ToDo, Bug or Mark attributes (or any other specified in the parameter mark), the node will be highlighted (by reveresing the background and foreground colors).

This block’s method process_tree can be called on any node (not only root), which is useful for printing subtrees using node.draw(), which is internally implemented using this block.

For use in LaTeX, you can insert the output of this block (without colors) into egin{verbatim}…end{verbatim}, but you need to compile with pdflatex (xelatex not supported) and you must add the following code into the preambule:

\usepackage{pmboxdraw}
\DeclareUnicodeCharacter{256D}{   extSFi}  %╭
\DeclareUnicodeCharacter{2570}{   extSFii} %╰

SEE ALSO TextModeTreesHtml

add_node(idx, node)[source]

Render a node with its attributes.

before_process_document(document)[source]

Initialize ANSI colors if color is True or ‘auto’.

If color==’auto’, detect if sys.stdout is interactive (terminal, not redirected to a file).

static colorize_attr(attr, value, marked)[source]

Return a string with color markup for a given attr and its value.

colorize_comment(comment)[source]

Return a string with color markup for a given comment.

is_marked(node)[source]

Should a given node be highlighted?

print_headers(root)[source]

Print sent_id, text and other comments related to the tree.

process_tree(root)[source]

Print the tree to (possibly redirected) sys.stdout.

should_print_tree(root, allnodes)[source]

Should this tree be printed?