2018-02-28 21:11:39 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
import random
|
|
|
|
import inflect
|
|
|
|
|
|
|
|
p = inflect.engine()
|
|
|
|
dictionary = "/usr/share/dict/american-english-small"
|
2018-07-16 20:59:35 +00:00
|
|
|
BAD_WORDS_FILE = "badwords.txt"
|
|
|
|
|
2018-10-05 20:02:38 +00:00
|
|
|
|
2018-07-16 20:59:35 +00:00
|
|
|
def get_wordlist():
|
|
|
|
# I feel weird calling this "get_wordlist" when it's a generator without calling out that I do in fact realise it's weird - ~deltawitch
|
|
|
|
with open(BAD_WORDS_FILE, "r") as fp:
|
|
|
|
bad_words = set(fp)
|
|
|
|
|
|
|
|
for word in open(dictionary).readlines():
|
|
|
|
if "'" not in word and word not in bad_words:
|
|
|
|
yield word.rstrip()
|
2018-02-28 21:11:39 +00:00
|
|
|
|
2018-10-05 20:02:38 +00:00
|
|
|
|
2018-02-28 21:11:39 +00:00
|
|
|
def get_puzzle():
|
2018-07-16 20:59:35 +00:00
|
|
|
dict_words = list(get_wordlist())
|
2018-02-28 21:11:39 +00:00
|
|
|
words = random.sample(dict_words, 3)
|
2018-10-05 20:02:38 +00:00
|
|
|
key = random.randrange(0, 3) # get values 1-3
|
|
|
|
puzzle = (
|
|
|
|
"When alphebetized, what is the "
|
|
|
|
+ p.ordinal(p.number_to_words(key + 1))
|
|
|
|
+ " in "
|
|
|
|
+ ", ".join(words)
|
|
|
|
)
|
2018-02-28 21:11:39 +00:00
|
|
|
words.sort()
|
|
|
|
answer = words[key]
|
|
|
|
return [answer, puzzle]
|
2018-04-24 15:21:29 +00:00
|
|
|
|
2018-10-05 20:02:38 +00:00
|
|
|
|
|
|
|
def get_anagram(maxlen=6):
|
|
|
|
dict_words = [
|
|
|
|
word for word in get_wordlist() if len(word) > 2 and len(word) <= maxlen + 1
|
|
|
|
]
|
2018-04-24 15:21:29 +00:00
|
|
|
word = random.choice(dict_words)
|
|
|
|
while True:
|
2018-10-05 20:02:38 +00:00
|
|
|
anagram = "".join(random.sample(word, len(word)))
|
2018-04-24 15:21:29 +00:00
|
|
|
if anagram != word:
|
|
|
|
break
|
|
|
|
puzzle = "Unscramble the following word: '" + anagram + "'"
|
2018-07-16 21:01:36 +00:00
|
|
|
|
|
|
|
# Anagrams can have multiple answers, so we provide a check function that accepts all possibilities
|
|
|
|
def answer_checker(guess):
|
|
|
|
# Check for exact match
|
|
|
|
if guess == word:
|
|
|
|
return True
|
|
|
|
# Bail out early if they didn't even use all the same letters
|
|
|
|
if sorted(guess) != sorted(word):
|
|
|
|
return False
|
|
|
|
# Ok, gotta actually check if it's a word now
|
|
|
|
return any(guess == item for item in get_wordlist())
|
2018-10-05 20:02:38 +00:00
|
|
|
|
2018-07-16 21:01:36 +00:00
|
|
|
return [answer_checker, puzzle]
|