# -*- 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 :   ')

vowel_text=open(lang,"r") #Only modification required here
vowels=vowel_text.read().split()


def vowelcheck (letter):
	for onevow in vowels:
		if (letter==onevow):
			return 1
	return 0

#Scan File name and open it
filename = input('Please Enter file name:  ')		

text_file = open(filename, "r")
words = text_file.read().split()

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

#Print to a file 
for oneword in words: 
	
	first=0
	for index in range(len(oneword)):	
		if(index==len(oneword)-1):
			intFile.write(oneword[first:index+1]+"\n")
			break;
		if(index>0):
			if(vowelcheck(oneword[index])==1):
				if(index==len(oneword)-2):
					intFile.write(oneword[first:index+2]+"\n")
					break
				
				else:
					intFile.write(oneword[first:index+1]+"\n")
					first=index+1

		

	

text_file.close()
intFile.close()
vowel_text.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:1000])

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:1000]
#for eachstring in samples:
#		print(eachstring)

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

# Y (position) values for each sample
pos = arange(1000) + 0.5

# 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('Syllable Frequency.png')

data.close()



#Plot Zone






bigramFile.write(bigramString)

bigramFile.close()