Skip to content

Support printing enhanced graphs in Tikz #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion udapi/block/write/tikz.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class Tikz(BaseWriter):
"""

def __init__(self, print_sent_id=True, print_text=True, print_preambule=True,
attributes=None, as_tree=False, comment_attribute=None, **kwargs):
attributes=None, as_tree=False, comment_attribute=None,
enhanced=False, **kwargs):
"""Create the Tikz block object.

Args:
Expand All @@ -50,6 +51,7 @@ def __init__(self, print_sent_id=True, print_text=True, print_preambule=True,
attributes: comma-separated list of node attributes to print (each on a separate line).
as_tree: boolean - should print it as a 2D tree?
comment_attribute: which attribute to print as a string under each graph (e.g. text_en)
enhanced: boolean - print the enhanced graph below the sentence, too?
"""
super().__init__(**kwargs)
self.print_sent_id = print_sent_id
Expand All @@ -63,6 +65,9 @@ def __init__(self, print_sent_id=True, print_text=True, print_preambule=True,
self.node_attributes = 'form,upos'.split(',')
self.as_tree = as_tree
self.comment_attribute = comment_attribute
if as_tree and enhanced:
raise ValueError("The enhanced graph cannot be printed as a tree")
self.enhanced = enhanced

def before_process_document(self, doc):
super().before_process_document(doc)
Expand Down Expand Up @@ -140,6 +145,12 @@ def process_tree(self, tree):
print(r'\deproot{%d}{root}' % node.ord)
else:
print(r'\depedge{%d}{%d}{%s}' % (node.parent.ord, node.ord, node.deprel))
if self.enhanced:
for dep in node.deps:
if dep['parent'].is_root():
print(r'\deproot[edge below]{%d}{root}' % node.ord)
else:
print(r'\depedge[edge below]{%d}{%d}{%s}' % (dep['parent'].ord, node.ord, dep['deprel']))
if self.comment_attribute and tree.comment:
start_pos = tree.comment.find(self.comment_attribute + ' = ')
if start_pos != -1:
Expand Down