from edit_dist_ph import *
import codecs
import re

fi=codecs.open('w2g.txt', 'r', encoding='utf-8')
f2g=fi.read()
fi.close()
w2g_dict={}
for line in f2g.split('\n')[:-2]:
	x=line.split('\t')
	key=x[-1]
	value=int(x[0])
	if (' ' not in key.split(' ')):
		w2g_dict[key]=value	
		
print("Bigram Dictionary is ready...")

error_words=[]
	
def max_prob(temp):
	#print(temp)
	flag=0
	x=0
	y=0
	return_i=0;
	for element in temp:
		if element[2]!= 'N':
			flag=1
			break
	#print(flag)
	if flag==1:	#Look at bigram frequencies
		for i_ind, i in enumerate(temp):
			if i[2]!='N':
				if i[2]>x:
					x=i[2]
					return_i=i_ind
	else: #unigram
		for i_ind, i in enumerate(temp):
			#print(i)
			#print(len(temp))
			if i[1]>y:
				y=i[1]
				return_i=i_ind
	return temp[return_i][0]
	
def bigram_freq(w, p): #w is word and p is preceding word
	#Generate candidates of w
	temp=[]
	candidates=find_candidates(w)
	for (candidate, value) in candidates.items():
		#print(value)
		temp_element=[candidate, value]
		#print(temp_element)
		if(p!='$'):
			bigram_str=p+' '+candidate
			if bigram_str in w2g_dict:
				temp_element.append(w2g_dict[bigram_str])
				#print(temp_element)
			else:
				temp_element.append('N')
				#print(temp_element)
		else:
			temp_element.append('N')
			#print(temp_element)

		temp.append(temp_element)
	successful_candidate=max_prob(temp)
	return successful_candidate

err_list=[]
fi=codecs.open('corpus_error.txt', 'r', encoding='utf-8')
f1=fi.read()
fi.close()
lines=f1.split('\n')
for line in lines:
	w=line.split(' : ')[-1]
	w=re.sub("[ ]+","",w)
	if w!= "":
		err_list.append(w)
#print(err_list)
	
fi=codecs.open('aspell_list.txt', 'r', encoding='utf-8')
f=fi.read()
fi.close()
lines=f.split('\n')

for l_index, line in enumerate(lines):
	line=re.sub('[ .-:-?“”!;,\"\'।‘’\(\)१२३४५६७८९०]+',' ',line) 
	word_list=re.compile("[ ]+").split(line)
	for index, w in enumerate(word_list):
		if (w not in word_dict or w in err_list) and w not in [' ','','को']:	#considering non word errors
			if index==0:
				suggestion=bigram_freq(w, '$')
			else:
				suggestion=bigram_freq(w, word_list[index-1])				
			error_words.append([l_index, index, w, suggestion]) 

replaced=[]
for x in error_words:
	print(x[3])
	if x[2]!=x[3]:
		replaced.append(x)
		#print(x)			
#print(len(error_words))
#print(len(replaced))
