我正在尝试使用nltk在Python中编写脚本,该脚本将句子从第二人更改为第一人称。 示例:句子
I went to see Avatar and you came with me
应该成为
You went to see Avatar and I came with you
nltk中是否有内置函数可以执行此操作?
答案 0 :(得分:1)
嗯,不确定使用内置功能,但您可以尝试.replace()
例如:
.replace("I","You")
会将字符串中的每个“I”更改为“You”
答案 1 :(得分:1)
在英语中不应该有太多形式的个人和所有格代词。如果您创建第1和第2人表单之间的对应字典,则可以对原始句子进行标记并替换字典中的单词:
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 .
由于you
的含糊不清,你可能无法获得更好的结果。