-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpellingQuiz.py
137 lines (120 loc) · 4.82 KB
/
SpellingQuiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# coding: utf-8
import readline
import random
import json
from Speaker import Speaker
class SpellingQuiz(object):
"""Encapsulates a specific spelling quiz"""
def __init__(self):
with open("config.json") as config_file:
config = json.load(config_file)
self.speechEngine = Speaker()
self.name = config['name']
self.words = config['words']
self.explanations = config['explanations']
def mainMenu(self):
"""Get a selection from the main menu"""
categories="\n\t\t".join(self.words.keys())
print("""
Hey there %s! Welcome to spelling quiz.
I can ask you about:
%s.
Would you like to:
a)Practise words from a category you pick
b)Practise words from a category I pick
c)Practise words which I pick from the whole list
d)go for champion speller
""" % (self.name, categories))
ans=raw_input("Type a, b, c, or d, or press enter to quit:")
return ans
def pickCategory(self, categories):
"""Present a menu of choices for the user to pick a category"""
idx=0
choices={}
print "Type a number to pick a category"
for cat in categories:
idx = idx + 1
choices[idx] = cat
print "\t%d for %s" % (idx, cat)
done = False
while not done:
ans = raw_input("Type 1 to %d or enter to go back:" % idx)
if ans != "":
try:
selection = choices[int(ans)]
return selection
except (ValueError, KeyError):
pass
else:
return ""
def testList(self, wordList):
"""Test a specific list of words"""
totalCount=len(wordList)
correct=0
wrong=[]
msg = "OK %s, type the words after you hear them. Just press 'enter' to hear the word again." % self.name
print(msg)
self.speechEngine.say(msg)
idx = 0
prompts = [
"Please spell '%s'.",
"Can you spell '%s'?",
"I bet you can't spell '%s'!"
]
for word in wordList:
idx = idx + 1
gotIt=False
while not gotIt:
prompt = random.choice(prompts)
self.speechEngine.say(prompt % word)
if word in self.explanations:
self.speechEngine.say("For example, '%s'." % self.explanations[word])
ans = raw_input("%d of %d: Type the word you just heard, or press 'enter' to hear it again. =>" % (idx, totalCount))
if(ans!=""):
gotIt=True
if(ans==word):
correct = correct + 1
else:
wrong = wrong + [word]
return (totalCount, correct, wrong)
def assess(self, count, correct, wrong, champion=False):
if count == correct:
print("Nice one! You got %d out of %d" % (correct, count))
if champion:
print("""
-------------------------------------------
>>>YOU ARE OFFICIALLY A CHAMPION SPELLER<<<
-------------------------------------------
""")
self.speechEngine.say("Well done! You are a champion speller!")
else:
print("You got %d out of %d, and these are the words you got wrong: %s" % (correct, count, wrong))
def runTest(self):
allWords = reduce(lambda xs, x: xs + x, self.words.values())
done=False
while not done:
ans=self.mainMenu()
if(ans==""):
print("OK. See you again soon!")
done=True
else:
print("\n\n\n")
category=''
if ans=='a':
category=self.pickCategory(self.words.keys())
print("OK. You picked '%s'." % category)
if ans=='b':
category=random.choice(self.words.keys())
print("Thanks for letting me pick! I chose '%s'." % category)
if category:
selection = self.words[category]
random.shuffle(selection)
self.assess(*self.testList(selection))
if ans=='c':
selection = random.sample(allWords,len(allWords)/3)
self.assess(*self.testList(selection))
if ans=='d':
selection = allWords
random.shuffle(selection)
self.assess(*self.testList(selection), champion=True)
# vim: set sw=4 ts=4 et: