gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:17:46 26 4
gpt4 key购买 nike

有没有办法使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com