nltk.draw.tree module¶
Graphically display a Tree.
- class nltk.draw.tree.TreeSegmentWidget[source]¶
Bases:
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 theroof
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. Ifroof
is true, draw a single triangular “roof” over the subtrees. Ifroof
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.
- class nltk.draw.tree.TreeView[source]¶
Bases:
object
- class nltk.draw.tree.TreeWidget[source]¶
Bases:
CanvasWidget
A canvas widget that displays a single Tree.
TreeWidget
manages a group ofTreeSegmentWidgets
that are used to display a Tree.Attributes:
node_attr
: Sets the attributeattr
on all of the node widgets for thisTreeWidget
.node_attr
: Sets the attributeattr
on all of the leaf widgets for thisTreeWidget
.loc_attr
: Sets the attributeattr
on all of the location widgets for thisTreeWidget
(if it was built from a Tree). Note that a location widget is aTextWidget
.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 theDRAGGABLE
property on all of theTreeWidget
’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.
- 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()
.
- 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.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 createsCanvasWidgets
.make_node
is used to convert the Tree’s nodes intoCanvasWidgets
. If no constructor is specified, thenTextWidget
will be used.make_leaf – A
CanvasWidget
constructor or a function that createsCanvasWidgets
.make_leaf
is used to convert the Tree’s leafs intoCanvasWidgets
. If no constructor is specified, thenTextWidget
will be used.attribs – Attributes for the canvas widgets that make up the returned
TreeSegmentWidget
. Any attribute beginning with'tree_'
will be passed to allTreeSegmentWidgets
(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).