NER Anonymizer
This package contains some developmental tools to anonymize a pandas dataframe.
NER Anonymizer contains a class DataAnonymizer which handles anonymization for both free text and categorical columns in a pandas dataframe:
- For free text columns, it uses a pretrained model from the transformers package to perform named entity recognition (NER) to pick up user specified entities such as location and person, generate a MD5 hash for the entity, replaces the entity with the hash, and stores the hash to entity in a dictionary
- For categorical columns, it simply generates a MD5 hash for every category, replaces the category with the hash, and stores the hash to category in a dictionary
The saved dictionary can then be used for de-anonymization and the original dataset is obtained. Referential integrity is preserved as the same hash will be generated for the same category / entity.
Installation
Install the package with pip
pip install ner-anonymizer
Example Usage
The package uses the NER model dslim/bert-base-NER by default. To anonymize a particular pandas dataframe, df, using a pretrained NER model:
import ner_anonymizer
# to anonymize
anonymizer = ner_anonymizer.DataAnoynmizer(
pretrained_model_name="dslim/bert-base-NER",
label_list=["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"],
labels_to_anonymize=["B-PER", "I-PER", "B-LOC", "I-LOC"]
)
anonymized_df, hash_dictionary = anonymizer.anonymize(
df=df,
free_text_columns=["free_text_column_1", "free_text_column_2"],
categorical_columns=["categorical_column_1"],
)
# to de-anonymize
de_anonymized_df = ner_anonymizer.de_anonymize_data(anonymized_df, hash_dictionary)
You may specify for the argument pretrained_model_name any available pre-trained NER model from the transformers package in the links below (do note that you will need to specify the labels that the NER model uses, label_list, and from that list, the labels you want to anonymize, labels_to_anonymize):
You may also view an example notebook in the following directory examples/example_usage.ipynb.
Metadata
Release files for ner-anonymizer 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ner-anonymizer-0.1.6.tar.gz | 5.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ner_anonymizer-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / ner-anonymizer-0.1.6.tar.gz
| Download URL | ner-anonymizer-0.1.6.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a3a21ac3ce956dd5847b90006cafb5121c6f23682d3fa9340130e37ec392d47
|
|
BLAKE2b-256 checksum How to use checksums |
38a9fc67158397973552c5bc67f6a22e8c9f998baf10cb39f84731bfacde4209
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
|
Release files / ner_anonymizer-0.1.6-py3-none-any.whl
| Download URL | ner_anonymizer-0.1.6-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
280dbbbf08e543ff88bbbafd6a7dc9848ea028dd25de048255e8424640dd75d2
|
|
BLAKE2b-256 checksum How to use checksums |
142b1cd6198e2a68ad62a75434ea3303aa7e3d4079cfeef96d2f4ed30c35e755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
|
Security for ner-anonymizer 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.