I am new to Python. I used collections.Counter to count the most frequent bigrams in a text:
import sys, codecs
import nltk, collections
from nltk.util import ngrams
InputFile = codecs.open("testin.txt", 'r', 'utf-8')
text=InputFile.read().lower()
tokens = text.split()
bi_tokens = ngrams(tokens, 2)
bi_freq = collections.Counter(bi_tokens)
If I use:
for row in bi_freq.most_common(100):
print (row)
The result appears as:
(('star', 'wars'), 29)
(('blu', 'ray'), 21)
If I use:
for row in bi_freq.most_common(1000):
print (row[0], "\t", row[1])
The result appears a bit cleaner as:
('star', 'wars') 29
('blu', 'ray') 21
I would like to get to:
star wars 29
blu ray 21
which I would import into a spreadsheet in two columns with tab as a separator.
So my question is: how do I access each tuple value, when the tuple is a key in a dictionary, so that I can concatenate them into a string? Thanks in advance.
Use join()
to create a delimited string from a sequence.
for bigram, c in b_freq.most_common(1000):
print(" ".join(bigram), c)