Reputation: 21
I am struggling with this piece of code . I need to create a 1 and 2 gram model and map the grams with their frequency; After I need to write the 2 models to one EXCEL file in two different sheets.. I come to here displaying the 2 model gram and frequency but struggling on on how to append the outcome and create the excel file.
import nltk
nltk.download('punkt')
f = open('data.json','r')
raw = f.read()
tokens = nltk.word_tokenize(raw)
#Create your bigrams
bgs = nltk.bigrams(tokens)
#compute frequency distribution for all the bigrams in the text
fdist = nltk.FreqDist(bgs)
for k,v in fdist.items():
print (k,v)
Thank you
Upvotes: 1
Views: 1102
Reputation: 963
This code will export the frequence distribution in a csv file. :
import csv
import nltk
nltk.download('punkt')
f = open('data.json','r')
raw = f.read()
tokens = nltk.word_tokenize(raw)
#Create your bigrams
bgs = nltk.bigrams(tokens)
#compute frequency distribution for all the bigrams in the text
fdist = nltk.FreqDist(bgs)
with open("fdist.csv", "w") as fp:
writer = csv.writer(fp, quoting=csv.QUOTE_ALL)
writer.writerows(fdist.items())
Upvotes: 1