python - 使用 NLTK WordNet 查找专有名词

标签 python nltk wordnet

有没有办法使用 NLTK WordNet 找到专有名词?即,我可以使用 nltk Wordnet 标记所有格名词吗?

最佳答案

我认为您不需要 WordNet 来查找专有名词,我建议使用词性标注器 pos_tag

要查找专有名词,请查找 NNP 标签:

from nltk.tag import pos_tag

sentence = "Michael Jackson likes to eat at McDonalds"
tagged_sent = pos_tag(sentence.split())
# [('Michael', 'NNP'), ('Jackson', 'NNP'), ('likes', 'VBZ'), ('to', 'TO'), ('eat', 'VB'), ('at', 'IN'), ('McDonalds', 'NNP')]

propernouns = [word for word,pos in tagged_sent if pos == 'NNP']
# ['Michael','Jackson', 'McDonalds']

您可能不太满意,因为 MichaelJackson 被拆分为 2 个标记,那么您可能需要更复杂的东西,例如名称实体标记器。

正确地,正如 penntreebank 标签集所记录的,对于所有格名词,您可以简单地查找 POS 标签 http://www.mozart-oz.org/mogul/doc/lager/brill-tagger/penn.html .但是当它是一个 NNP 时,标注器通常不会标记 POS

要查找所有格名词,请查找 str.endswith("'s") 或 str.endswith("s'"):

from nltk.tag import pos_tag

sentence = "Michael Jackson took Daniel Jackson's hamburger and Agnes' fries"
tagged_sent = pos_tag(sentence.split())
# [('Michael', 'NNP'), ('Jackson', 'NNP'), ('took', 'VBD'), ('Daniel', 'NNP'), ("Jackson's", 'NNP'), ('hamburger', 'NN'), ('and', 'CC'), ("Agnes'", 'NNP'), ('fries', 'NNS')]

possessives = [word for word in sentence if word.endswith("'s") or word.endswith("s'")]
# ["Jackson's", "Agnes'"]

或者,您可以使用 NLTK ne_chunk,但它似乎并没有做太多其他事情,除非您担心从句子中得到什么样的专有名词:

>>> from nltk.tree import Tree; from nltk.chunk import ne_chunk
>>> [chunk for chunk in ne_chunk(tagged_sent) if isinstance(chunk, Tree)]
[Tree('PERSON', [('Michael', 'NNP')]), Tree('PERSON', [('Jackson', 'NNP')]), Tree('PERSON', [('Daniel', 'NNP')])]
>>> [i[0] for i in list(chain(*[chunk.leaves() for chunk in ne_chunk(tagged_sent) if isinstance(chunk, Tree)]))]
['Michael', 'Jackson', 'Daniel']

使用 ne_chunk 有点冗长,它不会让你掌握所有格。

关于python - 使用 NLTK WordNet 查找专有名词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669952/

相关文章:

python - 将字符串转换为字典列表

python - 遍历 Pandas DF 列表,然后遍历每个 DF 行

Javascript+Python : sending array to Python script, 将结果返回给 Javascript

带有 WordNet 2.1 的 Python 3 和 NLTK - 这可能吗?

python - NLTK 使用列表理解进行词形还原

java - 格式错误的行异常 - Android 上的 JWI

python - PySpark takeOrdered 多个字段(升序和降序)

python - 类型错误 : unsupported operand type(s) for div: 'str' and 'int' [line 14]

python - 删除应用后无法删除用户

python - 识别出现在语料库文档中少于 1% 的单词