Exemplo n.º 1
0
class CurrencyLooper:

    def __init__(self, configFile):

        self.speech = Speech(configFile)
        self.nlp = Nlp(configFile)

    def __repr__(self):
        pass

    def outputLine(self, content):

        print('\n\n{}'.format(content));

        self.speech.voiceOut(content);

    def runWithVoice(self, rulefile):

        def clearScreen():
            os.system('clear')

        def str2int(content):
            return int(content)

        self.outputLine('请说出您的名字吧');
        name = self.speech.getVoiceText()

        if name is not None:
            names = self.nlp.getPersons(name)

            if names is not None:
                name = names[0]
 
        if name is None:
            name = ''

        self.outputLine('欢迎您!{}'.format(name));

        formulaRule = FormulaRule(rulefile)

        self.outputLine('总共是{}关,您想从第几关开始?'.format(formulaRule.getSize()))

        text = self.speech.getVoiceText()

        if text is not None:
            text = Numeric.chinese2arabic(text)
            numbers = getMatchList(r'\d+', text, str2int)
            if len(numbers) > 0:
                formulaRule.set(numbers[0] - 1)

        while True:

            rule = formulaRule.createFormula()

            clearScreen()
            self.outputLine(u'当前第{}关,如果需要跳过或者退出,请说出跳过或者退出。'.format(formulaRule.getIndex() + 1))

            text = self.speech.getVoiceText()

            if text is not None and self.nlp.isSimilar('退出', text):
                self.outputLine('已退出')
                break

            if text is None or not self.nlp.isSimilar('跳过', text):

                clearScreen()

                formula = Formula.parse(rule)

                self.outputLine(u'第{}关题目如下:'.format(formulaRule.getIndex() + 1))

                msg = '{} = 多少?'.format(formula)

                for i in range(3):

                    if i is not 0:
                        self.outputLine('请再想想吧?')

                    self.outputLine(msg)

                    text = self.speech.getVoiceText()

                    if text is None:
                        continue

                    text = Numeric.chinese2arabic(text)

                    items = self.nlp.getNumbers(text)

                    if items is None:
                        continue

                    result = Currency.createFromReadableString(items)

                    if result is None:
                        continue

                    answer = result.toCurrency()

                    correct = False

                    try:
                        condition = '{0} == {1} or ({0} > {1} and {0} < {1} + 0.01) or ({1} > {0} and {1} < {0} + 0.01)'.format(rule, answer)

                        if eval(condition):
                            correct = True
                    except Exception as e:
                        pass

                    try:
                        answer = Formula.parse(answer)
                        answer = answer.getResult()
                    except CurrencyException as e:
                        pass

                    self.outputLine('{} = {}'.format(formula, answer))

                    if correct:
                        self.outputLine('回答正确!恭喜您通过第{}关!'.format(formulaRule.getIndex() + 1))
                        break

                else:
                    self.outputLine('可能您不太理解,正确答案是:{} = {}。'.format(formula, formula.getResult()))
                    continue

            else:
                self.outputLine(u'已跳过第{}关。'.format(formulaRule.getIndex() + 1))

            clearScreen()

            if not formulaRule.forward():
                self.outputLine('恭喜您!{},通过全部{}关!'.format(name, formulaRule.getSize()))
                break

    def run(self, rulefile):

        def clearScreen():
            os.system('clear')

        formulaRule = FormulaRule(rulefile)

        while True:

            rule = formulaRule.createFormula()

            clearScreen()
            print(u'第{}关,按回车键继续,按其它键跳过。'.format(formulaRule.getIndex() + 1))

            if '\r' == getch():

                clearScreen()

                formula = Formula.parse(rule)
                print(u'第{}关\n\n{} = ?'.format(formulaRule.getIndex() + 1, formula))

                msg = '请输入答案:'

                for i in range(3):

                    if i is not 0:
                        print('请再想想?')

                    try:
                        answer = raw_input(msg)
                    except NameError:
                        answer = input(msg)

                    correct = False

                    try:
                        condition = '{0} == {1} or ({0} > {1} and {0} < {1} + 0.01) or ({1} > {0} and {1} < {0} + 0.01)'.format(rule, answer)

                        if eval(condition):
                            correct = True
                    except Exception as e:
                        pass

                    try:
                        answer = Formula.parse(answer)
                        answer = answer.getResult()
                    except CurrencyException as e:
                        pass

                    print('{} = {}'.format(formula, answer))

                    if correct:
                        print('回答正确!恭喜您通过第{}关!'.format(formulaRule.getIndex() + 1))
                        break

                else:

                    print('可能您不太理解,按任意键看答案吧。')
                    getch()

                    print('正确答案是:{} = {}'.format(formula, formula.getResult()))

                    print('按任意键再来一次吧。')
                    getch()

                    continue

                print('按任意键继续')
                getch()

            clearScreen()

            if not formulaRule.forward():
                print('恭喜您通过全部{}关!'.format(formulaRule.getSize()))
                break

    @staticmethod
    def autorun():

        def clearScreen():
            os.system('clear')

        while True:

            msg = '例如:1元1角 + 2元2角 - 3元, 输入为:1.1 + 2.2 - 3\n请输入式子:\n'
            try:
                content = raw_input(msg)
            except NameError:
                content = input(msg)

            content.strip()

            if len(content) is 0:
                break

            try:
                clearScreen()

                formula = Formula.parse(content)
                print(formula)

                print('\n\n按任意键看答案')
                getch()

                result = formula.getResult()
                print('\n\n', result)

            except CurrencyException as e:
                print(e)

            print('按任意键继续')
            getch()

            clearScreen()