# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re, collections
import unicodedata
import re
import codecs

def words(text): return text

def edit_option1(word):
   split_word     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   delete_word    = [a + b[1:] for a, b in split_word if b]
   transpose_word = [a + b[1] + b[0] + b[2:] for a, b in split_word if len(b)>1]
   replace_word   = [a + c + b[1:] for a, b in split_word for c in alphabet if b]
   insert_word    = [a + c + b     for a, b in split_word for c in alphabet]
   return set(delete_word + transpose_word + replace_word + insert_word)

def known_edit_option2(word):
    return set(e2 for e1 in edit_option1(word) for e2 in edit_option1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS)

def correction(word):
    candidate_set=set()
    candidate_set=set(known([word])) |set(known(edit_option1(word))) | set([word])
    z={}
    for x in candidate_set:
       w={x:NWORDS.get(x)}
       z.update(w)
    suggestions=sorted(z.items(), key=lambda x:x[1],reverse=True)
    return (suggestions)
       
      

alphabet = 'ँंअइआउईएओकऔगखङघछचझजटञडठणढथतधदनफपभबयमरळलवषशहस:िाुीृूेैो्ौढ़ड़़'
a= codecs.open("AllignWordFreq_Vishal.txt", "r", "utf-8")
a=a.readlines()
NWORDS = collections.defaultdict(lambda: 1)
for i in range(len(a)):
    truth=a[i].split()
    dict={truth[0]:truth[1]}
    NWORDS.update(dict)
b= codecs.open("bigramVishal.txt", "r", "utf-8")
b=b.readlines()
BWORDS = collections.defaultdict(lambda: 1)
for i in range(len(b)):
    truth=b[i].split()
    bi=truth[0]+" "+truth[1]
    s=len(truth)-1
    dict={bi:truth[s]}
    BWORDS.update(dict)
c= codecs.open("sentences.txt", "r", "utf-8")
c=c.readlines()
for line in c:
   words=line.split()
   error=list()
   for i in words:
      if(int(NWORDS[i])<100):
         error.append(i) 
   for k in error:
      possible=correction(k)
      maxi=0
      correct_data=""
      for i in possible:
         position=words.index(k)
         if(position==0):
             b2="<s>"+" "+words[position]
             strength=int(BWORDS[b2])
             print strength
         if(position==len(words)-1):
             b1=words[position]+"</s>"
             strength=int(BWORDS[b1])
             print strength
         else:
            b1=words[position-1]+" "+i[0]
            b2=i[0]+" "+words[position+1]
            strength=int(BWORDS[b1])*int(BWORDS[b2])
         if(strength>maxi):
            maxi=strength
            correct_data=i
      print correct_data[0]
