Bigrams and ranks of words
<p>I am using this code to get the frequency of bigrams:</p>

<pre><code>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
</code></pre>

<p>The output is :</p>

<pre><code>('the', 'cat') 1
('dog', 'in') 1
('cat', 'jumped') 1
('jumped', 'over') 1
('in', 'the') 1
('over', 'the') 1
('dog', 'house') 1
('the', 'dog') 2
</code></pre>

<p>What I need is for the bigrams to be listed, but instead of each word, I need to word's rank to be printed. When I mean "rank" I mean the word with the highest frequency has rank 1, the second has rank 2 ect... Here the ranks are : 1.the 2.dog and the ones with equal frequency are assigned ranks in decreasing order. 3.cat 4.jumped 5.over etc..</p>

<p>eg </p>

<pre><code>1 3 1
</code></pre>

<p>instead of</p>

<pre><code>('the', 'cat') 1
</code></pre>

<p>I believe that to do this I need a dictionary with words and their rank, but I am stuck and don't know how to proceed. What I have is:</p>

<pre><code>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
</code></pre>
