Skip to content

Instantly share code, notes, and snippets.

@jlee7
Created January 19, 2018 19:12
Show Gist options
  • Save jlee7/02bf06a18a5dd9a6947b77a816db1f5c to your computer and use it in GitHub Desktop.
Save jlee7/02bf06a18a5dd9a6947b77a816db1f5c to your computer and use it in GitHub Desktop.

Revisions

  1. jlee7 created this gist Jan 19, 2018.
    100 changes: 100 additions & 0 deletions edit_distance_with_gui.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    # -*- coding: UTF-8 -*-
    # Editierdistanz wird auf Basis der Levenshtein-Distanz ermittelt.
    # Quelle: Source: http://code.activestate.com/recipes/576874-levenshtein-
    # distance/http://stackoverflow.com/questions/5306756/how-to-show-percentage-
    # in-python
    #
    # Jede Operation wird mit einem Punkt bewertet

    from Tkinter import *
    from ScrolledText import *

    #input von raw nach Textfeld ändern
    s1 = "dummy"
    s2 = "dummy"
    global distance

    def printMatrix(m):
    print ' '
    for line in m:
    spTupel = ()
    breite = len(line)
    for column in line:
    spTupel = spTupel + (column, )
    print "%3i"*breite % spTupel

    def Prozentzahl():
    global d
    global x
    len1 = len(s1)
    len2 = len(s2)

    if len1 > len2:
    x = len1
    else:
    x = len2

    distance = levenshtein(s1, s2)
    d = x - distance
    print "Der Fuzzy Match liegt bei: {0:.0f}%".format(float(d)/x * 100)


    def levenshtein(s1, s2):
    l1 = len(s1)
    l2 = len(s2)

    matrix = [range(l1 + 1)] * (l2 + 1)
    for zz in range(l2 + 1):
    matrix[zz] = range(zz,zz + l1 + 1)
    for zz in range(0,l2):
    for sz in range(0,l1):
    if s1[sz] == s2[zz]:
    matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz])
    else:
    matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz] + 1)
    #print "Die Levenshtein-Matrix beider Strings sieht wie folgt aus:"
    #printMatrix(matrix)
    return matrix[l2][l1]



    def Compare():
    print "def Compare"
    global s1
    global s2
    s1 = Textfeld1.get("0.0", END).encode('utf-8')
    s2 = Textfeld2.get("0.0", END).encode('utf-8')

    levenshtein(s1, s2)
    distance = levenshtein(s1, s2)
    print 'Die Levenshtein-Distanz von ',s1, ' und ', s2, ' ist ', distance
    Prozentzahl()
    Ergebnisfeld.config(text= "Fuzzy Match: {0:.0f}%".format(float(d)/x * 100))


    def Clear():
    Textfeld1.delete("0.0", END)
    Textfeld2.delete("0.0", END)



    #GUI
    tkFenster = Tk()
    tkFenster.wm_title = "EditDistance"

    Textfeld1 = ScrolledText(tkFenster, width = 80, height = 10)
    Textfeld1.pack(fill="both", expand=True)

    Textfeld2 = ScrolledText(tkFenster, width = 80, height = 10)
    Textfeld2.pack(fill="both", expand=True)

    Ergebnisfeld = Label(tkFenster, text = "Fuzzy match: 0%", anchor=N)
    Ergebnisfeld.pack()

    Button1 = Button(tkFenster, text = "Check", command = Compare)
    Button1.pack(fill="both", expand=True)

    Button2 = Button(tkFenster, text = "Clear", command = Clear)
    Button2.pack(fill="both", expand=True)

    tkFenster.mainloop()