2012-01-19 9 views
1

私はバイグラムの頻度を取得するには、このコードを使用しています:バイグラムと言葉のランク

text1='the cat jumped over the dog in the dog house' 
text=text1.split() 

counts = defaultdict(int) 
for pair in nltk.bigrams(text): 
    counts[pair] +=1 

for c, pair in ((c, pair) for pair, c in counts.iteritems()): 
    print pair, c 

出力は次のようになります。

('the', 'cat') 1 
('dog', 'in') 1 
('cat', 'jumped') 1 
('jumped', 'over') 1 
('in', 'the') 1 
('over', 'the') 1 
('dog', 'house') 1 
('the', 'dog') 2 

私は必要なもの、一覧表示するバイグラムのためですが、各単語の代わりに、私は単語のランクを印刷する必要があります。私が「ランク」を意味するとき、最も高い頻度の単語がランク1を持っていて、2番目のランクがランク2を持っていることを意味します...ここでランクは次のとおりです:1.the 2.dogと等しい周波数を持つランクが降順。 3.cat 4.jumped 5.overなど。

例えば

1 3 1 

代わりの

('the', 'cat') 1 

私はそれは私が単語とそのランクの辞書を必要とするこれを行うには信じていますが、私は立ち往生し、どのように進むべきかを知らない。私は何を持っていることは次のとおりです。

fd=FreqDist() 
ranks=[] 
rank=0 
for word in text: 
    fd.inc(word) 
for rank, word in enumerate(fd): 
    ranks.append(rank+1) 

word_rank = {} 
for word in text: 
    word_rank[word] = ranks 

print ranks 
+0

「( 'the'、 'cat')1 => '1 3 1' ,? 'cat'はなぜ3であるのですか?それは2ではいけませんか? ( 'cat'はあなたのテキストの2番目の単語です) – juliomalegria

+0

私が「ランク」を意味するときは、最も高い頻度の単語がランク1を持ち、2番目のランクが2ランクです...ここでランクは次のとおりです。犬と同じ頻度のものには降順でランクが割り当てられます。 3.cat 4.jumped 5.over ect ... – Julia

+0

最初の「犬」が最初の「 –

答えて

3

は、すでに作成されているcountsと仮定すると、結果を取得する必要があり、次はあなたが欲しい:

freq = defaultdict(int) 
for word in text: 
    freq[word] += 1 

ranks = sorted(freq.keys(), key=lambda k: (-freq[k], text.index(k))) 
ranks = dict(zip(ranks, range(1, len(ranks)+1))) 

for (a, b), count in counts.iteritems(): 
    print ranks[a], ranks[b], count 

出力:ここ

1 3 1 
2 6 1 
3 4 1 
4 5 1 
6 1 1 
5 1 1 
2 7 1 
1 2 2 

は、いくつかの中間値であることどのように動作するかを理解するのに役立ちます:

>>> dict(freq) 
{'house': 1, 'jumped': 1, 'over': 1, 'dog': 2, 'cat': 1, 'in': 1, 'the': 3} 
>>> sorted(freq.keys(), key=lambda k: (-freq[k], text.index(k))) 
['the', 'dog', 'cat', 'jumped', 'over', 'in', 'house'] 
>>> dict(zip(ranks, range(1, len(ranks)+1))) 
{'house': 7, 'jumped': 4, 'over': 5, 'dog': 2, 'cat': 3, 'in': 6, 'the': 1} 
+0

素晴らしい、ありがとう! – Julia

+0

フォローアップの質問:この生成された行列をファイルに保存するにはどうすればよいですか?ありがとうございました! – Julia

+1

まだ別の質問をしても気にならない場合は、[ファイルへの書き込み方法](http://stackoverflow.com/search?q=python+write+to+file)にいくつか質問があります。 –

0
text1='the cat jumped over the dog in the dog house'.split(' ') 
word_to_rank={} 
for i,word in enumerate(text1): 
    if word not in word_to_rank: 
     word_to_rank[word]=i+1 

from collections import Counter 
word_to_frequency=Counter(text1) 

word_to_tuple={} 
for word in word_to_rank: 
    word_to_tuple[word]=(-word_to_frequency[word],word_to_rank[word]) 

tuple_to_word=dict(zip(word_to_tuple.values(),word_to_tuple.keys())) 

sorted_by_conditions=sorted(tuple_to_word.keys()) 

word_to_true_rank={} 
for i,_tuple in enumerate(sorted_by_conditions): 
    word_to_true_rank[tuple_to_word[_tuple]]=i+1 

def fix(pair,c): 
    return word_to_true_rank[pair[0]],word_to_true_rank[pair[1]],c 

pair=('the', 'cat') 
c=1 
print fix(pair,c) 

pair=('the', 'dog') 
c=2 
print fix(pair,c) 


>>> 
(1, 3, 1) 
(1, 2, 2)