I'd like to print displaCy result in text format.
Here is one example that prints the tree.
How to get the dependency tree with spaCy?
But it is still different from what is shown displaCy. For example, if I use the following code,
import spacy
from nltk import Tree
en_nlp = spacy.load('en')
import sys
doc = en_nlp(u'The quick brown fox jumps over the lazy dog.')
def tok_format(tok):
return "_".join([tok.orth_, tok.tag_])
def to_nltk_tree(node):
if node.n_lefts + node.n_rights > 0:
return Tree(tok_format(node), [to_nltk_tree(child) for child in node.children])
else:
return tok_format(node)
print [to_nltk_tree(sent.root).pretty_print() for sent in doc.sents]
it will print the following.
jumps_VBZ
_____________|________________________
| | over_IN
| | |
| fox_NN dog_NN
| ________|________ _______|_______
._. The_DT quick_JJ brown_JJ the_DT lazy_JJ
[None]
However, displaCy shows the following, which has NOUN, VERB, ADP, NOUN, as well as nsub, prep, pobj.
Does anybody know a way to exactly replicate displaCy output in text format? Thanks.