def main(): wordList=makeWordList() wordSet=set(wordList) wordDict=pronounce.read_dictionary() works=[] for word in wordList: t=(word,word[1:],word[0]+word[2:]) if areHomophones(wordDict,t): works.append(word) for answer in works: print '%s is a solution' %answer
def check_homophone(d): pronounce_dict = read_dictionary() for original_word in d: first_word = original_word[1:] second_word = original_word[0] + original_word[2:] if first_word not in d and second_word not in d: continue if pronounce_dict.get(original_word, 0) == pronounce_dict.get( first_word, 1) and pronounce_dict.get( original_word, 0) == pronounce_dict.get(second_word, 1): print(original_word, first_word, second_word)
def ex11_6(): '''Uses a lot of if statements to check for solutions to homophone puzzle ''' phonetic_d = read_dictionary() word_dict = build_word_dict() word_list = ['wrack'] for word in word_dict: word1 = word[1:] word2 = word[0] + word[2:] if word1 in word_dict and word2 in word_dict: if word in phonetic_d and word1 in phonetic_d and word2 in phonetic_d: if phonetic_d[word] == phonetic_d[word1] == phonetic_d[word2]: print(word, word1, word2)
def find_homophone_word(): pronounce_dict = pronounce.read_dictionary() result_list = [] for test_word in pronounce_dict: if len(test_word) == 5: if test_word[1:] in pronounce_dict and ( test_word[0] + test_word[2:]) in pronounce_dict: if pronounce_dict[test_word] == pronounce_dict[test_word[1:]] == \ pronounce_dict[test_word[0] + test_word[2:]]: result_list.append(test_word) else: continue return result_list
def find_homophones(): """Finds words that solve the CarTalk riddle in exercise 11.11. Checks all words to see if removing their first letter results in a homophone, and if removing the second letter after returning the first results in a homophone. Result is a list of all qualifying words.""" res=[] word_dict=make_word_dict() d=read_dictionary() for word in word_dict: word1=word[1:] if word1 in word_dict: if homophones(word,word1,d): word2=word[0]+word[2:] if word2 in word_dict: if homophones(word,word2,d): res.append(word) return res
def homophones(): """ Finds all words, which without either the first or second letter is a homophone of the orignal word output: none """ pron = pronounce.read_dictionary('c06d') words = mkwrddct('words.txt') for word in words: phone1 = word[1:] phone2 = word[0] + word[2:] if phone1 in pron and phone2 in pron and word in pron: if pron[word] == pron[phone1] and pron[word] == pron[phone2]: print word, phone1, phone2
def reducible(x): d=pronounce.read_dictionary().keys() for key in d: if len(key)<2: d.remove(key) if x in dic: return if x in d: i=0 while i<len(x): if x[:i]+x[i+1:] in d and x[:i]: dic.setdefault(x,[]) dic[x].append(x[:i]+x[i+1:]) reducible(x[:i]+x[i+1:]) i+=1 k=dic.items() k.sort(key=lambda x:len(x[0]),reverse=True) return k
def find_triplephone(): d = dict() pro= read_dictionary(); for line in (open('words.txt')): word = line.strip() d[word] = {} for key in d: if len(key) ==5: w1 = key[1:] w2 = key[0] + key[2:] if (w1 in pro)&(w2 in pro): if pro[w1]==pro[w2]== pro[key]: print key
def cartalk_homophone_check(d, n=100): '''Checks a dictionary d for words satisfying the cartalk puzzle: 5-letter words which are homophones with their [1:] and //also// their [0]+[2:] (so to speak). ''' a = pronounce.read_dictionary() res = [] control = 0 for i in d: if control > n: return res if len(i) < 5 or i not in a: continue m = i[1:] if m not in a: continue if a[i] == a[m]: n = list(i) del n[1] n = ''.join(n) if n not in a: continue if a[m] == a[n]: res.append(i) control += 1 return res
Returns: None ''' for word, sound in d.items(): word_cut = word[1:] word_sliced = word[0] + word[2:] if word_cut in d: if d[word] == d[word_cut]: if word_sliced in d: if d[word] == d[word_sliced]: if len(word) == 5: print word if __name__ == '__main__': d = read_dictionary() nd = hp_word(d) ''' I got answers: eerie llama llano llana scent ooohs lloyd aaron while in author's solution answers are: (which might not be correct because puzzle said the answer should be a five character word)
def check_word(word, word_dict, phonetic): """Checks to see if the word has the following property: removing the first letter yields a word with the same pronunciation, and removing the second letter yields a word with the same pronunciation. word: string word_dict: dictionary with words as keys phonetic: map from words to pronunciation codes """ word1 = word[1:] if word1 not in word_dict: return False if not homophones(word, word1, phonetic): return False word2 = word[0] + word[2:] if word2 not in word_dict: return False if not homophones(word, word2, phonetic): return False return True if __name__ == '__main__': phonetic = read_dictionary() word_dict = make_word_dict() for word in word_dict: if check_word(word, word_dict, phonetic): print(word, word[1:], word[0] + word[2:])
def main(): words = read_words() pron = pronounce.read_dictionary() for word in words: check_homophones(word, pron)
def main(): words = make_dict() phonetic = read_dictionary() homophone(words, phonetic)
for i in range(1, 14): for word in wl: if rot_13(word, i) in wl: print(word, rot_13(word)) def check_puzzler(d): wa = d[1:] wb = d[0] + d[2:] wc = d[:] if check_words(wa, wb, wc): if pron(wa) == pron(wb) and pron(wa) == pron(wc): return True def pron(s): return c06d[s] def check_words(wa, wb, wc): if wa in word_list() and wb in word_list() and wc in word_list(): return True else: return False c06d = read_dictionary() wl = word_list() for s in c06d: print(s) if check_puzzler(s): print(s, ' works alright! ') #print(read_dictionary())
removing the first letter yields a word with the same pronunciation, and removing the second letter yields a word with the same pronunciation. word: string word_dict: dictionary with words as keys phonetic: map from words to pronunciation codes """ word1 = word[1:] if word1 not in word_dict: return False if not homophones(word, word1, phonetic): return False word2 = word[0] + word[2:] if word2 not in word_dict: return False if not homophones(word, word2, phonetic): return False return True if __name__ == '__main__': phonetic = read_dictionary() word_dict = make_word_dict() for word in word_dict: if check_word(word, word_dict, phonetic): print(word, word[1:], word[0] + word[2:])
return fiveletterwords def is_homophone_solution(wordDict, word): subcase1=word[1:] if subcase1 in wordDict: if wordDict[subcase1]==wordDict[word]: subcase2=word[0]+word[2:] if subcase2 in wordDict: if wordDict[subcase2]==wordDict[word]: return word + ' ' + subcase1 + ' ' + subcase2 return False def test_dictionary(wordDict): cleanTestCases=fiveletterfilter(dictPronounce) for eachWord in cleanTestCases: result=is_homophone_solution(wordDict, eachWord) if result != False: print result dictPronounce=read_dictionary() test_dictionary(dictPronounce)
def is_magic_word(word, word_dict, pron_dict): """checks whether the word has following properties: removing the first letter yields a new word with the same pronunciation. removing the second letter yields a new word with the same pronunciation. word: string word_dict: dict, words as keys pron_dict: dict, words as keys, related pronunciation as values """ word1 = remove_first_letter(word) word2 = remove_second_letter(word) if word1 not in word_dict or word2 not in word_dict: return False if not homophone_words(word, word1, pron_dict): return False if not homophone_words(word, word2, pron_dict): return False return True if __name__ == '__main__': pro_dict = read_dictionary() word_dict = make_word_dict() for word in word_dict: if is_magic_word(word, word_dict, pro_dict): print(word, remove_first_letter(word), remove_second_letter(word))
def print_word(): homophonic = read_dictionary() w = dict_words() for keyword in dict_words(): if check(keyword,w,homophonic): print keyword,keyword[1:],keyword[0] +keyword[2:]
from pronounce import read_dictionary sipe = read_dictionary() with open("words.txt", "r") as fin: wordDict = dict() for line in fin: words = line.strip().lower() wordDict[words] = True def check_word(word1, word2, homophone): if word1 not in homophone or word2 not in homophone: return False return homophone[word1] == homophone[word2] def homophone(word, anyDict): word1 = word[1:] word2 = word[0] + word[2:] if word1 not in anyDict or word2 not in anyDict: return False if check_word(word, word1, sipe) and check_word(word1, word2, sipe): print(word, word1, word2) for word in wordDict: homophone(word, wordDict)
""" Takes two words (s1 and s2) and checks to see if they have the same pronunciation. Compares in pronouncedict. pronouncedict is dictionary of pronounciations returns bool word: string worddict: dictionary pronouncedict: dictionary """ word1 = word[1:] word2 = word[0] + word[2:] if (word1 in pronouncedict) and (word2 in pronouncedict): if pronouncedict[word1] == pronouncedict[word2]: if (word1 in worddict) and (word2 in worddict): return True return False if __name__ == '__main__': worddict = create_dict('words.txt') pronouncedict = pronounce.read_dictionary() for i in worddict: if check_word(i, worddict, pronouncedict) and (len(i) == 5): print i #Changing the if statement to opposite would prevent me from #having to nest. So, if not word in dict, return False.
from pronounce import read_dictionary fin = open('words.txt') for line in fin: word = line.strip() f = word if len(word) == 5: m = word[1:] n = word[0] + word[2:] if read_dictionary(f) == read_dictionary(m) and read_dictionary( f) == read_dictionary(t): print(f)
### Exercise 11.6: solving the homophone problem # looking for words that are homophones when removed of the first & second letter from pronounce import read_dictionary pron_dict = read_dictionary("/Users/sinansmac/Documents/MIT600/c07b.txt") def homophone(word): """Checks to see if the word has the following property: removing the first letter yields a word with the same pronunciation, and removing the second letter yields a word with the same pronunciation. word: string """ global pron_dict word1 = word[1:] word2 = word[0] + word[2:] # if word in pron_dict: val = pron_dict[word] if word1 not in pron_dict or word2 not in pron_dict: return False return val == pron_dict[word1] and val == pron_dict[word2] if __name__ == '__main__': for word in pron_dict: if homophone(word): print(word, word[1:], word[0] + word[2:]) # compared to Allen's version, mine is much simpler, as I don't check for words.txt
from pronounce import read_dictionary def homophones(r_dict): """Solves Cartalk Puzzler in Exercise 11.11 (Ch. 11 of Think Python). r_dict: dict Returns: None""" for word in r_dict: if len(word) == 5: word1 = word[1:] word2 = word[0] + word[2:] if word1 in r_dict and word2 in r_dict: pron1 = r_dict[word1] pron2 = r_dict[word2] if pron1 == pron2: print word r_dict = read_dictionary() homophones(r_dict)