от
Я использую SpaCy, чтобы найти предложения, содержащие «есть» или «был», у которых есть местоимения в качестве субъектов, и вернуть объект предложения. Мой код работает, но я чувствую, что должен быть намного лучший способ сделать это.
import spacy
nlp = spacy.load('en_core_web_sm')

ex_phrase = nlp("He was a genius. I really liked working with him. He is a dog owner. She is very kind to animals.")


#create an empty list to hold any instance of this particular construction
list_of_responses = []

#split into sentences
for sent in ex_phrase.sents:
    for token in sent:
        #check to see if the word 'was' or 'is' is in each sentence, if so, make a list of the verb's constituents
        if token.text == 'was' or token.text == 'is':
            dependency = [child for child in token.children]
            #if the first constituent is a pronoun, make sent_object equal to the item at index 1 in the list of constituents
            if dependency[0].pos_ == 'PRON':
                sent_object = dependency[1]

    #create a string of the entire object of the verb. For instance, if sent_object = 'genius', this would create a string 'a genius'
    for token in sent:
        if token == sent_object:
            whole_constituent = [t.text for t in token.subtree]
            whole_constituent = " ".join(whole_constituent)

    #check to see what the pronoun was, and depending on if it was 'he' or 'she', construct a coherent followup sentence
    if dependency[0].text.lower() == 'he':
        returning_phrase = f"Why do you think him being {whole_constituent} helped the two of you get along?"
    elif dependency[0].text.lower() == 'she':
        returning_phrase = f"Why do you think her being {whole_constituent} helped the two of you get along?"

    #add each followup sentence to the list. For some reason it creates a lot of duplicates, so I have to use set
    list_of_responses.append(returning_phrase)
    list_of_responses = list(set(list_of_responses))
             

Ваш ответ

Отображаемое имя (по желанию):
Конфиденциальность: Ваш электронный адрес будет использоваться только для отправки уведомлений.
Анти-спам проверка:
Чтобы избежать проверки в будущем, пожалуйста войдите или зарегистрируйтесь.
...