I try to make a simple spell checking programm by given two strings and adapt the first to the second one. If the strings have the same length my code works fine but if they're different, then the problems start. It only executes the if-statements once and stops after that. If I remove the break points, I get an IndexError: list index out of range.
Here is my code:
#!python
# -*- coding: utf-8 -*-
def edit_operations(first,second):
a = list(first)
b = list(second)
counter = 0
l_a = len(a)
l_b = len(b)
while True:
if a == b:
break
if l_a > l_b:
if a[counter] != b[counter]:
a[counter] = ""
c = "".join(a)
print "delete", counter+1, b[counter], c
counter += 1
l_a -= 1
break
if l_a < l_b:
if a[counter] != b[counter]:
c = "".join(a)
c = c[:counter] + b[counter] + c[counter:]
print "insert", counter+1, b[counter], c
counter += 1
l_a += 1
break
if a[counter] != b[counter]:
a[counter] = b[counter]
c = "".join(a)
print "replace", counter+1, b[counter], c
counter += 1
else:
counter += 1
if __name__ == "__main__":
edit_operations("Reperatur","Reparatur")
edit_operations("Singel","Single")
edit_operations("Krach","Stall")
edit_operations("wiederspiegeln","widerspiegeln")
edit_operations("wiederspiglen","widerspiegeln")
edit_operations("Babies","Babys")
edit_operations("Babs","Babys")
edit_operations("Babeeees","Babys")
This is the output I get:
replace 4 a Reparatur
replace 5 l Singll
replace 6 e Single
replace 1 S Srach
replace 2 t Stach
replace 4 l Stalh
replace 5 l Stall
delete 3 d widerspiegeln
replace 3 d widderspiglen
replace 4 e wideerspiglen
replace 5 r widerrspiglen
replace 6 s widersspiglen
replace 7 p widersppiglen
replace 8 i widerspiiglen
replace 9 e widerspieglen
replace 11 e widerspiegeen
replace 12 l widerspiegeln
delete 4 y Babes
insert 4 y Babys
delete 4 y Babeees
By the last 3 lines you can see my problem and I'm kinda desperate right now. Hopefully someone could give me a hint what is wrong with it
Aucun commentaire:
Enregistrer un commentaire