Skip to content Skip to sidebar Skip to footer

Sentence Structure Analysis

I am trying to look at the structure similarity of sentences, specifically to the position of verbs, adj, nouns. For instance, I have three (or more) sentences which look likes as

Solution 1:

Here is a simple example using spacy:

import spacy
import pandas as pd

# load english language model
nlp = spacy.load('en_core_web_sm',disable=['ner','textcat'])

text = "I ate an apple pie, yesterday."

# create spacy 
doc = nlp(text)
pos = ""
for token in doc:
    pos += token.pos_ + " "
    
# create dataframe
df = pd.DataFrame([[text, pos]], columns=['Sentence', 'Structure'])
print(df)

Output is:

                      Sentence                                  Structure
0  I ate an apple pie, yesterday.  PRON VERB DET NOUN NOUN PUNCT NOUN PUNCT 

Post a Comment for "Sentence Structure Analysis"