Guest User

Untitled

a guest
Mar 31st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import pymysql
  2.  
  3. connection = pymysql.connect(host='localhost',
  4. user='root',
  5. password='12345',
  6. db='mycorpus',
  7. charset='utf8mb4',
  8. cursorclass=pymysql.cursors.DictCursor)
  9.  
  10. def minEditDistR(target, source):
  11. """ Minimum edit distance. Straight from the recurrence. """
  12.  
  13. i = len(target); j = len(source)
  14.  
  15. if i == 0: return j
  16. elif j == 0: return i
  17.  
  18. return(min(minEditDistR(target[:i-1],source)+1,
  19. minEditDistR(target, source[:j-1])+1,
  20. minEditDistR(target[:i-1], source[:j-1])+substCost(source[j-1], target[i-1])))
  21.  
  22. def substCost(x,y):
  23. if x == y: return 0
  24. else: return 2
  25.  
  26. entry = "kend"
  27. #fisrt two letter
  28. f2l = entry[:2]+"%"
  29.  
  30. #user_input = input("Enter a word : ")
  31.  
  32. dictArray = [{}]
  33.  
  34. try:
  35. with connection.cursor() as cursor:
  36. sql = "select WORD from corpus where WORD LIKE %s AND CHAR_LENGTH(WORD) BETWEEN %s AND %s"
  37. cursor.execute(sql,(f2l, len(entry)-1,len(entry)+1))
  38. results = cursor.fetchall()
  39. for i in results:
  40. dictArray.append({'wrd':i['WORD'],'distance':0})
  41. result_array.append((i['WORD'],0))
  42. finally:
  43. connection.close()
  44.  
  45. for i in range(1, len(dictArray)-1):
  46. dictArray[i]['distance'] = minEditDistR(dictArray[i]['wrd'], entry)
  47. if dictArray[i]['distance'] == 1:
  48. print(dictArray[i]['wrd'], dictArray[i]['distance'])
Add Comment
Please, Sign In to add comment