nltk.draw.tree module

Graphically display a Tree.

class nltk.draw.tree.TreeSegmentWidget[source]

Bases: nltk.draw.util.CanvasWidget

A canvas widget that displays a single segment of a hierarchical tree. Each TreeSegmentWidget connects a single “node widget” to a sequence of zero or more “subtree widgets”. By default, the bottom of the node is connected to the top of each subtree by a single line. However, if the roof attribute is set, then a single triangular “roof” will connect the node to all of its children.

Attributes:
  • roof: What sort of connection to draw between the node and its subtrees. If roof is true, draw a single triangular “roof” over the subtrees. If roof is false, draw a line between each subtree and the node. Default value is false.

  • xspace: The amount of horizontal space to leave between subtrees when managing this widget. Default value is 10.

  • yspace: The amount of space to place between the node and its children when managing this widget. Default value is 15.

  • color: The color of the lines connecting the node to its subtrees; and of the outline of the triangular roof. Default value is '#006060'.

  • fill: The fill color for the triangular roof. Default value is '' (no fill).

  • width: The width of the lines connecting the node to its subtrees; and of the outline of the triangular roof. Default value is 1.

  • orientation: Determines whether the tree branches downwards or rightwards. Possible values are 'horizontal' and 'vertical'. The default value is 'vertical' (i.e., branch downwards).

  • draggable: whether the widget can be dragged by the user.

__init__(canvas, label, subtrees, **attribs)[source]
label()[source]
subtrees()[source]
set_label(label)[source]

Set the node label to label.

replace_child(oldchild, newchild)[source]

Replace the child oldchild with newchild.

remove_child(child)[source]
insert_child(index, child)[source]
nltk.draw.tree.tree_to_treesegment(canvas, t, make_node=<class 'nltk.draw.util.TextWidget'>, make_leaf=<class 'nltk.draw.util.TextWidget'>, **attribs)[source]

Convert a Tree into a TreeSegmentWidget.

Parameters
  • make_node – A CanvasWidget constructor or a function that creates CanvasWidgets. make_node is used to convert the Tree’s nodes into CanvasWidgets. If no constructor is specified, then TextWidget will be used.

  • make_leaf – A CanvasWidget constructor or a function that creates CanvasWidgets. make_leaf is used to convert the Tree’s leafs into CanvasWidgets. If no constructor is specified, then TextWidget will be used.

  • attribs – Attributes for the canvas widgets that make up the returned TreeSegmentWidget. Any attribute beginning with 'tree_' will be passed to all TreeSegmentWidgets (with the 'tree_' prefix removed. Any attribute beginning with 'node_' will be passed to all nodes. Any attribute beginning with 'leaf_' will be passed to all leaves. And any attribute beginning with 'loc_' will be passed to all text locations (for Trees).

class nltk.draw.tree.TreeWidget[source]

Bases: nltk.draw.util.CanvasWidget

A canvas widget that displays a single Tree. TreeWidget manages a group of TreeSegmentWidgets that are used to display a Tree.

Attributes:

  • node_attr: Sets the attribute attr on all of the node widgets for this TreeWidget.

  • node_attr: Sets the attribute attr on all of the leaf widgets for this TreeWidget.

  • loc_attr: Sets the attribute attr on all of the location widgets for this TreeWidget (if it was built from a Tree). Note that a location widget is a TextWidget.

  • xspace: The amount of horizontal space to leave between subtrees when managing this widget. Default value is 10.

  • yspace: The amount of space to place between the node and its children when managing this widget. Default value is 15.

  • line_color: The color of the lines connecting each expanded node to its subtrees.

  • roof_color: The color of the outline of the triangular roof for collapsed trees.

  • roof_fill: The fill color for the triangular roof for collapsed trees.

  • width

  • orientation: Determines whether the tree branches downwards or rightwards. Possible values are 'horizontal' and 'vertical'. The default value is 'vertical' (i.e., branch downwards).

  • shapeable: whether the subtrees can be independently dragged by the user. THIS property simply sets the DRAGGABLE property on all of the TreeWidget’s tree segments.

  • draggable: whether the widget can be dragged by the user.

__init__(canvas, t, make_node=<class 'nltk.draw.util.TextWidget'>, make_leaf=<class 'nltk.draw.util.TextWidget'>, **attribs)[source]

Create a new canvas widget. This constructor should only be called by subclass constructors; and it should be called only “after” the subclass has constructed all graphical canvas objects and registered all child widgets.

Parameters
  • canvas (Tkinter.Canvas) – This canvas widget’s canvas.

  • parent (CanvasWidget) – This canvas widget’s hierarchical parent.

  • attribs – The new canvas widget’s attributes.

expanded_tree(*path_to_tree)[source]

Return the TreeSegmentWidget for the specified subtree.

Parameters

path_to_tree – A list of indices i1, i2, …, in, where the desired widget is the widget corresponding to tree.children()[i1].children()[i2]....children()[in]. For the root, the path is ().

collapsed_tree(*path_to_tree)[source]

Return the TreeSegmentWidget for the specified subtree.

Parameters

path_to_tree – A list of indices i1, i2, …, in, where the desired widget is the widget corresponding to tree.children()[i1].children()[i2]....children()[in]. For the root, the path is ().

bind_click_trees(callback, button=1)[source]

Add a binding to all tree segments.

bind_drag_trees(callback, button=1)[source]

Add a binding to all tree segments.

bind_click_leaves(callback, button=1)[source]

Add a binding to all leaves.

bind_drag_leaves(callback, button=1)[source]

Add a binding to all leaves.

bind_click_nodes(callback, button=1)[source]

Add a binding to all nodes.

bind_drag_nodes(callback, button=1)[source]

Add a binding to all nodes.

toggle_collapsed(treeseg)[source]

Collapse/expand a tree.

class nltk.draw.tree.TreeView[source]

Bases: object

__init__(*trees)[source]
resize(*e)[source]
destroy(*e)[source]
mainloop(*args, **kwargs)[source]

Enter the Tkinter mainloop. This function must be called if this demo is created from a non-interactive program (e.g. from a secript); otherwise, the demo will close as soon as the script completes.

nltk.draw.tree.draw_trees(*trees)[source]

Open a new window containing a graphical diagram of the given trees.

Return type

None

nltk.draw.tree.demo()[source]