あなたは私の最近のブログを読むことに興味があるかもしれません:
http://mattshomepage.com/#/blog/feb2013/liftingthehood
nltk.ne_chunk 関数がどのように機能するかを理解することです。しかし、ここに私が書いたコードをいくつか示します。すぐにコピーして貼り付けることができ、役立つかもしれません。
import nltk
# Loads the serialized NEChunkParser object
chunker = nltk.data.load('chunkers/maxent_ne_chunker/english_ace_multiclass.pickle')
# The MaxEnt classifier
maxEnt = chunker._tagger.classifier()
def ne_report(sentence, report_all=False):
# Convert the sentence into a tokens with their POS tags
tokens = nltk.word_tokenize(sentence)
tokens = nltk.pos_tag(tokens)
tags = []
for i in range(0, len(tokens)):
featureset = chunker._tagger.feature_detector(tokens, i, tags)
tag = chunker._tagger.choose_tag(tokens, i, tags)
if tag != 'O' or report_all:
print '\nExplanation on the why the word \'' + tokens[i][0] + '\' was tagged:'
featureset = chunker._tagger.feature_detector(tokens, i, tags)
maxEnt.explain(featureset)
tags.append(tag)
report_all フラグを使用すると、すべての単語がどのように選択されたかを表示できますが、名前付きエンティティがどのように選択されたかだけに関心がある可能性があります。デフォルトでは False に設定されています。
「I love Apple products」など、好きな文を入力してください。そして、その名前付きエンティティが MaxEnt 分類子によって選択された理由の説明を報告します。また、選択された可能性のある他のタグの確率の一部も報告されます。
NLTK の開発者は .explain() メソッドを提供しましたが、それはまさにこの関数が使用するものです。