/
treeprettyprinter.doctest
177 lines (153 loc) · 8.98 KB
/
treeprettyprinter.doctest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
.. Copyright (C) 2001-2021 NLTK Project
.. For license information, see LICENSE.TXT
=========================================================
Unit tests for nltk.tree.prettyprinter.TreePrettyPrinter
=========================================================
>>> from nltk.tree import Tree, TreePrettyPrinter
Tree nr 2170 from nltk.corpus.treebank:
>>> tree = Tree.fromstring(
... '(S (NP-SBJ (PRP I)) (VP (VBP feel) (ADJP-PRD (RB pretty) '
... '(JJ good)) (PP-CLR (IN about) (NP (PRP it)))) (. .))')
>>> tpp = TreePrettyPrinter(tree)
>>> print(tpp.text())
S
__________________________|_____________________
| VP |
| ____________________|___________ |
| | | PP-CLR |
| | | _____|_____ |
NP-SBJ | ADJP-PRD | NP |
| | _______|______ | | |
PRP VBP RB JJ IN PRP .
| | | | | | |
I feel pretty good about it .
>>> print(tpp.text(unicodelines=True))
S
┌──────────────────────────┼─────────────────────┐
│ VP │
│ ┌─────────────┬──────┴───────────┐ │
│ │ │ PP-CLR │
│ │ │ ┌─────┴─────┐ │
NP-SBJ │ ADJP-PRD │ NP │
│ │ ┌───────┴──────┐ │ │ │
PRP VBP RB JJ IN PRP .
│ │ │ │ │ │ │
I feel pretty good about it .
A tree with long labels:
>>> tree = Tree.fromstring(
... '(sentence (plural-noun-phrase (plural-noun Superconductors)) '
... '(verb-phrase (plural-verb conduct) '
... '(noun-phrase (singular-noun electricity))))')
>>> tpp = TreePrettyPrinter(tree)
>>> print(tpp.text(abbreviate=8, nodedist=2))
sentence
__________|__________
| verb-phr.
| __________|__________
plural-n. | noun-phr.
| | |
plural-n. plural-v. singular.
| | |
Supercon. conduct electric.
>>> print(tpp.text(maxwidth=8, nodedist=2))
sentence
_________|________
| verb-
| phrase
| ________|_________
plural- | noun-
noun- | phrase
phrase | |
| | |
plural- plural- singular-
noun verb noun
| | |
Supercon conduct electric
ductors ity
A discontinuous tree:
>>> tree = Tree.fromstring(
... '(top (punct 8) (smain (noun 0) (verb 1) (inf (verb 5) (inf (verb 6) '
... '(conj (inf (pp (prep 2) (np (det 3) (noun 4))) (verb 7)) (inf (verb 9)) '
... '(vg 10) (inf (verb 11)))))) (punct 12))', read_leaf=int)
>>> sentence = ('Ze had met haar moeder kunnen gaan winkelen ,'
... ' zwemmen of terrassen .'.split())
>>> tpp = TreePrettyPrinter(tree, sentence)
>>> print(tpp.text())
top
_____|______________________________________________
smain | |
_______________________________|_____ | |
| | inf | |
| | _____|____ | |
| | | inf | |
| | | ____|_____ | |
| | | | conj | |
| | _____ | ___ | _________|______ | __________________ |
| | inf | | | | | | |
| | _________|_____ | ___ | _________ | | | | |
| | pp | | | | | | | |
| | ____|____ | | | | | | | |
| | | np | | | | inf | inf |
| | | ____|____ | | | | | | | |
noun verb prep det noun verb verb verb punct verb vg verb punct
| | | | | | | | | | | | |
Ze had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .
>>> print(tpp.text(unicodelines=True))
top
┌─────┴──────────────────┬───────────────────────────┐
smain │ │
┌────┬──────────────────────────┴─────┐ │ │
│ │ inf │ │
│ │ ┌─────┴────┐ │ │
│ │ │ inf │ │
│ │ │ ┌────┴─────┐ │ │
│ │ │ │ conj │ │
│ │ ┌───── │ ─── │ ─────────┴────── │ ─────┬─────┬──────┐ │
│ │ inf │ │ │ │ │ │ │
│ │ ┌─────────┴───── │ ─── │ ─────────┐ │ │ │ │ │
│ │ pp │ │ │ │ │ │ │ │
│ │ ┌────┴────┐ │ │ │ │ │ │ │ │
│ │ │ np │ │ │ │ inf │ inf │
│ │ │ ┌────┴────┐ │ │ │ │ │ │ │ │
noun verb prep det noun verb verb verb punct verb vg verb punct
│ │ │ │ │ │ │ │ │ │ │ │ │
Ze had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .
Importing TreePrettyPrinter
---------------------------
First of all, a simple tree will be constructed::
>>> from nltk.tree import Tree
>>> tree = Tree.fromstring('(S (NP Mary) (VP walks))')
We'll use this sample tree to show that the method of importing `TreePrettyPrinter` work correctly:
- Recommended::
>>> from nltk.tree import TreePrettyPrinter
>>> print(TreePrettyPrinter(tree).text())
S
____|____
NP VP
| |
Mary walks
- Alternative but valid options::
>>> from nltk import TreePrettyPrinter
>>> print(TreePrettyPrinter(tree).text())
S
____|____
NP VP
| |
Mary walks
>>> from nltk.tree.prettyprinter import TreePrettyPrinter
>>> print(TreePrettyPrinter(tree).text())
S
____|____
NP VP
| |
Mary walks
- Deprecated, do not use::
>>> from nltk.treeprettyprinter import TreePrettyPrinter
>>> print(TreePrettyPrinter(tree).text())
S
____|____
NP VP
| |
Mary walks
This method will throw a DeprecationWarning::
Import `TreePrettyPrinter` using `from nltk.tree import TreePrettyPrinter` instead.