Skip to content

Instantly share code, notes, and snippets.

@elyasha
Created October 31, 2020 14:42
Show Gist options
  • Save elyasha/a4befbdb27239f3813425207ca619c3c to your computer and use it in GitHub Desktop.
Save elyasha/a4befbdb27239f3813425207ca619c3c to your computer and use it in GitHub Desktop.
import spacy
from nltk import Tree
from squids import squids_text
dependency_parser = spacy.load('en')
parsed_squids = dependency_parser(squids_text)
# Assign my_sentence a new value:
my_sentence = "I am learning Parsing Text"
my_parsed_sentence = dependency_parser(my_sentence)
def to_nltk_tree(node):
if node.n_lefts + node.n_rights > 0:
parsed_child_nodes = [to_nltk_tree(child) for child in node.children]
return Tree(node.orth_, parsed_child_nodes)
else:
return node.orth_
for sent in parsed_squids.sents:
to_nltk_tree(sent.root).pretty_print()
for sent in my_parsed_sentence.sents:
to_nltk_tree(sent.root).pretty_print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment