首页 > 编程语言 > 详细

[Python学习笔记-007] 使用PyEnchant检查英文单词

时间:2019-12-07 20:25:52      阅读:108      评论:0      收藏:0      [点我收藏+]

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

 

[Python学习笔记-007] 使用PyEnchant检查英文单词

原文:https://www.cnblogs.com/idorax/p/12003057.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!