32

NLTK WordNet を使用して固有名詞を見つける方法はありますか?つまり、nltk Wordnet を使用して所有名詞にタグを付けることはできますか?

4

2 に答える 2

57

固有名詞を見つけるのに 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']

とは 2 つのトークンに分割されているため、あまり満足できない可能性があります。その場合、Name Entity tagger などのより複雑なものが必要になる場合がありMichaelます。Jackson

確かに、タグセットで文書化されているpenntreebankように、所有名詞については、POSタグhttp://www.mozart-oz.org/mogul/doc/lager/brill-tagger/penn.htmlを探すだけです。POSしかし、多くの場合、タガーはNNP.

所有名詞を見つけるには、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']

usingne_chunkは少し冗長で、所有格がわかりません。

于 2013-07-16T09:13:57.397 に答える
3

品詞タガーである taggerが必要だと思います。このツールは、文中の各単語に品詞タグ(固有名詞、所有代名詞など) を割り当てます。

NLTKにはいくつかのタガーが含まれています: http://nltk.org/book/ch05.html

また、Stanford Part-Of-Speech Taggerもあります (オープン ソースもあり、パフォーマンスが向上しています)。

于 2013-07-16T07:07:02.553 に答える