Skip to content

Commit 95ace0d

Browse files
authored
Add files via upload
1 parent 1958590 commit 95ace0d

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

stringalignment.py

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import Tkinter as Tk
2+
class App(object):
3+
def __init__(self):
4+
self.root = Tk.Tk()
5+
self.root.wm_title("Compare string")
6+
self.label = Tk.Label(self.root, text="Please enter two strings and press OK.")
7+
self.label.grid(column=2, row=0,sticky='W')
8+
self.string1 = Tk.StringVar()
9+
self.string2 = Tk.StringVar()
10+
self.inputbox1= Tk.Label(self.root, text="Source: ",width="10").grid(column=1, row=1, sticky='E')
11+
Tk.Entry(self.root, textvariable=self.string1,width="50").grid(column=2, row=1, sticky='W'+'E')
12+
self.inputbox2= Tk.Label(self.root, text="Target: ",width="10").grid(column=1, row=2, sticky='E')
13+
Tk.Entry(self.root, textvariable=self.string2,width="50").grid(column=2, row=2, sticky='W'+'E')
14+
self.buttontext = Tk.StringVar()
15+
self.buttontext.set("OK")
16+
Tk.Button(self.root,
17+
textvariable=self.buttontext,
18+
command=self.clicked1).grid(column=2, row=3,sticky='W'+'E')
19+
20+
self.label = Tk.Label(self.root, text="")
21+
self.label.grid(column=2, row=4)
22+
#self.label2 = Tk.Label(self.root, text="",width='100')
23+
#self.label2.grid(column=1, row=5, columnspan=3)
24+
#self.text = Tk.Text(self.root,height="10", width='100')
25+
#self.text.grid(column=0, row=6,columnspan=4,rowspan=1)
26+
self.text2 = Tk.Text(self.root,height='20',width='100')
27+
self.text2.grid(column=0, row=7,columnspan=4,rowspan=1)
28+
29+
self.root.mainloop()
30+
31+
def clicked1(self):
32+
source = self.string1.get().replace('\n', '').replace(" "," ")
33+
target = self.string2.get().replace('\n', '').replace(" "," ")
34+
a=[]
35+
b=[]
36+
a.append(0)
37+
b.append(0)
38+
39+
s = source.split(" ")
40+
t = target.split(" ")
41+
if len(s)==1:
42+
s=[source]
43+
if len(t)==1:
44+
t=[target]
45+
mat = [[0 for x in range(len(t)+1)] for x in range(len(s)+1)]
46+
for i in range(len(s)+1):
47+
mat[i][0]=i
48+
for j in range(len(t)+1):
49+
mat[0][j]=j
50+
mat[0][0]=0
51+
for i in range(1,(len(s)+1)):
52+
for j in range(1,(len(t)+1)):
53+
fleft=(mat[i][j-1])+1
54+
ftop=(mat[i-1][j])+1
55+
if s[i-1]==t[j-1] or (s[i-1].replace(",", "").replace('.', '').lower()==t[j-1].replace(",", "").replace('.', '').lower()):
56+
fdia=(mat[i-1][j-1])
57+
else:
58+
fdia=(mat[i-1][j-1])+1
59+
score=min(fleft,ftop,fdia)
60+
mat[i][j]=score
61+
Ldistance =mat[i][j]
62+
result=[]
63+
i=len(s)
64+
j=len(t)
65+
while True:
66+
if (s[i-1]==t[j-1]):
67+
if (mat[i][j]!=mat[i-1][j-1]):
68+
if (mat[i][j]==(mat[i][j-1]+1)):
69+
result.insert(0,["insert",i,(j-1)])
70+
j-=1
71+
elif (mat[i][j]==(mat[i-1][j]+1)):
72+
result.insert(0,["delete",i,(j+1)])
73+
i-=1
74+
elif (mat[i][j]==mat[i-1][j-1]):
75+
#result.insert(0,["nothing",(i-1),(j-1)])
76+
i-=1
77+
j-=1
78+
else:
79+
if (mat[i][j]==(mat[i-1][j-1]+1)):
80+
result.insert(0,["replace",(i-1),(j-1)])
81+
i-=1
82+
j-=1
83+
elif (mat[i][j]==(mat[i][j-1]+1)):
84+
result.insert(0,["insert",i,(j-1)])
85+
j-=1
86+
elif (mat[i][j]==(mat[i-1][j]+1)):
87+
result.insert(0,["delete",(i-1),j])
88+
i-=1
89+
else:
90+
result.append(["what",i,j])
91+
i-=1
92+
j-=1
93+
if mat[i][j]==0:
94+
break
95+
96+
Leditops=result
97+
98+
self.label.configure(text="Levenshtein Distance= "+str(Ldistance))#+'\n'+str(Leditops))
99+
#self.text.delete(0.0,'end')
100+
self.text2.delete(0.0,'end')
101+
#self.text.insert('end', ' '.join(map(str, s)))
102+
n=0
103+
for i in range(len(Leditops)):
104+
if Leditops[i][0]=='delete':
105+
t.insert((Leditops[i][2]+n),s[(Leditops[i][1])])
106+
n+=1
107+
if Leditops[i][0]=='replace':
108+
t.insert((Leditops[i][2]+n),s[Leditops[i][1]])
109+
n+=1
110+
111+
self.text2.insert('end',' '.join(map(str, t)))
112+
target=' '.join(map(str, t))
113+
for si in range(len(source)):
114+
if source[si]==" ":
115+
a.append(si+1)
116+
for ti in range(len(target)):
117+
if target[ti]==" ":
118+
b.append(ti+1)
119+
a.append(len(source))
120+
b.append(len(target))
121+
122+
n=0
123+
for i in range(len(Leditops)):
124+
if Leditops[i][0]=='insert':
125+
self.text2.tag_add("insert", ("1."+str(b[(Leditops[i][2])+n])),("1."+str(b[(Leditops[i][2])+1+n])))
126+
elif Leditops[i][0]=='delete':
127+
#self.text.tag_add("delete", ("1."+str(a[(Leditops[i][1])])), ("1."+str(a[(Leditops[i][1])+1])))
128+
self.text2.tag_add("delete", ("1."+str(b[(Leditops[i][2])+n])), ("1."+str(b[(Leditops[i][2])+1+n])))
129+
n+=1
130+
elif Leditops[i][0]=='replace':
131+
#self.text.tag_add("replace", ("1."+str(a[(Leditops[i][1])])),("1."+str(a[(Leditops[i][1])+1])))
132+
self.text2.tag_add("replace", ("1."+str(b[(Leditops[i][2])+n])),("1."+str(b[(Leditops[i][2])+1+n])))
133+
self.text2.tag_add("replace2", ("1."+str(b[(Leditops[i][2])+n+1])),("1."+str(b[(Leditops[i][2])+2+n])))
134+
n+=1
135+
136+
self.text2.tag_config("insert", background="yellow", foreground="#33CCCC")
137+
self.text2.tag_config("delete", foreground="blue", overstrike=True)
138+
#self.text.tag_config("delete", foreground="blue", overstrike=True)
139+
#self.text.tag_config("replace", foreground="red")
140+
self.text2.tag_config("replace", foreground="red", overstrike=True)
141+
self.text2.tag_config("replace2", foreground="red")
142+
143+
#self.label2.configure(text="hihi",background="blue", foreground="white")
144+
145+
def button_click(self, e):
146+
pass
147+
App()

0 commit comments

Comments
 (0)