22"""Explain acronyms"""
33
44import argparse
5- import io
6- import sys
7- import os
85import random
96import re
107from collections import defaultdict
@@ -16,46 +13,46 @@ def get_args():
1613 """get arguments"""
1714
1815 parser = argparse .ArgumentParser (
19- description = ' Explain acronyms' ,
16+ description = " Explain acronyms" ,
2017 formatter_class = argparse .ArgumentDefaultsHelpFormatter )
2118
22- parser .add_argument (' acronym' , help = ' Acronym' , type = str , metavar = ' STR' )
19+ parser .add_argument (" acronym" , help = " Acronym" , type = str , metavar = " STR" )
2320
24- parser .add_argument ('-n' ,
25- ' --num' ,
26- help = ' Maximum number of definitions' ,
21+ parser .add_argument ("-n" ,
22+ " --num" ,
23+ help = " Maximum number of definitions" ,
2724 type = int ,
28- metavar = ' NUM' ,
25+ metavar = " NUM" ,
2926 default = 5 )
3027
31- parser .add_argument ('-w' ,
32- ' --wordlist' ,
33- help = ' Dictionary/word file' ,
34- type = argparse .FileType ('r' ),
35- metavar = ' STR' ,
36- default = ' /usr/share/dict/words' )
28+ parser .add_argument ("-w" ,
29+ " --wordlist" ,
30+ help = " Dictionary/word file" ,
31+ type = argparse .FileType ("r" ),
32+ metavar = " STR" ,
33+ default = " /usr/share/dict/words" )
3734
38- parser .add_argument ('-x' ,
39- ' --exclude' ,
40- help = ' List of words to exclude' ,
35+ parser .add_argument ("-x" ,
36+ " --exclude" ,
37+ help = " List of words to exclude" ,
4138 type = str ,
42- metavar = ' STR' ,
43- nargs = '+' ,
44- default = ' a an the' .split ())
39+ metavar = " STR" ,
40+ nargs = "+" ,
41+ default = " a an the" .split ())
4542
46- parser .add_argument ('-s' ,
47- ' --seed' ,
48- help = ' Random seed' ,
43+ parser .add_argument ("-s" ,
44+ " --seed" ,
45+ help = " Random seed" ,
4946 type = int ,
50- metavar = ' INT' ,
47+ metavar = " INT" ,
5148 default = None )
5249
5350 args = parser .parse_args ()
5451
5552 if args .num < 1 :
5653 parser .error ('--num "{}" must be > 0' .format (args .num ))
5754
58- if not re .search (r' ^[A-Z]{2,}$' , args .acronym .upper ()):
55+ if not re .search (r" ^[A-Z]{2,}$" , args .acronym .upper ()):
5956 msg = 'Acronym "{}" must be >1 in length, only use letters' .format (
6057 args .acronym )
6158 parser .error (msg )
@@ -64,13 +61,14 @@ def get_args():
6461
6562
6663# --------------------------------------------------
67- def group_words (file , stop_words = set ()):
64+ def group_words (file , stop_words = ()):
6865 """Groups words in file by first letter"""
6966
7067 good = partial (re .search , r'^[a-z]{2,}$' )
7168 seen = set ()
7269 words_by_letter = defaultdict (list )
7370 clean = lambda word : re .sub ('[^a-z]' , '' , word )
71+
7472 for word in filter (good , map (clean , file .read ().lower ().split ())):
7573 if word not in seen and word not in stop_words :
7674 seen .add (word )
@@ -79,20 +77,6 @@ def group_words(file, stop_words=set()):
7977 return words_by_letter
8078
8179
82- # --------------------------------------------------
83- def test_group_words ():
84- """Test group_words()"""
85-
86- words = io .StringIO ('apple, "BANANA," The Coconut! Berry - APPLE; A cabbage.' )
87- stop = 'a an the' .split ()
88- words_by_letter = group_words (words , stop )
89-
90- assert words_by_letter ['a' ] == ['apple' ]
91- assert words_by_letter ['b' ] == ['banana' , 'berry' ]
92- assert words_by_letter ['c' ] == ['coconut' , 'cabbage' ]
93- assert 't' not in words_by_letter
94-
95-
9680# --------------------------------------------------
9781def make_definitions (acronym , words_by_letter , limit = 1 ):
9882 """Find definitions an acronym given groupings of words by letters"""
@@ -102,33 +86,12 @@ def make_definitions(acronym, words_by_letter, limit=1):
10286 definition = []
10387 for letter in acronym .lower ():
10488 opts = words_by_letter .get (letter .lower (), [])
105- definition .append (random .choice (opts ).title () if opts else '?' )
106- definitions .append (' ' .join (definition ))
89+ definition .append (random .choice (opts ).title () if opts else "?" )
90+ definitions .append (" " .join (definition ))
10791
10892 return definitions
10993
11094
111- # --------------------------------------------------
112- def test_make_definitions ():
113- """Test make_definitions()"""
114-
115- words = {
116- 'a' : ['apple' ],
117- 'b' : ['banana' , 'berry' ],
118- 'c' : ['coconut' , 'cabbage' ]
119- }
120-
121- random .seed (1 )
122- assert make_definitions ('ABC' , words ) == ['Apple Banana Cabbage' ]
123- random .seed (2 )
124- assert make_definitions ('ABC' , words ) == ['Apple Banana Coconut' ]
125- random .seed (3 )
126- assert make_definitions ('AAA' , words ) == ['Apple Apple Apple' ]
127- random .seed (4 )
128- assert make_definitions ('YYZ' , words ) == ['? ? ?' ]
129- random .seed (None )
130-
131-
13295# --------------------------------------------------
13396def main ():
13497 """Make a jazz noise here"""
@@ -142,13 +105,13 @@ def main():
142105 definitions = make_definitions (acronym , words_by_letter , args .num )
143106
144107 if definitions :
145- print (acronym .upper () + ' =' )
108+ print (acronym .upper () + " =" )
146109 for definition in definitions :
147- print (' - ' + definition )
110+ print (" - " + definition )
148111 else :
149- print (' Sorry I could not find any good definitions' )
112+ print (" Sorry I could not find any good definitions" )
150113
151114
152115# --------------------------------------------------
153- if __name__ == ' __main__' :
116+ if __name__ == " __main__" :
154117 main ()
0 commit comments