Пример #1
0
class AssociatedPair(QWidget):
    """ Gui for a simple vocabulary trainer
    """

    def __init__(self, use_wasabi=False):
        super(AssociatedPair, self).__init__()

        # Speech recognition:
        grammar = Grammar("example grammar")
        # rules = [NumberNullRule, NumberOneRule, NumberTwoRule, NumberThreeRule,
        #         NumberFourRule, NumberFiveRule, NumberSixRule,
        #         NumberSevenRule, NumberEightRule, NumberNineRule]

        # for rule in rules:
        #    grammar.add_rule(rule(self.answer_given))

        # grammar.load()

        grammar.add_rule(NumberNullRule(self.answer_given))
        grammar.add_rule(NumberOneRule(self.answer_given))
        grammar.add_rule(NumberTwoRule(self.answer_given))
        grammar.add_rule(NumberThreeRule(self.answer_given))
        grammar.add_rule(NumberFourRule(self.answer_given))
        grammar.add_rule(NumberFiveRule(self.answer_given))
        grammar.add_rule(NumberSixRule(self.answer_given))
        grammar.add_rule(NumberSevenRule(self.answer_given))
        grammar.add_rule(NumberEightRule(self.answer_given))
        grammar.add_rule(NumberNineRule(self.answer_given))

        grammar.load()
        self.training = True

        if DEBUG:
            self.emo_output = QLabel("")
            self.cog_output = QLabel("")
            self.speech_output = QLabel("")

            agent_layout = QGridLayout()
            agent_layout.addWidget(QLabel("Emotional Output:"), 0, 0)
            agent_layout.addWidget(self.emo_output, 0, 1)
            agent_layout.addWidget(QLabel("Cognition Output:"), 1, 0)
            agent_layout.addWidget(self.cog_output, 1, 1)
            agent_layout.addWidget(QLabel("Speech Output:"), 2, 0)
            agent_layout.addWidget(self.speech_output, 2, 1)

            agent_layout.setColumnMinimumWidth(0, 100)
            agent_layout.setColumnMinimumWidth(1, 500)
            agent = QWidget()
            agent.setLayout(agent_layout)

            self.start_button = QPushButton("&Start")
            self.start_button.clicked.connect(self.start_button_clicked)

            main_layout = QBoxLayout(2)
            main_layout.addWidget(agent)
            main_layout.addWidget(self.start_button)
            self.input_widget = self.get_input_widget()
            self.input_widget.hide()
            main_layout.addWidget(self.input_widget)

            self.setLayout(main_layout)
            self.resize(600, 400)
            # 192.168.0.46
            self.exp = Environment(use_wasabi)
            self.waiting_for_answer = False

            # emotion, cog, speech = self.exp.start()
            # self.update_output(emotion, cog, speech)
            self.phase = 0
        else:
            self.speech_output = QLabel("")

            self.start_button = QPushButton("&Start")
            self.start_button.clicked.connect(self.start_button_clicked)

            main_layout = QBoxLayout(2)
            main_layout.addWidget(self.start_button)
            self.input_widget = self.get_input_widget()
            self.input_widget.hide()
            main_layout.addWidget(self.input_widget)

            self.setLayout(main_layout)
            self.resize(300, 300)
            self.width = 300
            self.height = 300
            # 192.168.0.46

            self.exp = Environment(use_wasabi)
            self.waiting_for_answer = False

            # emotion, cog, speech = self.exp.start()
            # self.update_output(emotion, cog, speech)
            self.phase = 0
            QTimer.singleShot(4000, self.second_introduction)

    def second_introduction(self):

        self.exp.agent.say("Bitte sprechen Sie die folgenden Zahlen nach.")
        QTimer.singleShot(6000, self.train_number)

    def train_number(self):
        if self.exp.test_nr_index < len(self.exp.test_nr) - 1:
            self.exp.train_number()
            QTimer.singleShot(3000, self.train_number)
        else:
            print "training finished"
            if self.exp.test_correct >= 9:
                self.exp.agent.say("Alles richtig. Starten Sie den Test.")
                print "Alles ok"
            else:
                print "Nochmal"
                self.exp.test_correct = 0
                self.exp.test_nr_index = -1
                self.exp.agent.say("Mehr als ein Wort falsch. Noch ein Durchgang.")
                QTimer.singleShot(6000, self.train_number)

    def get_input_widget(self):
        # Nr layout:
        button0 = QPushButton("&0")
        button1 = QPushButton("&1")
        button2 = QPushButton("&2")
        button3 = QPushButton("&3")
        button4 = QPushButton("&4")
        button5 = QPushButton("&5")
        button6 = QPushButton("&6")
        button7 = QPushButton("&7")
        button8 = QPushButton("&8")
        button9 = QPushButton("&9")

        button0.clicked.connect(self.bu0_clicked)
        button1.clicked.connect(self.bu1_clicked)
        button2.clicked.connect(self.bu2_clicked)
        button3.clicked.connect(self.bu3_clicked)
        button4.clicked.connect(self.bu4_clicked)
        button5.clicked.connect(self.bu5_clicked)
        button6.clicked.connect(self.bu6_clicked)
        button7.clicked.connect(self.bu7_clicked)
        button8.clicked.connect(self.bu8_clicked)
        button9.clicked.connect(self.bu9_clicked)

        nr_layout = QGridLayout()
        nr_layout.addWidget(button7, 0, 0)
        nr_layout.addWidget(button8, 0, 1)
        nr_layout.addWidget(button9, 0, 2)
        nr_layout.addWidget(button4, 1, 0)
        nr_layout.addWidget(button5, 1, 1)
        nr_layout.addWidget(button6, 1, 2)
        nr_layout.addWidget(button1, 2, 0)
        nr_layout.addWidget(button2, 2, 1)
        nr_layout.addWidget(button3, 2, 2)
        nr_layout.addWidget(button0, 3, 0)
        nr_widget = QWidget()
        nr_widget.setLayout(nr_layout)
        return nr_widget

    def start_button_clicked(self):
        self.start_button.hide()
        self.input_widget.show()
        self.training = False
        self.exp.save_start_time()
        self.exp.start()
        QTimer.singleShot(6000, self.present_word)

    def bu0_clicked(self):
        self.answer_given(0)

    def bu1_clicked(self):
        self.answer_given(1)

    def bu2_clicked(self):
        self.answer_given(2)

    def bu3_clicked(self):
        self.answer_given(3)

    def bu4_clicked(self):
        self.answer_given(4)

    def bu5_clicked(self):
        self.answer_given(5)

    def bu6_clicked(self):
        self.answer_given(6)

    def bu7_clicked(self):
        self.answer_given(7)

    def bu8_clicked(self):
        self.answer_given(8)

    def bu9_clicked(self):
        self.answer_given(9)

    def answer_given(self, nr):
        if self.training:
            self.exp.check_nr(str(nr))
        else:

            if self.waiting_for_answer:
                # print '  @', now - self.exp.start_time, 'answer received: Took', now - self.exp.start_time_answer
                self.exp.evaluate(str(nr), datetime.datetime.now())

            self.waiting_for_answer = False

    def update_output(self, emotion, cog, speech):
        """ Updates the text output of the agent.
        """
        self.speech_output.setText(speech)
        if DEBUG:
            self.emo_output.setText(emotion)
            self.cog_output.setText(cog)

    def present_word(self):

        if self.exp.has_next():

            # print '@', now - self.exp.start_time, 'present word called'

            emotion, cog, speech = self.exp.present_word(datetime.datetime.now())
            self.update_output(emotion, cog, speech)
            self.waiting_for_answer = True
            QTimer.singleShot(5000, self.present_number)
        else:
            if self.exp.current_run < self.exp.total_runs - 1:
                self.exp.reset()
                QTimer.singleShot(10000, self.present_word)
            else:
                self.end()

    def present_number(self):

        # print '@', now-self.exp.start_time, 'present number called'

        self.waiting_for_answer = False
        emotion, cog, speech = self.exp.present_number(datetime.datetime.now())
        self.update_output(emotion, cog, speech)
        QTimer.singleShot(5000, self.present_word)

    def end(self):
        """ End vocabulary test
        """
        emotion, speech = self.exp.end()
        if DEBUG:
            self.emo_output.setText(emotion)
        self.speech_output.setText(speech)
        self.input_widget.hide()