from Tkinter import *

from tkFileDialog import *

from tkSimpleDialog import *

from array import *

class application(Frame):

def cal(self):

S = self.textS.get("1.0", END)

T = self.textT.get("1.0", END)

lens = len(S)

lent = len(T)

if lens == 1 and lent == 1:

return

self.maxlen = 2

dp = [([0] * lent) for i in range(lens)]

bk = [(['-'] * lent) for i in range(lens)]

for i in range(1,lent):

dp[0][i] = dp[0][i - 1] + self.Sci.get()

bk[0][i] = '-'

for i in range(1,lens):

dp[i][0] = dp[i - 1][0] + self.Scd.get()

bk[i][0] = '|'

for i in range(1,lens):

for j in range(1,lent):

if S[i - 1] == T[j - 1]:

dp[i][j] = dp[i - 1][j - 1]

bk[i][j] = '\\'

else:

dp[i][j] = min(dp[i - 1][j] + self.Scd.get(), dp[i][j - 1] + self.Sci.get(), dp[i - 1][j - 1] + self.Scs.get())

if dp[i][j] == dp[i - 1][j] + self.Scd.get():

bk[i][j] = '|'

if dp[i][j] == dp[i - 1][j - 1] + self.Scs.get():

bk[i][j] = '\\'

self.maxlen = max(len(str(dp[i][j])),self.maxlen)

self.maxlen += 2

self.Dis.delete(first = 0, last = END)

self.Dis.insert(END, dp[lens - 1][lent - 1])

if self.backtrack.get() == TRUE:

self.BM.insert(END, '\n--------------\n')

self.BM.insert(END, ' ')

for i in range(0,lent - 1):

self.BM.insert(END, ' ' + T[i])

self.BM.insert(END, '\n 0')

for i in range(0,lens):

if i != 0:

self.BM.insert(END, S[i - 1])

for j in range(0,lent):

if i == 0 and j == 0:

continue

if i == 0:

self.BM.insert(END, ' -')

if j == 0:

self.BM.insert(END, ' |')

if i != 0 and j != 0:

self.BM.insert(END, ' ' + bk[i][j])

self.BM.insert(END, '\n')

if self.fulledit.get() == TRUE:

self.FD.insert(END, '\n--------------\n')

self.FD.insert(END, ' ' * self.maxlen * 2)

for i in range(0,lent - 1):

self.FD.insert(END, T[i].ljust(self.maxlen))

self.FD.insert(END, '\n' + ' ' * self.maxlen)

for i in range(0,lens):

if i != 0:

self.FD.insert(END, S[i - 1].ljust(self.maxlen))

for j in range(0,lent):

self.FD.insert(END, str(dp[i][j]).ljust(self.maxlen))

self.FD.insert(END, '\n')

if self.align.get() == TRUE:

self.AL.insert(END, '\n--------------\n')

j = lent - 1

i = lens - 1

res = []

while TRUE:

res.append(bk[i][j])

if bk[i][j] == '\\':

i -= 1

j -= 1

elif bk[i][j] == '|':

i -= 1

elif bk[i][j] == '-':

j -= 1

if i == 0 and j == 0:

break

res.reverse()

i = j = k = 0

So = ['a'] * len(res)

way = ['a'] * len(res)

Ta = ['a'] * len(res)

for p in range(len(res)):

if res[p] == '\\':

So[k] = S[i]

Ta[k] = T[j]

if S[i] == T[j]:

way[k] = '|'

else:

way[k] = ' '

i += 1

j += 1

if res[p] == '|':

So[k] = S[i]

Ta[k] = '-'

way[k] = ' '

i += 1

if res[p] == '-':

So[k] = '-'

Ta[k] = T[j]

way[k] = ' '

j += 1

k += 1

for i in range(len(res)):

self.AL.insert(END, ' ' + So[i])

self.AL.insert(END, '\n')

for i in range(len(res)):

self.AL.insert(END, ' ' + way[i])

self.AL.insert(END, '\n')

for i in range(len(res)):

self.AL.insert(END, ' ' + Ta[i])

self.AL.insert(END, '\n')

def __init__(self, master):

Frame.__init__(self,master)

self.Sci = 1

self.Scd = 1

self.Scs = 1

self.maxlen = 2

self.pack()

self.create()

def changeRange(self):

while(TRUE):

Range = askinteger("Update the Range","Please input the range")

if Range > 0:

break

if Range == None:

break

self.Scd.config(to = Range)

self.Sci.config(to = Range)

self.Scs.config(to = Range)

def menu(self):

menu = Menu()

self.master.config(menu = menu)

option = Menu(menu)

menu.add_cascade(label = "Option", menu = option)

option.add_command(label = "Range of modify cost", command = self.changeRange)

def getOut(self, name,father):

self.frm = LabelFrame(father, text = name, padx = 10, pady = 5)

lb = Text(self.frm, width = 30, wrap = NONE)

