-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(presidio): improve structured analysis in batchanalyze
- Loading branch information
Showing
4 changed files
with
164 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,84 @@ POST http://localhost:3000/batchanalyze | |
Content-Type: application/json | ||
Accept: application/json | ||
|
||
// expected result | ||
//[ | ||
// "PHONE_NUMBER", | ||
// "URL", | ||
// "EMAIL_ADDRESS", | ||
// "LOCATION", | ||
// "PERSON" | ||
//] | ||
|
||
{ | ||
"json_to_analyze": { | ||
"key_a": { | ||
"key_a1": "My phone number is 212-121-1424" | ||
}, | ||
"key_b": [ | ||
"www.abc.com" | ||
], | ||
"key_c": 3, | ||
"names": [ | ||
"James Bond", | ||
"Clark Kent", | ||
"Hakeem Olajuwon", | ||
"No name here!" | ||
], | ||
"users": [ | ||
{ | ||
"id": 1, | ||
"name": "John Doe", | ||
"email": "[email protected]", | ||
"address": { | ||
"street": "123 Main St", | ||
"city": "Anytown", | ||
"state": "CA", | ||
"postal_code": "12345" | ||
} | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Jane Smith", | ||
"email": "[email protected]", | ||
"address": { | ||
"street": "456 Elm St", | ||
"city": "Somewhere", | ||
"state": "TX", | ||
"postal_code": "67890" | ||
} | ||
}, | ||
{ | ||
"id": 3, | ||
"name": "Alice Johnson", | ||
"email": "[email protected]", | ||
"address": { | ||
"street": "789 Pine St", | ||
"city": "Elsewhere", | ||
"state": "NY", | ||
"postal_code": "11223" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
|
||
### | ||
|
||
POST http://localhost:3000/batchanalyze | ||
Content-Type: application/json | ||
Accept: application/json | ||
|
||
// expected output: | ||
//[ | ||
// "PHONE_NUMBER", | ||
// "URL", | ||
// "PERSON", | ||
// "LOCATION", | ||
// "DATE_TIME", | ||
// "NRP" | ||
//] | ||
|
||
{ | ||
"json_to_analyze": { | ||
"key_F": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from typing import Any, Iterator, Set | ||
|
||
from presidio_analyzer import DictAnalyzerResult, RecognizerResult | ||
|
||
|
||
def convert_all_lists_to_dicts(data: Any) -> Any: | ||
""" | ||
Recursively transforms all lists into dictionaries. | ||
:param data: Incoming data | ||
:return: Transformed data | ||
""" | ||
|
||
if not isinstance(data, (list, dict)): | ||
return data | ||
|
||
if isinstance(data, dict): | ||
return {k: convert_all_lists_to_dicts(v) for k, v in data.items()} | ||
|
||
if isinstance(data, list): | ||
result = dict() | ||
|
||
for k, v in enumerate(data): | ||
# need to convert `int` indexes into strings, otherwise the | ||
# for-loop in the subsequent operation fails. | ||
result[str(k)] = convert_all_lists_to_dicts(v) | ||
|
||
return result | ||
|
||
raise TypeError("Unknown type of incoming data: " + str(type(data))) | ||
|
||
|
||
def extract_data_types_from_results( | ||
dict_results: Iterator[DictAnalyzerResult], | ||
) -> Set[str]: | ||
""" | ||
Extract `entity_type` fields from all nested `RecognizerResult` types within | ||
the incoming tree structure of `DictAnalyzerResult`. | ||
:param dict_results: Result of running `batch_analyzer.analyze_dict` | ||
function on JSON object | ||
:return: Set of entity types | ||
""" | ||
|
||
todo = list(dict_results) | ||
final = set() | ||
|
||
while len(todo) > 0: | ||
# The type of `recognizer_results` is defined here: | ||
# https://github.com/microsoft/presidio/blob/3c7eb8909a3341f2597453fbcaba6184477aa464/presidio-analyzer/presidio_analyzer/dict_analyzer_result.py#L25 | ||
recognizer_results = todo.pop().recognizer_results | ||
|
||
if isinstance(recognizer_results, list): | ||
# In this case `recognizer_results` has type | ||
# `Union[List[RecognizerResult], List[List[RecognizerResult]]]`. | ||
# Once we reach value of type `RecognizerResult`, we can extract | ||
# `entity_type` from it. | ||
for item in recognizer_results: | ||
if isinstance(item, RecognizerResult): | ||
final.add(item.entity_type) | ||
elif isinstance(item, list): | ||
for rr in item: | ||
if isinstance(rr, RecognizerResult): | ||
final.add(rr.entity_type) | ||
else: | ||
raise TypeError("Unknown type of result: " + str(type(rr))) | ||
else: | ||
raise TypeError("Unknown type of result: " + str(type(item))) | ||
|
||
elif isinstance(recognizer_results, Iterator): | ||
todo.extend(recognizer_results) | ||
|
||
else: | ||
raise TypeError("Unknown type of result: " + str(type(recognizer_results))) | ||
|
||
return final |