KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
first i want to say that i am a newbie in python. I trying to calculate the Levenshtein Distance for many lists of word. Until now i succeed writing the code for a pair of word, but i'm having some problems doing it for lists. I just habe two lists with words one below the other like this: carlos stiv peter I want to use the Levenshtein distance for a similarity approach. Could somebady tell me how i can load the lists and then use a function to calculate de distance? I'll appreciated! Here is my code just for two strings: #!/usr/bin/env python # -*- coding=utf-8 -*- def lev_dist(source, target): if source == target: return 0 #words = open(test_file.txt,'r').read().split(); # Prepare matrix slen, tlen = len(source), len(target) dist = [[0 for i in range(tlen+1)] for x in range(slen+1)] for i in xrange(slen+1): dist[i][0] = i for j in xrange(tlen+1): dist[0][j] = j # Counting distance for i in xrange(slen): for j in xrange(tlen): cost = 0 if source[i] == target[j] else 1 dist[i+1][j+1] = min( dist[i][j+1] + 1, # deletion dist[i+1][j] + 1, # insertion dist[i][j] + cost # substitution ) return dist[-1][-1] if __name__ == '__main__': import sys if len(sys.argv) != 3: print 'Usage: You have to enter a source_word and a target_word' sys.exit(-1) source, target = sys.argv[1], sys.argv[2] print lev_dist(source, target)
Tags (comma-separated)
Save Edits
Cancel