# quiztalker.py by Bruce_Landon@douglas.bc.ca 10-22-06
# works with display only once then close and restart IDLE
# this should work most easily with glossary style questions
# question lines need to be preporocesed to wrap before being in the list to show correctly 
from ccm.env.objects import *
import random
import pyTTS

tts = pyTTS.Create()
#set the speech rate
tts.Rate = 4
#set the speech volume percentage (0-100%)
tts.Volume = 60
#get a list of all the available voice actors
#print tts.GetVoiceNames()
#explicitly set a voice from MS standard voices
#tts.SetVoiceByName('MSSam')
tts.SetVoiceByName('MSMike')
#tts.SetVoiceByName('MSMary')
#explicitly set a voice from additional voices added
#tts.SetVoiceByName('CMUKal')
#tts.SetVoiceByName('VW Kate')
#tts.SetVoiceByName('VW Paul')
#speak the text
#tts.Speak('This is the sound of my voice.')

thekey='4255442232'
instructions="""
You will now be shown a series
of questions.  Answer each one
by pressing the appropriate letter.

Press any key to start.
"""

questions=[
"""
1. The sequence of brain regions from
 oldest to newest is:
A) limbic system; brainstem; cerebral
 cortex.
B) brainstem; cerebral cortex; limbic
 system.
C) limbic system; cerebral cortex;
 brainstem.
D) brainstem; limbic system; cerebral
 cortex.
E) cerebral cortex; brainstem; limbic
 system.
""",
"""
2. Simple thinking strategies that
 allow us to solve problems and make
 judgments efficiently are called:
A) semantics.
B) heuristics.
C) prototypes.
D) algorithms.
E) fixations.
""",
"""
3. College students routinely
 underestimate how much time it will
 take them to complete assigned course
 projects. This best illustrates the
 impact of:
A) framing.
B) functional fixedness.
C) the availability heuristic.
D) the representativeness heuristic.
E) overconfidence.
""",
"""
4. Attribution theory was designed to
 account for:
A) the process of revealing intimate
 aspects of ourselves to others.
B) the impact of both heredity and
 environment on social behavior.
C) social facilitation and social
 loafing.
D) the loss of self-awareness that
 occurs in group situations.
E) how people explain others` behavior.
""",
"""
5. Whorf`s linguistic determination
 hypothesis states that:
A) language is primarily a learned
 ability.
B) language is partially an innate
 ability.
C) the size of a person's vocabulary
 reflects his or her intelligence.
D) our language shapes our thinking.
""",
"""
6. During a televised political
 debate, the Republican and Democratic
 candidates each argued that the
 results of a recent public opinion
 poll supported their party`s platform
 regarding sexual harassment. Because
 both candidates saw the information
 as supporting their belief, it is
 clear that both were victims of:
A) functional fixedness.
B) mental set.
C) belief bias.
D) confirmation bias.
""",
"""
7. Failing to solve a problem that
 requires using an object in an
 unusual way illustrates the
 phenomenon of
A) mental set.
B) functional fixedness.
C) framing.
D) belief perseverance.
E) overconfidence.
""",
"""
8. The mind processes information
 like a computer is a statement most
 likely made by a
A) structuralist
B) cognitive psychologist
C) phenomenologist
D) behaviorist
""",
"""
9. Cognitive psychologists are most
 concerned in learning with the role
 of prior
A) activity
B) reinforcement
C) knowledge
D) socialization
""",
"""
10. Compared to behavioral psychology,
 cognitive psychology places more
 emphasis on
A) rote learning
B) mental processes
C) observational learning
D) the environment
"""
]


class MultipleChoiceEnvironment(ObjectEnvironment):
    def start(self):
        self.text=Object(isa='text',x=0.5,y=0.5)
        self.text.value=instructions
        tts.Speak(instructions)     # add voice
        self.currentQuestion=None
        self.answers=[]
        yield 'key'                 # wait for any key to be pressed
        self.text.value=None
        yield 0.5
        x=str(questions[0])
        # split into stem and choices
        stem=''
        choiceA=''
        choiceB=''
        choiceC=''
        choiceD=''
        choiceE=''
        stem=x[0:(x.find('\nA)'))]
        choiceA=x[(x.find('\nA)')):(x.find('\nB)'))]
        choiceB=x[(x.find('\nB)')):(x.find('\nC)'))]
        choiceC=x[(x.find('\nC)')):(x.find('\nD)'))]
        if choiceD.find('\nE)') > 0 :
            choiceD=x[(x.find('\nD)')):(x.find('\nE)'))]
            choiceE=x[(x.find('\nD')):]
        else :
            choiceD=x[(x.find('\nD')):]
          
        self.text.value=questions[0]     # show the first question
        # say stem - translated in memory
        tts.Speak(stem)                  # add voice
        self.currentQuestion=0           # remember what question we are on
        # model makes a choice
        # say choices - checking in memory for closeness to translated stem
        if choiceA != '' :
            tts.Speak(choiceA)
        if choiceB != '' :
            tts.Speak(choiceB)
        if choiceC != '' :
            tts.Speak(choiceC)
        if choiceD != '' :
            tts.Speak(choiceD)
        if choiceE != '' :
            tts.Speak(choiceE)

    @ccm.parallelize
    def press(self,key): #fix to add in model
        if self.currentQuestion is None: return    # make sure we are waiting
                                                   # for a response
        if key not in 'abcdeABCDE': return      # make sure it's a valid key        

        self.answers.append(key)         # remember the answer
        self.text.value=None
        nextQuestion=self.currentQuestion+1
        self.currentQuestion=None
        if nextQuestion<len(questions):      # more questions to ask?
            yield 0.5 # wait 0.5 seconds
            x=str(questions[nextQuestion])
            # split into stem and choices
            stem=''
            choiceA=''
            choiceB=''
            choiceC=''
            choiceD=''
            choiceE=''
            stem=x[0:(x.find('\nA)'))]
            choiceA=x[(x.find('\nA)')):(x.find('\nB)'))]
            choiceB=x[(x.find('\nB)')):(x.find('\nC)'))]
            choiceC=x[(x.find('\nC)')):(x.find('\nD)'))]
            if choiceD.find('\nE)') > 0 :
                choiceD=x[(x.find('\nD)')):(x.find('\nE)'))]
                choiceE=x[(x.find('\nD')):]
            else :
                choiceD=x[(x.find('\nD')):]
                      
            self.text.value=questions[nextQuestion]  # show the next question
            # say stem - translated in memory
            tts.Speak(stem)     # add voice
            # brief pause
            self.currentQuestion=nextQuestion
            # model makes a choice
            if choiceA != '' :
                tts.Speak(choiceA)
            if choiceB != '' :
                tts.Speak(choiceB)
            if choiceC != '' :
                tts.Speak(choiceC)
            if choiceD != '' :
                tts.Speak(choiceD)
            if choiceE != '' :
                tts.Speak(choiceE)
        else:
            log.answers=self.answers  # record the answers
            self.stop()


######### actr model for reading and making a choice ##########################

log=ccm.log()
env=MultipleChoiceEnvironment(log=log)

import ccm.display
ccm.display.Display(env)
env.run(rate=1)  # to run at real-time
print 'thekey=' + thekey

env.stop()

#env.run() #does not work as text in IDLE

