# -*- coding: utf-8 -*-
import nltk
from matplotlib.pyplot import *
from matplotlib import pyplot #for log to work
import matplotlib.pyplot as plt
from pylab import *


lang = input('Choose Language: bengali, english, latinDevanagari :   ')
#Scan File name and open it
filename = input('Please Enter file name:  ')		

text_file = open(filename, "r")
data=text_file.read()

intFile = open('intermediate', 'w')

#Print to a file 
for char in data: 
	if(char!=' '):
		intFile.write(char+"\n")
			


text_file.close()
intFile.close()

# plot code here 
matplotlib.rc('font', family='cmtt10')
data=open("intermediate","r")
bigramFile = open('bigramdata', 'w+')
fdist1=nltk.FreqDist(data)
#print (fdist1)
vocab=fdist1.keys()
#print(list(vocab)[-10:])
#print(fdist1.items())
#print(fdist1.most_common()[0:10])
bigramString=str(fdist1.most_common()[0:82])

bigramString = bigramString.replace('[','')
bigramString = bigramString.replace(']','')
bigramString = bigramString.replace('(', '')
bigramString = bigramString.replace(')', '\n')
bigramString = bigramString.replace('\\n', '')
bigramString = bigramString.replace(',', '')
if(lang=="bengali"):
		bigramString = bigramString.replace('n', '')

print(bigramString)


#Plot Zone





# Get samples and their frequencies
samples = [item for item, _ in fdist1.most_common()][0:82]
#for eachstring in samples:
#		print(eachstring)

freqs = log([fdist1[sample] for sample in samples][0:82])

# Y (position) values for each sample
pos = arange(82) + 0.65

# Use matplotlib to plot it
figure(figsize=(20, 200), dpi=100)
barh(pos, freqs, align='center')
yticks(pos - 0.65, samples)
xlabel('Occurrences')
title('Most frequent words')
savefig('Letter Frequency.png')

data.close()



#Plot Zone






bigramFile.write(bigramString)

bigramFile.close()