2016-10-25 7 views
0

파이썬에서 이진 검색을 사용하여 다음과 같은 코드를 작성했으며 올바르게 작동하지만 출력이 반전됩니다. 예를 들어 "hwllo world"라고 쓰고 출력은 world hello 이 수의 반면 다음에 "Hello World"바이너리 검색을 사용하는 맞춤법 검사기의 python 버그

내 코드대로 : 당신은 당신이 틀린 단어를 발견하면보다는 마지막에 모든 제안을 추가하고 있기 때문에이 문제가

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)) 

답변

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]) 

시도 :

for i in range(len(v)): 
    if binarySearch(f, v[i]): 
     ch.append(v[i]) 
    elif not binarySearch(f, v[i]): 
     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])