# -*- 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("bengali","r") #Only modification required here
vowels=vowel_text.read().split()


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


fullvowel_text=open("bengalifullvowel","r") #Only modification required here
fullvowels=fullvowel_text.read().split()


def fullvowelcheck (letter):
	for onevow in fullvowels:
		if (letter==onevow):
			return 1
	return 0

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

intFile = open('intermediate', 'w+')
text_file2 = open("corpusBengali", "r")
words = text_file2.read().split()


test=open('test','w+')

#Print to a file 
for oneword in words: 
	marker=[]
	for i in range (0,len(oneword)):
		marker.append('U')
	for index in range(0,len(oneword)): #first pass (Step 1 Done)
		if(fullvowelcheck(oneword[index])==1 or vowelcheck(oneword[index])==1):
			marker[index]='F'
		elif(vowelcheck(oneword[index])==0 and index<len(oneword)-1):
			if(vowelcheck(oneword[index+1])==1 or fullvowelcheck(oneword[index+1])==1):
				marker[index]='F'
			elif(oneword[index+1]=='\u09cd'): #Halanta
				marker[index]='H'

	for index in range(0,len(oneword)): #Second pass (Step 2 Done)
		if(marker[index]=='U' and oneword[index]=='\u09df' and index>0 and (marker[index-1]=='U' or oneword[index-1]=='\u09c0' or oneword[index-1]=='\u09bf' or oneword[index-1]=='\u0987' or oneword[index-1]=='\u0988'or oneword[index-1]=='\u09c1' or oneword[index-1]=='\u09c2' or oneword[index-1]=='\u0989' or oneword[index-1]=='\u09c3' or oneword[index-1]=='\u098b' or oneword[index-1]=='\u098a' )):
			marker[index]='F'

	for index in range(0,len(oneword)): #third pass (Step 3 Done)
		if(marker[index]=='U' and index>0 and marker[index-1]=='H' and vowelcheck(oneword[index-1])==0 and fullvowelcheck(oneword[index-1])==0 and (oneword[index]=='\u09df' or oneword[index]=='\u09b0' or oneword[index]=='\u09b2' or oneword[index]=='\u09ad')):
			marker[index]='F'


	for index in range(0,len(oneword)): #4th pass (Step 5 Done)
		if(marker[index]=='F'):
			break
		if(vowelcheck(oneword[index])==0 and fullvowelcheck(oneword[index])==0 and marker[index]=='U'):
			marker[index]=='F'	

	for index in range(0,len(oneword)): #5th pass (Step 6 Done)
		l=len(oneword)-1
		if(vowelcheck(oneword[l-index])==0 and fullvowelcheck(oneword[l-index])==0):
			if(marker[l-index]=='U'):
				marker[l-index]='H'
			break

	for index in range(0,len(oneword)): #6th pass (Step 7)
		if(vowelcheck(oneword[index])==0 and fullvowelcheck(oneword[index])==0 and marker[index]=='U'):
			if(vowelcheck(oneword[index+1])==0 and fullvowelcheck(oneword[index+1])==0 and marker[index+1]=='H'):
				marker[index]='F'

	for index in range(0,len(oneword)): #7th pass (Step 8)
		if(vowelcheck(oneword[index])==0 and fullvowelcheck(oneword[index])==0 and marker[index]=='U'):
			if(index>0 and index<len(oneword)-1 and marker[index-1]=='F' and (marker[index+1]=='U' or marker[index+1]=='F')):
				marker[index]='H'
			else:
				marker[index]='F'

	test.write("".join(marker))
	test.write("\n")

	breaker=[]
	for i in range (0,len(oneword)):
		breaker.append('0')

	for index in range(0,len(oneword)): #First break loop
		if(index<len(oneword)-1 and breaker[index]=='0'):
			if(marker[index]=='F' and marker[index+1]=='F'):
				if(vowelcheck(oneword[index+1])==0 and fullvowelcheck(oneword[index+1])==0):
					if(vowelcheck(oneword[index+1])==1):
						breaker[index+1]='1'
					else:
						breaker[index]='1'



	for index in range(0,len(oneword)): #2nd break loop
		if(index<len(oneword)-1 and breaker[index]=='0'):
			if(marker[index]=='H' and marker[index+1]=='F'): 
				flag=0
				for index2 in range(0,len(oneword)):
					if(index2==index):
						break
					if(vowelcheck(oneword[index2])==0 and fullvowelcheck(oneword[index+1])):
						flag=1
						break
				if(flag==0):
					if(vowelcheck(oneword[index+1])==1):
						breaker[index+1]='1'
					else:
						breaker[index]='1'
			'''
			elif(marker[index]=='F' and marker[index+1]=='H'):
				flag=0
				for index2 in range(0,len(oneword)):
					if(index2==index+1):
						break
					if(vowelcheck(oneword[index2])==0 and fullvowelcheck(oneword[index+1])):
						flag=1
						break
				if(flag==0):
					breaker[index]='1'
			'''
		if(index==len(oneword)-1 and breaker[index]=='0'):
			breaker[index]='1'

	last=-1
	for index in range(0,len(oneword)):
		
		if(breaker[index]=='1'):
			intFile.write(oneword[last+1:index+1]+"\n")
			last=index










text_file2.close()
intFile.close()
vowel_text.close()
fullvowel_text.close()

# plot code here 

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(',', '')

bigramString = bigramString.replace('n', '') #Bengali Exclusive

#print(bigramString)


#Plot Zone




matplotlib.rc('font', family='cmtt10')
# 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()
