1

I'd like to print displaCy result in text format.

https://explosion.ai/demos/displacy?text=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog.&model=en_core_web_sm&cpu=1&cph=1

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.

enter image description here

Does anybody know a way to exactly replicate displaCy output in text format? Thanks.

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • Actually, it's surprising that it worked at all! NLTK is trying to print a CFG tree while Spacy is trying to print a dependency tree. – alvas Apr 26 '18 at 06:38

0 Answers0