# -*- coding: utf-8 -*-
import sys
################## Read various files ###################################################
phoneme = open('hi_phonet.dat')
unigram = open('filteredcorpus.txt').read().decode('utf-8').strip().split('\n')
alpha = open('alphabets').read().translate(None,'\n').decode('utf-8')
bigram = open('bigramfreq.txt')


#####################    Replace alphabet by Phonemes   ############################################################
def r_phoneme(phone, a, b, word):
	index = 0;
	for i in word:
		for j in range(len(a)):
			if(i.encode('utf-8') == a[j]):
				if(b[j]  != "_"):
					w = word[0:index]+b[j].decode('utf-8')+word[index+1:]
					phone.append(w)
				else:
					w = word[0:index]+word[index+1:]
					phone.append(w)
		index = index + 1;
	index =0;
	for i in word:
		for j in range(len(b)):
			if(i.encode('utf-8') == b[j]):
				if(b[j]  != "_"):
					w = word[0:index]+a[j].decode('utf-8')+word[index+1:]
					phone.append(w)
		index = index + 1;

#############################  Construct UniGram Dictionary ###########################################################
unigramdict = {}
for line in unigram:					
	ele=line.split()
	ele[1] = ele[1].strip();
	unigramdict[ele[1]] = int(ele[0].decode('utf-8'))

#############################  Construct BiGram Dictionary ###########################################################
bigramdict = {}
for line in bigram:
	el = line.split(' ');
	el[0]=el[0].strip()
	el[1]=el[1].strip()
	el[2]=el[2].strip()
	st = ' '.join([el[1], el[2]])
	st = st.decode('utf-8')
	bigramdict[st] = int(el[0].decode('utf-8'))


############################   BiGram Suggestions   ##########################################################
def find_bigram(bigram, bigram_sug):
	try:
		fr = bigramdict[bigram];
		bigram_sug.append([fr, bigram]);
	except:
		return 

##########################    Find unigrams in dictionary #######################################################
def check(lwords, x):
	for w in lwords:
		try:
			fr = unigramdict[w];
			x.append([fr, w]);
		except:
			continue;

############################  Check existence of unigram ##########################################################
def check2(word):
	x=0
	try:
		x = unigramdict[word.decode('utf-8')]
	except:
		x=0
	if(x!=0):
		return 1
	else:
		return 0
	
##############################  Find Edit1-Distance  ###############################################################
def edits1(word):
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alpha if b]
   inserts    = [a + c + b for a, b in splits for c in alpha]
   return set(deletes + transposes + replaces + inserts)


a = []
b = []
for line in phoneme:
	ele = line.split(" ")
	ele[0] = ele[0].strip()
	ele[1] = ele[1].strip()
	a.append(ele[0])
	b.append(ele[1])

########## Suggestions(Bigram+Phonetics+Unigram) #############################
spell = open("hi-spell2.txt")
prev_word = "";
for line in spell:
	prev_word = ""
	print "----------------------------------------------------------------------------------"
	print line
	el=line.split(" ")
	for x in el:
		x=x.strip();	
		if(check2(x) == 1):
			x=x.decode('utf-8').strip()
			prev_word = x
			continue;
		else:
			x=x.decode('utf-8').strip()
			edits = edits1(x)
			uni=[]	
			check(edits, uni)
			uni.sort(key = lambda t:t[0], reverse=True)
			print "\nUnigram suggestions for word: ", x.encode('utf-8')
			for j in range(3):
				if (uni and j<len(uni)):
					print uni[j][1].encode('utf-8')
					
			bigram_sug = []
			if(prev_word != ""):
				for j in range(3):
					if (uni and j<len(uni)):
						prev_word = prev_word.strip();
						uni[j][1] = uni[j][1].strip()
						bigram = ' '.join([prev_word, uni[j][1]])
						find_bigram(bigram, bigram_sug)
				
				bigram_sug.sort(key = lambda t:t[0], reverse=True)
				print "\nBigram Suggestions: "
				if(bigram_sug):
					print "Actual Suggestion"
					for w in bigram_sug:
						print w[1].encode('utf-8')
				else:
					print "None"
				 
			phone = []					
			index=0;
			r_phoneme(phone, a, b, x)	
			z = []
			check(phone, z)
			z.sort(key = lambda t:t[0], reverse=True)	
			print "\nPhonetic Suggestions: "
			if(z):
				for w in z:
					print w[1].encode('utf-8')
	
			else:
				print "None"
			prev_word = x
