def get_answers(self, user_input): """ Given a user_input, returns all possible answers sorted by the most frequent to the least frequent :param user_input :return: sorted list of tuples (string, int), the first value is the answer and the second the #occurences if there is no answers, a empty list is returned """ if isinstance(user_input, str): user_input = user_input.decode("utf-8") user_input = RegexUtil.custom_strip(user_input) if user_input not in self.__user_input_answers_dic: return None return self.__user_input_answers_dic[user_input]
def get_answer(self, user_input): """ Given a user_input, returns the most probable answer. If there is draw, it is returned one of them :param user_input :return: The answer (string) """ if isinstance(user_input, str): user_input = user_input.decode("utf-8") user_input = RegexUtil.custom_strip(user_input) answers = self.get_answers(user_input) if answers is None: return AnswerPickerAnswerResult.INVALID_USER_INPUT if len(answers) == 0: return AnswerPickerAnswerResult.TRIGGER_NOT_FOUND else: return answers[0][0] # [first answer] [first element tuple]
def _evaluate(self, answer_picker, questions_file_path, max_n_answers): answers_list = list() with open(questions_file_path) as questionFile: for question in questionFile: # remove - and whitespace question = RegexUtil.custom_strip(question) answer = answer_picker.get_answer(question) # check if input is invalid or if there is no possible answer if answer == AnswerPickerAnswerResult.INVALID_USER_INPUT or AnswerPickerAnswerResult.TRIGGER_NOT_FOUND == answer: annotation = 'n' else: annotation = self._get_annotation(question, answer, max_n_answers) # add to the list for future analysis answers_list.append(annotation) questionFile.close() return answers_list