Example #1
0
    def ask(self, question):
        # Repetition
        if self.previn and clean_string(question) == clean_string(self.previn[0]):
            res = random.choice(self.repetitiondb)
            res = res.replace('----', "'%s'"%' '.join(self.prevout[0]))
            yield self.macro.subst(res)
            return
        
        answers = []
        lang = detect_lang(question, self.lastlang)
        if lang is None:
            lang = self.lastlang
        #print 'Language:', lang
        #print 'Last Language:', self.lastlang
        if lang != self.lastlang:
            tran = translate(question, lang).encode('utf-8')
            lasttran = translate(question, self.lastlang).encode('utf-8')
            lang, prob, tran = max([(lang, ratio_correct(tran), tran),
                                    (self.lastlang, ratio_correct(lasttran), lasttran)],
                                   key=lambda x: x[1])
            question = tran
            #print 'Chosen:', lang, 'prob:', prob
        #print 'Question:', question

        is_foreign = lang != 'en'
        #if is_foreign:
        #    question = language.translate(question, lang).encode('utf-8')
        
        if question is None:
            return
        
        #check = check_blank(question)
        #if check is not None:
        #    return [check]
        
        if check_for_end(question):
            self.running = False
            return
        
        #question = self._clean_text(question)
        
        for sentence in sentence_split(question):
            answer = None
            for handler in self.handlers:
                if handler.check(sentence):
                    try:
                        ans = handler.answer(sentence)
                    except HALcannotHandle:
                        continue
                    else:
                        answer = ans
                        break
            if answer is None:
                answer = random.choice(self.generic)
            answer = self.macro.subst(answer)
            if self.semantics:
                lang, inter, answer = HALtran.transform(answer)
            if self.rndname:
                answer = rndname(answer, self.user)
            if is_foreign:
                #print 'Answer:', answer
                answer = translate(answer, 'en', lang)
            yield answer
            answers.append(answer)
        #answers = [self.macro.subst(answer) for answer in answers]
        self.do_speech(' '.join(answers))
        self.previn.appendleft(question)
        self.prevout.appendleft(answers)
Example #2
0
def detect(m):
    text = m.group(1)
    ret = detect_lang(text)
    if ret is None:
        ret = 'en' # Default
    return ret