1

私は NLTK を使用して、多数の個別のドキュメントの分析を行っています。これらのドキュメントの内容は、それらがすべて同じトークンで終了および開始する傾向があることを意味します。

ドキュメントをリストのリストにトークン化し、BigramCollocationFinder.from_documents を使用してファインダーを作成します。生の頻度で ngram をスコア付けすると、最も一般的な出現は終了文字/開始文字であることがわかります。これは、すべてのドキュメントを 1 つに実行し、全体で不要な ngram を見つけていることを示唆しています。

コードのサンプル:

line_tokenizer = nltk.RegexpTokenizer('\{|\}|[^,"}]+')
seqs = ["{B,C}", "{B,A}", "{A,B,C}"]
documents = [line_tokenizer.tokenize(s) for s in seqs]
finder = BigramCollocationFinder.from_documents(documents)
bigram_measures = nltk.collocations.BigramAssocMeasures()
print(finder.score_ngrams(bigram_measures.raw_freq))

これにより、次の出力が得られます。

[(('B', 'C'), 0.15384615384615385), 
 (('C', '}'), 0.15384615384615385), 
 (('{', 'B'), 0.15384615384615385), 
 (('}', '{'), 0.15384615384615385), 
 (('A', 'B'), 0.07692307692307693), 
 (('A', '}'), 0.07692307692307693), 
 (('B', 'A'), 0.07692307692307693), 
 (('{', 'A'), 0.07692307692307693)]

ngram }{ はリストに表示されますが、 } が隣り合って表示されることはありません。

} がリストに表示されないようにするために、この問題にアプローチする別の方法はありますか?

4

1 に答える 1

1

いくつかの単語が常に文の最後または最初にあることを知っておくとよい場合があるため、バイグラムを維持したいと考えています{AC}そしてハック:

}{からバイグラムを削除し、bigram_measure他のバイグラムの確率を で再計算し1-prob('}{')ます。

import nltk
line_tokenizer = nltk.RegexpTokenizer('\{|\}|[^,"}]+')
seqs = ["{B,C}", "{B,A}", "{A,B,C}"]
documents = [line_tokenizer.tokenize(s) for s in seqs]
finder = nltk.collocations.BigramCollocationFinder.from_documents(documents)
bigram_measures = nltk.collocations.BigramAssocMeasures()
# Put bigram measures into a dict for easy access
x = dict(finder.score_ngrams(bigram_measures.raw_freq))

# Re-adjust such that the score of 
# each bigram is divided by 1-prob('}{')
newmax = 1- x[('}','{')]

# Remove "}{" from bigrams.
del x[('}','{')]

# Recalcuate prob for each bigram with newmax
y =[(i,j/float(newmax)) for i,j in x.iteritems()]
print y

[(('B', 'C'), 0.18181818181818182), (('C', '}'), 0.18181818181818182), (('B', 'A'), 0.09090909090909091), (('{', 'A'), 0.09090909090909091), (('{', 'B'), 0.18181818181818182),  (('A', 'B'), 0.09090909090909091), (('A', '}'), 0.09090909090909091)]
于 2013-09-28T15:42:43.857 に答える