Make wottasquare.py's dictionary file configurable.
Chris Pressey
9 years ago
54 | 54 |
|
55 | 55 |
Basic usage:
|
56 | 56 |
|
57 | |
wottasquare.py filename.wottasquare
|
|
57 |
wottasquare.py [--dictionary filename] filename.wottasquare
|
58 | 58 |
|
59 | 59 |
Reads the Wottasquare program from the given file and compiles it to an
|
60 | 60 |
equivalent Beatnik program on standard output.
|
|
68 | 68 |
a Scrabble score of 5, such as `slug`, for use in Beatnik.
|
69 | 69 |
|
70 | 70 |
By default, `wottasquare.py` looks in `/usr/share/dict/words` for
|
71 | |
words to use when translating Wottasquare to Beatnik. TODO: make this
|
72 | |
dictionary file configurable.
|
|
71 |
words to use when translating Wottasquare to Beatnik. A different dictionary
|
|
72 |
file can be specified with the `--dictionary` command-line argument. Note
|
|
73 |
that the dictionary file is parsed like a Beatnik source file would be;
|
|
74 |
punctuation is ignored (and treated as word seperator), etc.
|
73 | 75 |
|
74 | 76 |
There are two flags which trigger special behaviour:
|
75 | 77 |
|
46 | 46 |
}
|
47 | 47 |
|
48 | 48 |
|
49 | |
def load_dictionary():
|
50 | |
with open('/usr/share/dict/words') as f:
|
|
49 |
def load_dictionary(filename):
|
|
50 |
with open(filename) as f:
|
51 | 51 |
for line in f:
|
52 | 52 |
for word in re.findall(r'[A-Za-z]+', line):
|
53 | 53 |
if len(word) <= 2 or word[0].isupper():
|
|
62 | 62 |
def main(args):
|
63 | 63 |
find_text = None
|
64 | 64 |
find_amount = 20
|
|
65 |
dictionary_filename = '/usr/share/dict/words'
|
65 | 66 |
while args and args[0].startswith('--'):
|
66 | 67 |
switch = args.pop(0)
|
67 | |
if switch == '--find':
|
|
68 |
if switch == '--dictionary':
|
|
69 |
dictionary_filename = args.pop(0)
|
|
70 |
elif switch == '--find':
|
68 | 71 |
find_text = args.pop(0)
|
69 | 72 |
elif switch == '--find-all':
|
70 | 73 |
find_text = args.pop(0)
|
|
72 | 75 |
else:
|
73 | 76 |
raise KeyError("Unknown command-line option '%s'" % switch)
|
74 | 77 |
|
75 | |
load_dictionary()
|
|
78 |
load_dictionary(dictionary_filename)
|
76 | 79 |
|
77 | 80 |
if find_text:
|
78 | 81 |
value = None
|