def classify_string_classifier_rule(state, normalized_answer): """Run the classifier if no prediction has been made yet. Currently this is behind a development flag. """ best_matched_answer_group = None best_matched_answer_group_index = len(state.interaction.answer_groups) best_matched_rule_spec_index = None best_matched_truth_value = rule_domain.CERTAIN_FALSE_VALUE sc = classifier_services.StringClassifier() training_examples = [ [doc, []] for doc in state.interaction.confirmed_unclassified_answers ] for (answer_group_index, answer_group) in enumerate(state.interaction.answer_groups): fuzzy_rule_spec_index = answer_group.get_fuzzy_rule_index() if fuzzy_rule_spec_index is not None: fuzzy_rule_spec = answer_group.rule_specs[fuzzy_rule_spec_index] else: fuzzy_rule_spec = None if fuzzy_rule_spec is not None: training_examples.extend( [[doc, [str(answer_group_index)]] for doc in fuzzy_rule_spec.inputs['training_data']]) if len(training_examples) > 0: sc.load_examples(training_examples) doc_ids = sc.add_examples_for_predicting([normalized_answer]) predicted_label = sc.predict_label_for_doc(doc_ids[0]) if (predicted_label != classifier_services.StringClassifier.DEFAULT_LABEL): predicted_answer_group_index = int(predicted_label) predicted_answer_group = state.interaction.answer_groups[ predicted_answer_group_index] best_matched_truth_value = rule_domain.CERTAIN_TRUE_VALUE for rule_spec in predicted_answer_group.rule_specs: if rule_spec.rule_type == rule_domain.FUZZY_RULE_TYPE: best_matched_rule_spec_index = fuzzy_rule_spec_index break best_matched_answer_group = predicted_answer_group best_matched_answer_group_index = predicted_answer_group_index return { 'outcome': best_matched_answer_group.outcome.to_dict(), 'answer_group_index': best_matched_answer_group_index, 'classification_certainty': best_matched_truth_value, 'rule_spec_index': best_matched_rule_spec_index, } else: return None return None
def classify_string_classifier_rule(state, normalized_answer): """Run the classifier if no prediction has been made yet. Currently this is behind a development flag. """ best_matched_answer_group = None best_matched_answer_group_index = len(state.interaction.answer_groups) best_matched_rule_spec_index = None sc = classifier_services.StringClassifier() training_examples = [ [doc, []] for doc in state.interaction.confirmed_unclassified_answers ] for (answer_group_index, answer_group) in enumerate(state.interaction.answer_groups): classifier_rule_spec_index = answer_group.get_classifier_rule_index() if classifier_rule_spec_index is not None: classifier_rule_spec = answer_group.rule_specs[ classifier_rule_spec_index] else: classifier_rule_spec = None if classifier_rule_spec is not None: training_examples.extend( [[doc, [str(answer_group_index)]] for doc in classifier_rule_spec.inputs['training_data']]) if len(training_examples) > 0: sc.load_examples(training_examples) doc_ids = sc.add_docs_for_predicting([normalized_answer]) predicted_label = sc.predict_label_for_doc(doc_ids[0]) if (predicted_label != classifier_services.StringClassifier.DEFAULT_LABEL): predicted_answer_group_index = int(predicted_label) predicted_answer_group = state.interaction.answer_groups[ predicted_answer_group_index] for rule_spec in predicted_answer_group.rule_specs: if rule_spec.rule_type == exp_domain.CLASSIFIER_RULESPEC_STR: best_matched_rule_spec_index = classifier_rule_spec_index break best_matched_answer_group = predicted_answer_group best_matched_answer_group_index = predicted_answer_group_index return { 'outcome': best_matched_answer_group.outcome.to_dict(), 'answer_group_index': best_matched_answer_group_index, 'rule_spec_index': best_matched_rule_spec_index, } else: return None return None
def setUp(self): super(StringClassifierUnitTests, self).setUp() self.string_classifier = classifier_services.StringClassifier() self.string_classifier.load_examples(self._EXAMPLES_TRAIN)