Turning a sentence from first to second person

故事扮演 提交于 2021-02-17 07:06:09

问题


I'm trying to write a script in Python using nltk which changes a sentence from second person to first person. Example: the sentence

I went to see Avatar and you came with me

should become

You went to see Avatar and I came with you

Is there a built-in function in nltk that does this?


回答1:


There shouldn't be too many forms of personal and possessive pronouns in English. If you create a dictionary of correspondence between 1st and 2nd person forms, you can then tokenize the original sentence and replace the words that are in the dictionary:

forms = {"am" : "are", "are" : "am", 'i' : 'you', 'my' : 'yours', 'me' : 'you', 'mine' : 'yours', 'you' : 'I', 'your' : 'my', 'yours' : 'mine'} # More?
def translate(word):
  if word.lower() in forms: return forms[word.lower()]
  return word

sent = 'You went to see Avatar, and I came with you.'
result = ' '.join([translate(word) for word in nltk.wordpunct_tokenize(sent)])
print(result.caputalize())
# I went to see avatar , and you came with i .

Because of the ambiguity of you you probably cannot get any better results.




回答2:


Um, not sure about using a built in function, but you can try .replace()

For example:

.replace("I","You")

would change every "I" in the string to "You"



来源:https://stackoverflow.com/questions/41051125/turning-a-sentence-from-first-to-second-person

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