git @ Cat's Eye Technologies Beatnik / 3924c8e
Make wottasquare.py's dictionary file configurable. Chris Pressey 9 years ago
2 changed file(s) with 12 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
5454
5555 Basic usage:
5656
57 wottasquare.py filename.wottasquare
57 wottasquare.py [--dictionary filename] filename.wottasquare
5858
5959 Reads the Wottasquare program from the given file and compiles it to an
6060 equivalent Beatnik program on standard output.
6868 a Scrabble score of 5, such as `slug`, for use in Beatnik.
6969
7070 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.
7375
7476 There are two flags which trigger special behaviour:
7577
4646 }
4747
4848
49 def load_dictionary():
50 with open('/usr/share/dict/words') as f:
49 def load_dictionary(filename):
50 with open(filename) as f:
5151 for line in f:
5252 for word in re.findall(r'[A-Za-z]+', line):
5353 if len(word) <= 2 or word[0].isupper():
6262 def main(args):
6363 find_text = None
6464 find_amount = 20
65 dictionary_filename = '/usr/share/dict/words'
6566 while args and args[0].startswith('--'):
6667 switch = args.pop(0)
67 if switch == '--find':
68 if switch == '--dictionary':
69 dictionary_filename = args.pop(0)
70 elif switch == '--find':
6871 find_text = args.pop(0)
6972 elif switch == '--find-all':
7073 find_text = args.pop(0)
7275 else:
7376 raise KeyError("Unknown command-line option '%s'" % switch)
7477
75 load_dictionary()
78 load_dictionary(dictionary_filename)
7679
7780 if find_text:
7881 value = None