2020-04
21

找2个词汇表

By xrspook @ 17:59:37 归类于: 扮IT

最根本的在于做好词语的切片和组合,知道要找什么词,丢到字典里找那是秒杀的事。我不知道他们为什么这么折腾,直接在有音标的词汇表里找不就得了,但偏偏要两个词汇表都要求同时存在,于是循环判断神马不得不多一层。幸好字典里用in那是非常简单快捷的事。

Exercise 6: Here’s another Puzzler from Car Talk (http://www.cartalk.com/content/puzzlers): This was sent in by a fellow named Dan O’Leary. He came upon a common one-syllable, five-letter word recently that has the following unique property. When you remove the first letter, the remaining letters form a homophone of the original word, that is a word that sounds exactly the same. Replace the first letter, that is, put it back and remove the second letter and the result is yet another homophone of the original word. And the question is, what’s the word? Now I’m going to give you an example that doesn’t work. Let’s look at the five-letter word, ‘wrack.’ W-R-A-C-K, you know like to ‘wrack with pain.’ If I remove the first letter, I am left with a four-letter word, ’R-A-C-K.’ As in, ‘Holy cow, did you see the rack on that buck! It must have been a nine-pointer!’ It’s a perfect homophone. If you put the ‘w’ back, and remove the ‘r,’ instead, you’re left with the word, ‘wack,’ which is a real word, it’s just not a homophone of the other two words. But there is, however, at least one word that Dan and we know of, which will yield two homophones if you remove either of the first two letters to make two, new four-letter words. The question is, what’s the word? You can use the dictionary from Exercise 1 to check whether a string is in the word list. To check whether two words are homophones, you can use the CMU Pronouncing Dictionary. You can download it from http://www.speech.cs.cmu.edu/cgi-bin/cmudict or from http://thinkpython2.com/code/c06d and you can also download http://thinkpython2.com/code/pronounce.py, which provides a function named read_dictionary that reads the pronouncing dictionary and returns a Python dictionary that maps from each word to a string that describes its primary pronunciation. Write a program that lists all the words that solve the Puzzler. Solution: http://thinkpython2.com/code/homophone.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from pronounce import read_dictionary
from time import time
def set_dict(fin):
    d ={}
    for line in fin:
        word = line.strip()
        d[word] = 0
    return d
def same_pronounce(a, b, c, mydict, pdict):
    if b in mydict and c in mydict:
        if b in pdict and c in pdict:
            if pdict[a] == pdict[b] and pdict[a] == pdict[c]:
                return True
    return False
start = time()
count = 0
pdict = read_dictionary()
fin = open('words.txt')
mydict = set_dict(fin)
for word in mydict:
    if word in pdict:
        word1 = word[1:]
        word2 = word[:1] + word[2:]
        if same_pronounce(word, word1, word2, mydict, pdict):
            print(word, word1, word2)
            count += 1
print(count)
end = time()
print(end - start)
# llama lama lama
# llamas lamas lamas
# scent cent sent
# 3
# 0.28800010681152344
© 2004 - 2024 我的天 | Theme by xrspook | Power by WordPress