2016-11-06 2 views
2

2つのリストの要素の点相互情報スコアを計算したいと思います。 のは、私たちは、私はその後、すべてのペアのPMIスコアを計算することができますどのように2つのリストの要素のPMI

ListA = "Hi there, This is only a test message. Please enjoy the weather in the park." 
ListB = "work, bank, tree, weather, sun" 

があるとしましょう(仕事、こんにちは)、(仕事、そこに)、(仕事、これ)....(日、公園)。

それは1つのリストのバイグラムののPMIを算出するために私のために働いた:

def pmi(word1, word2, unigram_freq, bigram_freq, unigram_freq_values, bigram_freq_values, output_name): 
    prob_word1 = unigram_freq[word1]/float(sum(unigram_freq_values)) 
    prob_word2 = unigram_freq[word2]/float(sum(unigram_freq_values)) 
    prob_word1_word2 = bigram_freq/float(sum(bigram_freq_values)) 
    pmi = math.log(prob_word1_word2/float(prob_word1*prob_word2),2) 

unigrams = nltk.FreqDist(ListA) 
bigrams = ngrams(ListA,2) 

n1_freq = nltk.FreqDist(unigrams) 
n2_freq = nltk.FreqDist(bigrams) 

output_pmi = "test.txt" 
for bigram, freq in n2_freq.most_common(1000): 
    w1 = bigram[0] 
    w2 = bigram[1] 
    unigram_freq_val = n1_freq.values() 
    bigram_freq_val = n2_freq.values() 
    pmi(w1, w2, unigrams, freq, unigram_freq_val, bigram_freq_val, output_pmi) 

IはLISTAとListBのからバイグラムのPMIを求める問題に捕まってしまいました。誰かが私を助けることができれば本当に感謝しています。どうもありがとう!

(。二つのリストには、当然のことながら、私の仕事はどのように見えるかの最低限の例です)

答えて

1

次の2つのリストのすべての組み合わせを見つけるためにしようとしている場合:

import itertools 

ListA = "Hi there, This is only a test message. Please enjoy the weather in the park." 
ListB = "work, bank, tree, weather, sun" 
WordsA = ListA.split() 
WordsB = ListB.split() 
#print(WordsA, "\n\n", WordsB)    #This is to show what the new lists are 
c = list(itertools.product(WordsA, WordsB)) 
print(c) 
+0

をしかし、どのようにすることができます私はその後、この組み合わせのPMIを計算しますか?それは私にとってまだ不明な部分です。 – JohnD

+0

はい、私もそれを考えるのに苦労しました。 PMIについての説明を私に与えてもらえますか(私はそれを忘れています)。私は今できることをしてきましたが、あなたがそれを説明することができれば、あなたの問題に対する完全な解決策を考え出すことができます。また、私が助けてくれた(または助けようとしている)と思うなら、本当に役立つように自分のコードを+1してください。ありがとう! –

関連する問題