2015-09-01 136 views
0

對不起,但對我來說很困難: 我有一些基本頻率的代碼用於某些文本,它代表「most_common」模式下的輸出。但它用文字表示。Python的NLTK字詞頻率在分區

def sym(senten): 
    stopwords = nltk.corpus.stopwords.words("english") 
    V = [",", ".", "'", "(", ")", '"', "'", ":", "it", "may", "I", "the", "but", "two", "this", "since", "for", "whether", "and", "?", "if", "even", "Mr.", "also", "at", "p."] 
    content = [w for w in senten if w not in stopwords and w not in V] 
    fd = nltk.FreqDist(content) 
    fdc = fd.most_common(75) 
    return fdc 

爲了進一步分析,我需要頻率代碼,它代表我在sents(句子)中的輸出。所以,輸出必須向我顯示按位置排列的單詞頻率選擇的sents。

我有一些想法用「製表」來實現它。有一個代碼(例如):

S= ["proposition", "modus", "logic", "2"] #The most frequent words(for example) 
cfd = nltk.ConditionalFreqDist(
    (senten, S) 
    for senten in senten 
    for S in senten) 
print cfd.tabulate(conditions = senten, 
      samples=S) 

它的作品,但也有大約不常用的單詞的句子太多毫無意義的數據。

我會感謝您的想法,這可以解決我的問題。

+0

你的意思是* sents *,*句子*! –

+0

原因!我很抱歉。 – mannheim

+2

你可以請示例輸入句子和樣品輸出嗎? –

回答

0

分兩步做。你已經有了能找到最常用詞的代碼,所以這很好。現在建立一個索引(一個字典),它會告訴你每個單詞,哪些句子包含它。所以這本詞典中的關鍵詞應該是單詞,價值將成爲整個句子的列表 - 基本上與你試圖做到這一點相反。您將多次添加每個句子(不要擔心它實際上不會被複制,因此效率很高)。

這本詞典不需要計算任何東西 - 你只會看單詞。因此,您可以使用普通的dict,或使用collections.defaultdict以方便使用。最後一步是使用當前函數獲取最常見單詞的列表,並且對於每個這樣的單詞,您可以簡單地請求包含它的所有句子。夠清楚了嗎?

+0

謝謝!我會試試看。 – mannheim