Skip to content

Commit 57d64d5

Browse files
committed
Pretty-print constituency trees
1 parent 7d38248 commit 57d64d5

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ For BiLSTM-based semantic dependency parsing models, lemmas and POS tags are nee
157157
```py
158158
>>> import os
159159
>>> import tempfile
160-
>>> Parser.load('biaffine-dep-en').predict(['I','saw','Sarah','with','a','telescope','.'], verbose=False)[0]
160+
>>> dep = Parser.load('biaffine-dep-en')
161+
>>> dep.predict(['I', 'saw', 'Sarah', 'with', 'a', 'telescope', '.'], verbose=False)[0]
161162
1 I _ _ _ _ 2 nsubj _ _
162163
2 saw _ _ _ _ 0 root _ _
163164
3 Sarah _ _ _ _ 2 dobj _ _
@@ -185,7 +186,7 @@ For BiLSTM-based semantic dependency parsing models, lemmas and POS tags are nee
185186
186187
''')
187188
...
188-
>>> Parser.load('biaffine-dep-en').predict(path, pred='pred.conllx', verbose=False)[0]
189+
>>> dep.predict(path, pred='pred.conllx', verbose=False)[0]
189190
# text = But I found the location wonderful and the neighbors very kind.
190191
1 But _ _ _ _ 3 cc _ _
191192
2 I _ _ _ _ 3 nsubj _ _
@@ -201,13 +202,26 @@ For BiLSTM-based semantic dependency parsing models, lemmas and POS tags are nee
201202
11 kind _ _ _ _ 6 conj _ _
202203
12 . _ _ _ _ 3 punct _ _
203204

204-
>>> Parser.load('crf-con-en').predict(['I','saw','Sarah','with','a','telescope','.'], verbose=False)[0]
205-
(TOP (S (NP (_ I)) (VP (_ saw) (NP (_ Sarah)) (PP (_ with) (NP (_ a) (_ telescope)))) (_ .)))
206-
>>> Parser.load('biaffine-sdp-en').predict([[('I','I','PRP'), ('saw','see','VBD'),
207-
('Sarah','Sarah','NNP'), ('with','with','IN'),
208-
('a','a','DT'), ('telescope','telescope','NN'),
209-
('.','_','.')]],
210-
verbose=False)[0]
205+
>>> con = Parser.load('crf-con-en')
206+
>>> con.predict(['I', 'saw', 'Sarah', 'with', 'a', 'telescope', '.'], verbose=False)[0].pretty_print()
207+
TOP
208+
|
209+
S
210+
_____________|______________________
211+
| VP |
212+
| _________|____ |
213+
| | | PP |
214+
| | | ____|___ |
215+
NP | NP | NP |
216+
| | | | ___|______ |
217+
_ _ _ _ _ _ _
218+
| | | | | | |
219+
I saw Sarah with a telescope .
220+
221+
>>> sdp = Parser.load('biaffine-sdp-en')
222+
>>> sdp.predict([[('I','I','PRP'), ('saw','see','VBD'), ('Sarah','Sarah','NNP'), ('with','with','IN'),
223+
('a','a','DT'), ('telescope','telescope','NN'), ('.','_','.')]],
224+
verbose=False)[0]
211225
1 I I PRP _ _ _ _ 2:ARG1 _
212226
2 saw see VBD _ _ _ _ 0:root|4:ARG1 _
213227
3 Sarah Sarah NNP _ _ _ _ 2:ARG2 _

supar/utils/transform.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,6 @@ def __init__(self, transform, tree):
762762

763763
def __repr__(self):
764764
return self.values[-2].pformat(1000000)
765+
766+
def pretty_print(self):
767+
self.values[-2].pretty_print()

0 commit comments

Comments
 (0)