web-dev-qa-db-ja.com

バイグラムとトライグラムのNLTKコロケーションスコアリングについて

背景:

単語のペアを比較して、どのペアが米国英語で他のペアよりも「発生する可能性が高い」かを確認しようとしています。私の計画は、NLTKのコロケーション機能を使用して単語のペアをスコアリングすることでした。

アプローチ:

私はPythonでNLTKを使用して以下をコーディングしました(簡潔にするためにいくつかのステップとインポートは削除されています):

bgm    = nltk.collocations.BigramAssocMeasures()
Finder = BigramCollocationFinder.from_words(tokens)
scored = Finder.score_ngrams( bgm.likelihood_ratio  )
print scored

結果:

次に、2つの単語のペアを使用して結果を調べました。1つは共起する可能性が高く、もう1つは発生しないはずです(「ローストカシュー」と「ガソリンカシュー」)。これらの単語のペアリングスコアがまったく同じであることに驚きました。

[(('roasted', 'cashews'), 5.545177444479562)]
[(('gasoline', 'cashews'), 5.545177444479562)]

私のテストでは、「ローストカシューナッツ」が「ガソリンカシューナッツ」よりも高いスコアになると予想していました。

質問:

  1. コロケーションの使用を誤解していますか?
  2. 私のコードは間違っていますか?
  3. スコアが異なるはずだという私の仮定は間違っていますか?

情報やヘルプをありがとうございました!

26
ccgillett

NLTKコロケーションドキュメントは私にはかなり良いようです。 http://www.nltk.org/howto/collocations.html

スコアラーに、使用する実際のかなりのコーパスを与える必要があります。これは、NLTKに組み込まれたブラウンコーパスを使用した実用的な例です。実行には約30秒かかります。

import nltk.collocations
import nltk.corpus
import collections

bgm    = nltk.collocations.BigramAssocMeasures()
Finder = nltk.collocations.BigramCollocationFinder.from_words(
    nltk.corpus.brown.words())
scored = Finder.score_ngrams( bgm.likelihood_ratio  )

# Group bigrams by first Word in bigram.                                        
prefix_keys = collections.defaultdict(list)
for key, scores in scored:
   prefix_keys[key[0]].append((key[1], scores))

# Sort keyed bigrams by strongest association.                                  
for key in prefix_keys:
   prefix_keys[key].sort(key = lambda x: -x[1])

print 'doctor', prefix_keys['doctor'][:5]
print 'baseball', prefix_keys['baseball'][:5]
print 'happy', prefix_keys['happy'][:5]

出力は妥当なようで、野球ではうまく機能しますが、医者にとってはそれほどうまくいきません。

doctor [('bills', 35.061321987405748), (',', 22.963930079491501), 
  ('annoys', 19.009636692022365), 
  ('had', 16.730384189212423), ('retorted', 15.190847940499127)]

baseball [('game', 32.110754519752291), ('cap', 27.81891372457088), 
  ('park', 23.509042621473505), ('games', 23.105033513054011), 
  ("player's",    16.227872863424668)]

happy [("''", 20.296341424483998), ('Spahn', 13.915820697905589), 
 ('family', 13.734352182441569), 
 (',', 13.55077617193821), ('bodybuilder', 13.513265447290536)
32
Rob Neuhaus