def edit_distance_simple_test(diag_score, off_diag_score, dash_score): ''' Insert: Repalce the string x + y by the string x + a + y Delete: Replace the string x + a + y by the string x + y Substitute: Replace the string x + a + y by the string x + b + y To find the optimal argument of scoring_matrix ''' scoring_matrix = scoring_matrix = build_scoring_matrix({'x', 'a', 'y', 'b'}, diag_score, off_diag_score, dash_score) insert_score = get_edit_distance('xay', 'xaay', scoring_matrix) delete_score = get_edit_distance('xaay', 'xby', scoring_matrix) substitute_score = get_edit_distance('xaaay', 'xby', scoring_matrix) return insert_score, delete_score, substitute_score
def check_spelling(checked_word, dist, words): ''' words is the list of correct spell word, dist is the edit distance Return the set of words that are with in dist of the string checked_word ''' alphabet = set('qwertyuiopasdfghjklzxcvbnm') scoring_matrix = scoring_matrix = build_scoring_matrix(alphabet=alphabet, diag_score=2, off_diag_score=1, dash_score=0) answer = set([]) for word in words: edit_distance = get_edit_distance(checked_word, word, scoring_matrix) if edit_distance <= dist: answer.add(word) return answer