将句子从第一人称转为第二人称 [英] Turning a sentence from first to second person

查看:212
本文介绍了将句子从第一人称转为第二人称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 nltk 在 Python 中编写一个脚本,该脚本将句子从第二人称更改为第一人称.例子:句子

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

应该变成

You went to see Avatar and I came with you

nltk 中是否有内置函数可以执行此操作?

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

推荐答案

英语中不应该有太多形式的人称代词和所有格代词.如果您创建了第一人称和第二人称形式之间的对应关系词典,则可以对原始句子进行标记化并替换词典中的单词:

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.

这篇关于将句子从第一人称转为第二人称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