最近在教儿子做自然拼读,跟他玩了一个单词游戏,就是利用简单的枚举找出适合小朋友学习的两个字母的单词。人工找寻难免有疏漏之处,这里使用PyEnchant给出一个简单的脚本。

01 - foo.py

 1 #!/usr/bin/python3
 2 """
 3     A simple script to check a string is an English word
 4
 5     1. download PyEnchant from https://pypi.org/project/pyenchant/
 6     2. save pyenchant-2.0.0.tar.gz to /tmp
 7     3. tar zxf pyenchant-2.0.0.tar.gz
 8     4. export PYTHONPATH=/tmp/pyenchant-2.0.0:$PYTHONPATH
 9     5. ./foo.py <string>
10 """
11
12 import sys
13 import enchant
14
15
16 def is_english_word(word):
17     d_en = enchant.Dict("en_US")
18     return d_en.check(word)
19
20
21 def get_alphabet():
22     l_alph = []
23     for i in range(26):
24         l_alph.append(chr(ord('a') + i))
25     return l_alph
26
27
28 def main(argc, argv):
29     if argc != 2:
30         sys.stderr.write("Usage: %s <char>\n" % argv[0])
31         return 1
32
33     char_in = argv[1]
34
35     l_word1 = []
36     l_alph = get_alphabet()
37     for char in l_alph:
38         word = char_in + char
39         if is_english_word(word):
40             l_word1.append(word)
41     print(l_word1)
42
43     l_word2 = []
44     for char in l_alph:
45         word = char_in + char
46         word = word.upper()
47         if is_english_word(word):
48             if word.lower() in l_word1:
49                 continue
50             l_word2.append(word)
51     print(l_word2)
52     return 0
53
54 if __name__ == '__main__':
55     sys.exit(main(len(sys.argv), sys.argv))

很简单,核心代码就是:

def is_english_word(word):
    d_en = enchant.Dict("en_US")
    return d_en.check(word)

02 - 测试foo.py

kaiba$ ./foo.py 'a'
['ab', 'ac', 'ad', 'ah', 'am', 'an', 'as', 'at', 'av', 'aw', 'ax']
['AA', 'AF', 'AG', 'AI', 'AK', 'AL', 'AP', 'AR', 'AU', 'AZ']
kaiba$ ./foo.py 'b'
['be', 'bf', 'bi', 'bk', 'bl', 'bu', 'bx', 'by']
['BA', 'BB', 'BC', 'BM', 'BO', 'BP', 'BR', 'BS']
kaiba$ ./foo.py 'be'
['bed', 'bee', 'beg', 'bet', 'bey']
['BEN']
kaiba$ ./foo.py 't'
['ta', 'ti', 'tn', 'to', 'tr', 'ts']
['TB', 'TC', 'TD', 'TE', 'TH', 'TL', 'TM', 'TU', 'TV', 'TX', 'TY']
kaiba$ ./foo.py 'tea'
['teak', 'teal', 'team', 'tear', 'teas', 'teat']
[]
12-26 11:16
查看更多