我试图用nltk用Python编写一个脚本,它将一个句子从第二人称改为第一人称。例句:句子
I went to see Avatar and you came with me应该变成
You went to see Avatar and I came with you在nltk中有一个内置函数来实现这个功能吗?
发布于 2016-12-09 04:20:56
英语中不应该有太多的人称代词和所有格代词。如果您创建了第一人称表单和第二人称表单之间的对应字典,那么您可以标记原始句子并替换字典中的单词:
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的模糊性,您可能无法获得更好的结果。
发布于 2016-12-09 00:27:44
呃,不确定是否使用内置函数,但您可以尝试.replace()
例如:
.replace("I","You")将字符串中的每一个"I“改为"You”
https://stackoverflow.com/questions/41051125
复制相似问题