2016-11-06 34 views
2

我想計算兩個列表元素的點互信息分數。 比方說,我們有兩個列表元素的PMI

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

我怎樣才能再計算所有對的PMI分數(工作,嗨),(工作,有),(工作,此)......(太陽,公園)。

它的工作對我來說,計算一個列表中bigram的採購經理人指數:

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) 

我被困的問題,從計算和利斯塔的雙字母組數組listB的PMI。如果有人能幫助我,我會非常感激。非常感謝!

(這兩個名單是的,當然,我的任務是如何看起來像最小的例子。)

回答

1

如果你正在努力尋找這兩個列表的所有組合:

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我的代碼,因爲它確實有幫助。謝謝! –

相關問題