words/scripts/gen_wordlist.py

67 lines
1.9 KiB
Python
Raw Normal View History

2022-01-27 18:36:45 +00:00
#!/usr/bin/env python3
import sys
import json
2022-01-30 22:05:51 +00:00
class WordListItem:
def __init__(self, line):
word = line.split('\t')
self.word = word[1] if word[1].isalpha() else word[3]
self.pos = word[2]
self.freq = int(word[4])
2022-01-30 22:05:51 +00:00
def __repr__(self):
return f"<WordListItem {self.word=} {self.pos=} {self.freq=}>"
2022-01-30 22:05:51 +00:00
2022-01-27 18:36:45 +00:00
def get_args():
""" Get command line arguments """
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('wordlist')
parser.add_argument('word_length', type=int)
parser.add_argument('valid_answer_freq_min', type=int)
parser.add_argument('valid_input_freq_min', type=int)
parser.add_argument('allowedtypelist')
2022-01-27 18:36:45 +00:00
return parser.parse_args()
def main(args):
""" Entry point for script """
with open(args.allowedtypelist) as fp:
allowed_types = fp.read().split('\n')
types = set()
2022-01-27 18:36:45 +00:00
with open(args.wordlist) as fp:
src_words = [ WordListItem(line) for line in fp.read().strip().lower().split('\n') ]
src_words = [ word for word in src_words if word.word.isalpha() ]
src_words = [ word for word in src_words if len(word.word) == args.word_length ]
src_words = [ word for word in src_words if word.pos in allowed_types ]
2022-01-27 18:36:45 +00:00
[ types.add(word.pos) for word in src_words ]
words = {}
words['valid_answers'] = [ w.word for w in src_words if w.freq >= args.valid_answer_freq_min ]
words['valid_inputs'] = [ w.word for w in src_words if w.freq >= args.valid_input_freq_min ]
2022-01-27 19:14:29 +00:00
2022-01-27 20:16:33 +00:00
# remove duplicates
2022-01-27 18:36:45 +00:00
print(f"wordlist = {json.dumps(words)}")
2022-01-30 22:05:51 +00:00
print(f"{args=}", file=sys.stderr)
print(f"{len(words['valid_answers'])=}", file=sys.stderr)
print(f"{len(words['valid_inputs'])=}", file=sys.stderr)
print(f"{types=}", file=sys.stderr)
2022-01-27 18:36:45 +00:00
return 0
if __name__ == '__main__':
try:
sys.exit(main(get_args()))
except KeyboardInterrupt:
sys.exit(0)