如何在NLTK中为复制器添加复合词?

时间:2013-06-04 20:54:01

标签: python nlp nltk

所以,我想知道是否有人知道如何组合多个术语以在NLTK.中的标记器中创建单个术语。

例如,当我这样做时:

nltk.pos_tag(nltk.word_tokenize('Apple Incorporated is the largest company'))

它给了我:

[('Apple', 'NNP'), ('Incorporated', 'NNP'), ('is', 'VBZ'), ('the', 'DT'), ('largest', 'JJS'), ('company', 'NN')]

如何将'Apple'和'Incorporated'放在一起成为('Apple Incorporated','NNP')

2 个答案:

答案 0 :(得分:1)

您可以尝试查看nltk.RegexParser。它允许您根据正则表达式对部分语音标记内容进行分块。 在您的示例中,您可以执行类似

的操作
pattern = "NP:{<NN|NNP|NNS|NNPS>+}"
c = nltk.RegexpParser(p)
t = c.parse(nltk.pos_tag(nltk.word_tokenize("Apple Incorporated is the largest company")))
print t

这会给你:

Tree('S', [Tree('NP', [('Apple', 'NNP'), ('Incorporated', 'NNP')]), ('is', 'VBZ'), ('the', 'DT'), ('largest', 'JJS'), Tree('NP', [('company', 'NN')])])

答案 1 :(得分:0)

代码正在完成它应该做的事情。它正在为令牌添加词性标签。 'Apple Incorporated'不是一个单一的标记。它是两个单独的令牌,因此不能应用单个POS标签。这是正确的行为。

我想知道你是否正在尝试使用错误的工具来完成工作。你想做什么/你为什么要这样做?也许您有兴趣识别搭配而不是POS标记?你可以看看这里: collocations module