NLTK WordNet を使用して固有名詞を見つける方法はありますか?つまり、nltk Wordnet を使用して所有名詞にタグを付けることはできますか?
2 に答える
固有名詞を見つけるのに 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
は少し冗長で、所有格がわかりません。
品詞タガーである taggerが必要だと思います。このツールは、文中の各単語に品詞タグ(固有名詞、所有代名詞など) を割り当てます。
NLTKにはいくつかのタガーが含まれています: http://nltk.org/book/ch05.html
また、Stanford Part-Of-Speech Taggerもあります (オープン ソースもあり、パフォーマンスが向上しています)。