Last active
October 26, 2023 02:35
-
-
Save TristynAlxander/3eb0af4e5207dcb4546719b47be7bdf1 to your computer and use it in GitHub Desktop.
Example printing syntactic tree using SpaCy and the benepar self-attentive encoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports | |
import benepar, spacy | |
# Load Parser | |
nlp = spacy.load("en_core_web_lg") | |
nlp.add_pipe('benepar', config={'model': 'benepar_en3'}) | |
# Define Print Syntactic Tree | |
def print_tree(phrase_list,i=0): | |
# Correct Input | |
if( isinstance(phrase_list,spacy.tokens.span.Span) ): | |
phrase_list = list(phrase_list._.children) | |
# Iterate Recursively | |
for phrase in phrase_list: | |
# If Phrase has Labels (i.e. isn't just a word), print labels | |
if(phrase._.labels): print(" "*i+phrase._.labels[0]) | |
# Get Lower Level of Phrases | |
sub_phrase_list = list(phrase._.children) | |
if(len(sub_phrase_list)): | |
print_tree(sub_phrase_list,i+1) | |
# Print Words | |
else: | |
word = phrase[0] | |
print(" "*i+word.pos_ +f" ({word.text})") | |
# Get Sentence. | |
doc = nlp("The quick brown fox jumps over the lazy dog.") | |
sentence = list(doc.sents)[0] | |
# Print Tree | |
print_tree(sentence) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment