2012-11-27 1 views
-5

텍스트 파일을 읽고 맞춤법이 잘못된 단어 사전을 만드는 함수를 만들어야합니다. 이 내가 지금까지어떻게 내 맞춤법 검사 코드를 완료 할 수 있습니까?

def spellCheck(textFileName): 

    file=open("words.txt","r") 
    wordsList = file.read() 
    print wordsList 
    file.close() 

    file=open(textFileName, "r") 
    wordsToCheck = file.read() 
    print wordsToCheck 
    file.close() 

    # The next line creates the dictionary 
    # This dictionary will have the word that has been spelt wrong as the key and the number of times it has been spelt wrong as the value 
    spellingErrors = dict= {'words', wordsToCheck} 

가진 것을 내가 필요로 :

# Loop through the wordsToCheck list 
# Change the current word into lower case 
# If the current word does not exist in the wordsList then 
     # Check if the word already exists in the spellingErrors dictionary 
       # If it does not exist than add it to the dictionary with the initial value of 1. 
       # If it does exist in the dictionary then increase the value by 1 

# Return the dictionary 
+1

그래서,에 대한 실제 알고리즘, 당신은 지금까지 무엇을 시도 했습니까? – learner

+0

너무 현지화되었습니다. 다시. http://stackoverflow.com/questions/13573552/can-anyone-help-me-with-my-spell-check-code – Yandros

답변

1

이 매우 간단하고, 매우 느려질 수 :

wordsList = wordsList.lower().split() 
for word in wordsToCheck.lower().split(): 
    if not word in wordsList: 
     if word not in spellingErrors: 
      spellingErrors[word]=0 
     spellingErrors[word] += 1 
+0

이걸 실행할 때이 오류가 발생합니다 추적 (가장 최근에 마지막으로 호출) : 파일 " "(있는 test1.txt") 파일 "J 맞춤법 검사에서, 라인 1" : \ 파이썬 spellingErrors [워드 = 0 형식 오류가 맞춤법에 맞춤법 1 평 "라인 (48) \ 셋트 '개체가 항목 할당을 지원하지 않습니다. – user1839493

1
# load the dictonary 
dict_text = open('words.txt','r').read().split() 
# convert dictionary to lowercase set() for easy access 
dictionary = set([i.lower() for i in dict_text]) 

# load a text 
words_to_check = open(text_file_name, 'r').read().split() 
# and check it 
for word in words_to_check : 
    if word.lower() not in dictionary : 
     print 'misspelled:', word