Sentence structure analysis

限于喜欢 提交于 2020-12-15 01:41:40

问题


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 follows:

I ate an apple pie, yesterday. 
I ate an orange, yesterday.
I eat a lemon, today. 

All of them starts with a pronoun (I) followed by a verb (ate/eat) and a noun (apple pie, orange, lemon) and, finally, an adverb (yesterday/tomorrow).

I would like to know if there is a way to identify the structure, i.e. PRONOUN VERB NOUN ADVERB in the sentence.

If I think of it as a pandas dataframe:

SENTENCE
    I ate an apple pie, yesterday. 
    I ate an orange, yesterday.
    I eat a lemon, today. 

I would need to have something like as follows:

SENTENCE                                    STRUCTURE
    I ate an apple pie, yesterday.        PRONOUN VERB NOUN ADJECTIVE
    I ate an orange, yesterday.           PRONOUN VERB NOUN ADJECTIVE         
    I eat a lemon, today.                 PRONOUN VERB NOUN ADJECTIVE

Do you know how I can get this (or similar) results?


回答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 


来源:https://stackoverflow.com/questions/63763542/sentence-structure-analysis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!