def correct_words(classified_words, actual_words, dictionary_word_list, names_word_list, mode='single'): corrected_words = classified_words[:] for word_index in xrange(len(corrected_words)): current_word = corrected_words[word_index] lowercase_word = corrected_words[word_index].lower() # Don't carry out correction for valid single letters valid_single_letters = 'ai' if not lowercase_word in valid_single_letters: # Preserve capitalisation of first letter of each word capitalised = current_word[0].isupper() if not dictionary_word_list.word_match(lowercase_word) and not names_word_list.word_match(lowercase_word): name_word = names_word_list.correct_word(lowercase_word, isCapitalised=capitalised, mode=mode) dictionary_word = dictionary_word_list.correct_word(lowercase_word, isCapitalised=capitalised, mode=mode) # Skip loop if no valid corrections were found if name_word is None and dictionary_word is None: continue # If no name words were found, use the dictionary word elif name_word is None and dictionary_word is not None: current_word = dictionary_word # If no dictionary words were found, use the name word elif name_word is not None and dictionary_word is None: current_word = name_word # See if word is closer to a name or a dictionary word and correct it to the closer one elif WordList.word_difference(current_word, name_word) < dictionary_word_list.word_difference(current_word, dictionary_word): current_word = name_word else: current_word = dictionary_word corrected_words[word_index] = current_word return corrected_words