10

As the title shows I am trying to look for pronouns in a string and replace it with it's antecedent like:

[in]: "the princess looked from the palace, she was happy".
[out]: "the princess looked from the palace, the princess was happy". 

I use pos tag to return pronouns and nouns. I need to know how to replace without knowing the sentence, meaning how to specify the subject in the sentence to replace the pronoun with it. Any suggestions?

4

1 回答 1

5

我不知道 nltk 包(从未使用过),但它似乎马上就给出了答案。如果您查看 nltk.org 上的解析树示例,它表明主题已成功标记为“NP-SBJ”标签。这不是你要找的吗?

(早些时候,我忽略了标题中的“nltk”部分,我写了下面的部分。我认为作为如何解决此类问题的一般介绍可能会很有趣(特别是如果您没有可用的软件包),所以我把它留在这里:)

这更像是一个“自然语言”(即英语)问题,而不是 Python 问题。您能否更具体地说明您期望什么样的句子?它应该适用于所有可能的英语句子吗?我认为那将是非常困难的。

如果句子足够“简单”,那么假设第一个动词之前的所有内容都是主语就足够了。这适用于您的示例,但不适用于以下句子:

yesterday the princess looked from the palace, she was happy.
the princes who drank tea looked from the palace, she was happy.

(注意后一句的主语是“喝茶的公主”,“喝茶的人”部分是“形容词”)。

另外,指定如果代词不指向主语(例如,指向宾语)会发生什么:

the princess looked at the prince, he was happy.

为了在最一般的情况下解决您的问题,您应该找到(或制定)英语(或任何其他)语言的正式规范,它可以准确地告诉您句子的哪个部分是主语、动词、宾语等. 示例:许多简单的英语句子的形式(括号[]之间的部分是可选的,括号()之间的部分是选择,即,(the|a)表示您应该选择'the'或'a'):

sentence := subject verb [object]

规范右侧的每个部分都需要更详细地指定,例如:

subject, object := (noun_part_of_sentence|noun_part_of_sentence_plural)
noun_part_of_sentence := article [adjectivelist] [noun_modifier] noun # I guess there is a formal name for this...
noun_part_of_sentence_plural := [adjectivelist] [noun_modifier] noun_plural # note: no article
adjectivelist:= adjective [adjectivelist] # i.e., one or more adjectives

对于更复杂的句子,例如上面带有形容词短语的句子,上面的规范是不够的,应该是这样的:

noun_part_of_sentence := (the|a) [adjectivelist] [noun_modifier] [noun] [adjective_phrase]
adjective_phrase := relative_pronoun verb [object]
relative_pronoun := (who|which|that)

请注意,上面的规范已经非常强大:(如果您能够正确识别每个单词的类型,例如动词、名词、冠词等)它可以成功检测到以下句子:

The princess drank the tea.
The beautiful princess drank the tea.
The beautiful princess drank delicious the tea.
A beautiful princess drank delicious the lemon tea.
The beautiful princess who saw the handsome prince drank the refreshing tea.
The beautiful princess who saw the handsome prince who made the tea drank the refreshing tea.

但是,它(还)不允许像“公主看着宫殿”、“公主喝茶”(注意:不是“茶”)和无数其他句子。诀窍是将您的正式规范扩展到足以满足您期望的句子类型的水平。

成功解析句子后,您(因此)知道主语,任何代词,并且可以进行替换。但是请注意,英语并不是明确的,例如:

The princess looked at her mother, she was happy.

她指的是公主还是她的母亲?

祝你好运!

PS 英语不是我的母语,所以我希望我对所有事情都使用了正确的术语!

于 2013-04-08T10:46:02.853 回答