Exemple #1
0
def test_label_merging():
    aligned_predictions = [
        {"target_labels": ["O", "O"], "extractor_labels":
            {"A": ["O", "O"]}},
        {"target_labels": ["LOC", "O", "O"], "extractor_labels":
            {"A": ["O", "O", "O"]}}
    ]

    assert all(merge_labels(aligned_predictions) ==
               ["O", "O", "LOC", "O", "O"])
    assert all(merge_labels(aligned_predictions, "A") ==
               ["O", "O", "O", "O", "O"])
Exemple #2
0
def test_label_merging():
    aligned_predictions = [
        {"target_labels": ["O", "O"], "extractor_labels":
            {"A": ["O", "O"]}},
        {"target_labels": ["LOC", "O", "O"], "extractor_labels":
            {"A": ["O", "O", "O"]}}
    ]

    assert all(merge_labels(aligned_predictions) ==
               ["O", "O", "LOC", "O", "O"])
    assert all(merge_labels(aligned_predictions, "A") ==
               ["O", "O", "O", "O", "O"])
def test_entity():
    #load model
    interpreter = Interpreter.load(model_location)

    duckling_extractors = {"ner_duckling", "ner_duckling_http"}

    #create dictionary of entity results
    entity_results = defaultdict(lambda: defaultdict(list))

    #get extractors of the interpreter
    extractors = evaluate.get_entity_extractors(interpreter)

    #get entity predictions and tokens
    entity_predictions, tokens = evaluate.get_entity_predictions(
        interpreter, testing_data)

    # Create classification report
    if duckling_extractors.intersection(extractors):
        entity_predictions = evaluate.remove_duckling_entities(
            entity_predictions)
        extractors = evaluate.remove_duckling_extractors(extractors)

    if not extractors:
        return entity_results

    #get entity_targets
    entity_targets = evaluate.get_entity_targets(testing_data)

    #get aligned_prections
    aligned_predictions = evaluate.align_all_entity_predictions(
        entity_targets, entity_predictions, tokens, extractors)

    merged_targets = evaluate.merge_labels(aligned_predictions)
    merged_targets = evaluate.substitute_labels(merged_targets, "O",
                                                "no_entity")

    for extractor in extractors:
        merged_predictions = evaluate.merge_labels(aligned_predictions,
                                                   extractor)
        merged_predictions = evaluate.substitute_labels(
            merged_predictions, "O", "no_entity")
        report, precision, f1, accuracy = evaluate.get_evaluation_metrics(
            merged_targets, merged_predictions)
        entity_results[extractor]["Accuracy"].append(accuracy)
        entity_results[extractor]["F1-score"].append(f1)
        entity_results[extractor]["Precision"].append(precision)

    print("entity_results:  {}\n".format(entity_results),
          "Classification report: \n{}".format(report))