问题
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