lb.config(font = ("Courier", 10))

sl = Scrollbar(self.frm)

sl.set(0.5,1)

sl.pack(side = RIGHT, fill = Y)

lb['yscrollcommand'] = sl.set

lb.pack(side = LEFT)

sl['command'] = lb.yview

sl = Scrollbar(self.frm, orient = HORIZONTAL)

sl.set(0.5,1)

sl.pack(fill = X)

lb['xscrollcommand'] = sl.set

lb.pack(side = TOP)

sl['command'] = lb.xview

self.frm.pack(side = LEFT)

return lb

def getFileS(self):

fd = askopenfilename()

if fd == "":

return

tmpfile = open(fd,"r")

self.textS.delete("1.0", END)

self.textS.insert("1.0", tmpfile.readline()[:-1])

tmpfile.close()

def getFileT(self):

fd = askopenfilename()

if fd == "":

return

tmpfile = open(fd,"r")

self.textT.delete("1.0", END)

self.textT.insert("1.0", tmpfile.readline()[:-1])

tmpfile.close()

def create(self):

self.menu()

Input = LabelFrame(self, text = "Input", padx = 10, pady = 5)

frm_L = LabelFrame(Input, text = "Input S & T", padx = 10, pady = 5)

self.frm_LS = Frame(frm_L)

Label(self.frm_LS, text = "S", font = ('Arial', 12), padx = 10).pack(side = LEFT)

self.textS = Text(self.frm_LS,height = 3, width = 18, padx = 10)

self.textS.pack(side = LEFT)

Button(self.frm_LS, text = "Open", command = self.getFileS).pack(side = LEFT)

self.frm_LS.pack(side = TOP)

frm_LT = Frame(frm_L)

Label(frm_LT, text = "T", font = ('Arial', 12), padx = 10).pack(side = LEFT)

self.textT = Text(frm_LT,height = 3, width = 18, padx = 10)

self.textT.pack(side = LEFT)

Button(frm_LT, text = "Open", command = self.getFileT).pack(side = LEFT)

frm_LT.pack(side = TOP)

frm_L.pack(side = LEFT)

frm_M = LabelFrame(Input, text = "Cost of modify", padx = 10, pady = 5)

frm_Mcd = Frame(frm_M)

Label(frm_Mcd, text = "deletion", font = ('Arial', 11), width = 10).pack(side = LEFT)

self.Scd = Scale(frm_Mcd, orient = HORIZONTAL, width = 10, from_ = 1, to = 5)

self.Scd.pack(side = LEFT)

frm_Mcd.pack(side = TOP)

frm_Mci = Frame(frm_M)

Label(frm_Mci, text = "insertion", font = ('Arial', 11), width = 10).pack(side = LEFT)

self.Sci = Scale(frm_Mci, orient = HORIZONTAL, width = 10, from_ = 1, to = 5)

self.Sci.pack(side = LEFT)

frm_Mci.pack(side = TOP)

frm_Mcs = Frame(frm_M)

Label(frm_Mcs, text = "substitution", font = ('Arial', 11), width = 10).pack(side = LEFT)

self.Scs = Scale(frm_Mcs, orient = HORIZONTAL, width = 10, from_ = 1, to = 5)

self.Scs.pack(side = LEFT)

frm_Mcs.pack(side = TOP)

frm_R = LabelFrame(Input, text = "Pick the output", padx = 10, pady = 5)

self.fulledit = BooleanVar()

Checkbutton(frm_R, text = "full edit",height = 2, width = 18, anchor = W, variable = self.fulledit).pack(side = TOP, anchor = W)

self.backtrack = BooleanVar()

Checkbutton(frm_R, text = "backtrack matrix", height = 2, width = 18, anchor = W, variable = self.backtrack).pack(side = TOP, anchor = W)

self.align = BooleanVar()

Checkbutton(frm_R, text = "alignment", height = 2, width = 18, anchor = W, variable = self.align).pack(side = TOP, anchor = W)

convert = Frame(self)

Button(convert, text = "Compare", command = self.cal).pack()

output = LabelFrame(self, text = "Output", padx = 10)

Disfrm = Frame(output, padx = 10, pady = 5)

Label(Disfrm, text = "Distance", padx = 10).pack(side = LEFT)

self.Dis = Entry(Disfrm, width = 10)

self.Dis.pack(side = LEFT)

Disfrm.pack(side = TOP)

self.FD = self.getOut("full edit", output)

self.BM = self.getOut("backtrack matrix", output)

self.AL = self.getOut("alignment", output)

frm_L.pack(side = LEFT)

frm_M.pack(side = LEFT)

frm_R.pack(side = LEFT)

Input.pack()

convert.pack()

output.pack()

if __name__ == "__main__":

root = Tk()

root.title("Distance Calculator")

root.geometry("900x700")

root.resizable(width = True, height = True)

app = application(root)

root.mainloop()