Beispiel #1
0
def test_given_anonymize_called_with_multiple_scenarios_then_expected_results_returned(
    anonymize_scenario, ):
    anonymizer_request_dict = json.loads(
        get_scenario_file_content("anonymize",
                                  f"{anonymize_scenario}.in.json"))
    expected_anonymize_result_json = json.loads(
        get_scenario_file_content("anonymize",
                                  f"{anonymize_scenario}.out.json"))
    items = []
    for item in expected_anonymize_result_json["items"]:
        items.append(
            AnonymizedEntity(
                item["anonymizer"],
                item["entity_type"],
                item["start"],
                item["end"],
                item["anonymized_text"],
            ))
    expected_anonymize_result = AnonymizerResult(
        expected_anonymize_result_json["text"], items)
    engine = AnonymizerEngine()
    anonymizers_config = AnonymizerRequest.get_anonymizer_configs_from_json(
        anonymizer_request_dict)
    analyzer_results = AnonymizerRequest.handle_analyzer_results_json(
        anonymizer_request_dict)

    try:
        actual_anonymize_result = engine.anonymize(
            anonymizer_request_dict.get("text"), analyzer_results,
            anonymizers_config)
    except Exception as e:
        actual_anonymize_result = str(e)

    assert actual_anonymize_result == expected_anonymize_result
Beispiel #2
0
def run_anonymizer(engine, text, analyzer_results, transformations=None):
    req = AnonymizerRequest({
        'analyzer_results': [res.to_dict() for res in analyzer_results],
        'text': text,
        'transformations': transformations
    }, AnonymizerEngine.builtin_anonymizers)
    trans = req.get_transformation('PERSON')
    trans['replace_text'] = "[GDPRREDACT]"
    return engine.anonymize(req)
Beispiel #3
0
        def anonymize():
            content = request.get_json()
            if not content:
                return ErrorResponse("Invalid request json").to_json(), 400

            anonymizers_config = AnonymizerRequest.get_anonymizer_configs_from_json(
                content)
            analyzer_results = AnonymizerRequest.handle_analyzer_results_json(content)
            text = self.engine.anonymize(text=content.get("text"),
                                         analyzer_results=analyzer_results,
                                         anonymizers_config=anonymizers_config)
            return jsonify(result=text)
Beispiel #4
0
        def anonymize():
            content = request.get_json()
            if not content:
                raise BadRequest("Invalid request json")

            anonymizers_config = AnonymizerRequest.get_anonymizer_configs_from_json(
                content)
            analyzer_results = AnonymizerRequest.handle_analyzer_results_json(
                content)
            anoymizer_result = self.engine.anonymize(
                text=content.get("text"),
                analyzer_results=analyzer_results,
                anonymizers_config=anonymizers_config,
            )
            return Response(anoymizer_result.to_json(),
                            mimetype="application/json")
Beispiel #5
0
def test_given_conflicting_analyzer_results_then_none_conflicting_results_returned():
    payload = get_dup_payload()
    analyze_results = AnonymizerRequest.handle_analyzer_results_json(payload)
    assert len(analyze_results) == len(payload.get("analyzer_results"))
    sorted_results = analyze_results.to_sorted_unique_results()
    assert len(sorted_results) == 4
    for index in range(len(sorted_results) - 1):
        assert list(sorted_results)[index].start < list(sorted_results)[index + 1].start
        assert list(sorted_results)[index].end < list(sorted_results)[index + 1].end
Beispiel #6
0
def test_given_anonymize_called_with_error_scenarios_then_expected_errors_returned(
    anonymize_scenario, ):
    anonymizer_request_dict = json.loads(
        get_scenario_file_content("anonymize",
                                  f"{anonymize_scenario}.in.json"))
    expected_anonymize_result_json = json.loads(
        get_scenario_file_content("anonymize",
                                  f"{anonymize_scenario}.out.json"))
    engine = AnonymizerEngine()
    anonymizers_config = AnonymizerRequest.get_anonymizer_configs_from_json(
        anonymizer_request_dict)
    analyzer_results = AnonymizerRequest.handle_analyzer_results_json(
        anonymizer_request_dict)

    try:
        actual_anonymize_result = engine.anonymize(
            anonymizer_request_dict.get("text"), analyzer_results,
            anonymizers_config)
    except Exception as e:
        actual_anonymize_result = str(e)

    assert actual_anonymize_result == expected_anonymize_result_json