使用二进制搜索的拼写检查器中的python错误

时间:2016-10-25 00:59:49

标签: python spell-checking

我在python中使用二进制搜索为拼写检查器编写了以下代码,它正常工作,但输出是反向的...例如我写了“hwllo world”,输出是世界问候,而它是“hello world”

我的代码如下:

    import difflib
    #L=[]
    ch=[]
    def binarySearch(alist, item):
            first = 0
            last = len(alist) - 1
            while first <= last:
                midpoint = (first + last) // 2
                if alist[midpoint] == item:
                   return True
                else:
                  if item < alist[midpoint]:
                     last = midpoint - 1
                else:
                     first = midpoint + 1
           return False

    f = open('wordlist.txt', 'r').read().splitlines()
    v=str(input("enter your sentence : ")).split()


    for i in range(len(v)):
       if binarySearch(f, v[i]) == True:
          ch.append(v[i])


       elif binarySearch(f, v[i]) == False:
          sugg = []
          for word in f:
             if difflib.SequenceMatcher(None, v[i], word).ratio() >= 0.8:
               sugg.append(word)
          print(sugg)

    for j in range (len(sugg)):
          print( "if you mean ",(sugg[j]),"press",(j))

    x=int(input())
    ch.append(sugg[x])

    print (' '.join(ch))

1 个答案:

答案 0 :(得分:0)

这是有问题的,因为您在最后添加了所有建议,而不是找到拼写错误的单词。

而不是:

:compile 'com.google.android.gms:play-services:9.8.0'

尝试:

for i in range(len(v)):
   if binarySearch(f, v[i]) == True:
      ch.append(v[i])
   elif binarySearch(f, v[i]) == False:
      sugg = []
      for word in f:
         if difflib.SequenceMatcher(None, v[i], word).ratio() >= 0.8:
           sugg.append(word)
      print(sugg)

for j in range (len(sugg)):
      print( "if you mean ",(sugg[j]),"press",(j))

x=int(input())
ch.append(sugg[x])