pii_ner / pii_ner.py
acram's picture
Upload pii_ner.py
ba26015 verified
raw
history blame
2.34 kB
import json
from itertools import chain
import datasets
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """[PII NER]"""
_NAME = "pii_ner"
_VERSION = "1.0.1"
_URL = f'https://huggingface.co/datasets/acram/{_NAME}/raw/main/dataset'
_URLS = {
str(datasets.Split.TEST): [f'{_URL}/test.json'],
str(datasets.Split.TRAIN): [f'{_URL}/train.json'],
str(datasets.Split.VALIDATION): [f'{_URL}/valid.json'],
}
class PiiNERConfig(datasets.BuilderConfig):
"""BuilderConfig"""
def __init__(self, **kwargs):
"""BuilderConfig.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(PiiNERConfig, self).__init__(**kwargs)
class PiiNER(datasets.GeneratorBasedBuilder):
"""Dataset."""
BUILDER_CONFIGS = [
PiiNERConfig(name=_NAME, version=datasets.Version(_VERSION), description=_DESCRIPTION),
]
def _split_generators(self, dl_manager):
downloaded_file = dl_manager.download_and_extract(_URLS)
return [datasets.SplitGenerator(name=i, gen_kwargs={"filepaths": downloaded_file[str(i)]})
for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]]
def _generate_examples(self, filepaths):
_key = 0
for filepath in filepaths:
logger.info(f"generating examples from = {filepath}")
with open(filepath, encoding="utf-8") as f:
_list = [i for i in f.read().split('\n') if len(i) > 0]
for i in _list:
data = json.loads(i)
yield _key, data
_key += 1
def _info(self):
#names = ["B-LOC", "B-MISC", "B-ORG", "B-PER", "I-LOC", "I-MISC", "I-ORG", "I-PER", "O"]
names=['O','B-NAME_STUDENT','I-NAME_STUDENT','B-STREET_ADDRESS','I-STREET_ADDRESS','B-USERNAME','B-EMAIL','B-URL_PERSONAL','B-PHONE_NUM','B-DRIVING_LICENSE',
'B-PASSPORT','B-PAN_NUMBER','B-ID_NUM','B-AADHAR_ID']
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"tokens": datasets.Sequence(datasets.Value("string")),
"tags": datasets.Sequence(datasets.features.ClassLabel(names=names)),
}
),
supervised_keys=None,
)