def closest_word_for_letter(target, first_letter, similar_phone_substitution_cost): first_letter = first_letter.lower() words = cmudict.words() letter_start = binary_search(words, first_letter, lambda x, y: 0 if x == y else 1 if x > y else -1) letter_end = binary_search(words, chr(ord(first_letter) + 1), lambda x, y: 0 if x == y else 1 if x > y else -1) return closest_word(target, words[letter_start:letter_end], similar_phone_substitution_cost)
def test_closest_word_krypton(self): words = cmudict.words() c_start = binary_search(words, 'c', lambda x, y: 0 if x == y else 1 if x > y else -1) c_end = binary_search(words, 'd', lambda x, y: 0 if x == y else 1 if x > y else -1) assert self.calculator.closest_words("KRYPTON", words[c_start:c_end]) == ['crippen', 'crypto']