Guest User

Untitled

a guest
Apr 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. def distance(a, b):
  2.     a=" " + a
  3.     b=" " + b
  4.     dist=[]
  5.     for i in range(0, len(a)):
  6.         dist.append([0]*len(b))
  7.    
  8.     for i in range( len(a)):
  9.         dist[i][0]=i
  10.     for i in range( len(b)):
  11.         dist[0][i]=i
  12.    
  13.     for j in range(1, len(b)):
  14.         for i in range(1, len(a)):
  15.             if a[i] == b[j]:
  16.                 dist[i][j]=dist[i-1][ j-1]
  17.             else:
  18.                 dist[i][j] = min(
  19.                         dist[i-1][j]+1, #deletion
  20.                         dist[i][j-1]+1, #insertion
  21.                         dist[i-1][ j-1]+1.5 ) #substitute
  22.     return dist[-1][-1]
  23.    
  24. possible=[]
  25. words=open('words', 'r')
  26.  
  27. inword="deffinetly"
  28.  
  29. for i in words:
  30.     i=i[:-1]
  31.     d=distance(i, inword)
  32.     if d < len(inword)//2:
  33.         possible.append([d,str(i)])
  34. possible.sort()
  35. print( possible)
Add Comment
Please, Sign In to add comment