Ejemplo n.º 1
0
    def get_answer_helper(self, question_raw, status):
        question = {
            "question": to_unicode(question_raw),
            "fixed_typos": spellcheck(question_raw),
        }
        question["preprocessed"] = question["fixed_typos"].split(" ")

        answer = self.get_answer(question=question, status=status)

        if "answer" not in answer.keys():
            raise Exception("answer not found")
        if "score" not in answer.keys():
            raise Exception("score not found")
        if "state_update" not in answer.keys():
            answer["state_update"] = {}
        if not (0.0 <= answer["score"]):
            raise Exception("invalid score")

        logging.info(
            "%s answered: %s [%f]" % (
                self.my_name(),
                answer["answer"],
                answer["score"],
            ), )

        return answer
Ejemplo n.º 2
0
    def get_answer(self, question, status):
        if self.q_asked:
            temp = question["question"]
            q = to_unicode(temp)
            if self.keynow == "sex":
                return self.recsex(q)
            elif self.keynow == "age":
                return self.recage(q)
            elif self.keynow == "firstyear":
                return self.recfirst(q)
            elif self.keynow == "candidate":
                return self.reccand(q)
            elif self.keynow == "student":
                return self.recstudent
            else:
                return {"answer": "Brak kategorii", "score": 0}

        #zadanie pierwszego pytania
        for key in status:
            if (status[key] == "?"):
                try:
                    question = self.questions[key]
                    self.keynow = key
                    self.q_asked = True
                    return {"answer": question.decode('utf-8'), "score": 1.0}
                except KeyError:
                    return {
                        "answer": "Nie wiem, o co mnie pytasz.",
                        "score": 0
                    }
        #nie ma nic do powiedzenia
        return {"answer": "Tu nie powinnismy dojsc nigdy", "score": 0}
Ejemplo n.º 3
0
 def get_answer(self, question, status, **kwargs):
     q = question["question"]
     temp = to_unicode(q)
     for key in self.answers:
         if re.match(key, temp):
             st = {}
             try:
                 if status["firstyear"]:
                     score = self.answers[key][1] * 0.99
                 else:
                     score = 0.2
             except KeyError:
                 score = self.answers[key][1]
                 st = {"firstyear": "?"}
             return {
                 "answer": (self.answers[key][0]).decode('utf-8'),
                 "score": score,
                 "state_update": st
             }
     return {
         "answer": "Nie umiem odpowiedzieć.",
         "score": 0,
     }
Ejemplo n.º 4
0
 def load_sentences(self, file, sep):
     logging.info('loading sentences from %s', file)
     with open(file) as f:
         added = False
         for l in f.readlines():
             last_added = added
             added = False
             try:
                 l = to_unicode(l).strip()  # noqa
                 if l.startswith('#') or len(l) == 0:
                     continue
                 l = l.split(sep, 1)[1]  # noqa
                 if last_added:
                     self.responses[-1] = l
                 l = self.preprocess(l)  # noqa
                 if not self.has_vector(l):
                     continue
                 self.idf.add_document(l)
                 self.sentences.append(l)
                 self.responses.append('')
                 added = True
             except Exception:
                 # traceback.print_exc()
                 pass
Ejemplo n.º 5
0
 def get_answer(self, question, *args, **kwargs):
     q = to_unicode(question['fixed_typos'])
     sentence = self.preprocess(q)
     vec = self.get_vector(sentence)
     if vec is None:
         return {
             "answer": "nic nie zrozumiałem",
             "score": 0
         }
     cosine = (self.vectors * vec.reshape(1, -1)).sum(axis=1)
     best = sorted(range(len(cosine)), key=lambda x: -cosine[x])
     best = filter(lambda i: self.responses[i] != '', best)
     if len(best) == 0:
         return {
             "answer": "nic nie zrozumiałem",
             "score": 0
         }
     
     best = best[:5]
     answer, score = self.choose_average(cosine[best], [self.responses[i] for i in best])
     return {
         "answer": answer,
         "score": score ** 0.7
     }