Upload pii_ner.py
Browse files- pii_ner.py +66 -0
pii_ner.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from itertools import chain
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
logger = datasets.logging.get_logger(__name__)
|
6 |
+
_DESCRIPTION = """[PII NER]"""
|
7 |
+
_NAME = "pii_ner"
|
8 |
+
_VERSION = "1.0.1"
|
9 |
+
|
10 |
+
_URL = f'https://huggingface.co/datasets/acram/{_NAME}/raw/main/dataset'
|
11 |
+
_URLS = {
|
12 |
+
str(datasets.Split.TEST): [f'{_URL}/test.json'],
|
13 |
+
str(datasets.Split.TRAIN): [f'{_URL}/train.json'],
|
14 |
+
str(datasets.Split.VALIDATION): [f'{_URL}/valid.json'],
|
15 |
+
}
|
16 |
+
|
17 |
+
|
18 |
+
class PiiNERConfig(datasets.BuilderConfig):
|
19 |
+
"""BuilderConfig"""
|
20 |
+
|
21 |
+
def __init__(self, **kwargs):
|
22 |
+
"""BuilderConfig.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
**kwargs: keyword arguments forwarded to super.
|
26 |
+
"""
|
27 |
+
super(PiiNERConfig, self).__init__(**kwargs)
|
28 |
+
|
29 |
+
|
30 |
+
class PiiNER(datasets.GeneratorBasedBuilder):
|
31 |
+
"""Dataset."""
|
32 |
+
|
33 |
+
BUILDER_CONFIGS = [
|
34 |
+
PiiNERConfig(name=_NAME, version=datasets.Version(_VERSION), description=_DESCRIPTION),
|
35 |
+
]
|
36 |
+
|
37 |
+
def _split_generators(self, dl_manager):
|
38 |
+
downloaded_file = dl_manager.download_and_extract(_URLS)
|
39 |
+
return [datasets.SplitGenerator(name=i, gen_kwargs={"filepaths": downloaded_file[str(i)]})
|
40 |
+
for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]]
|
41 |
+
|
42 |
+
def _generate_examples(self, filepaths):
|
43 |
+
_key = 0
|
44 |
+
for filepath in filepaths:
|
45 |
+
logger.info(f"generating examples from = {filepath}")
|
46 |
+
with open(filepath, encoding="utf-8") as f:
|
47 |
+
_list = [i for i in f.read().split('\n') if len(i) > 0]
|
48 |
+
for i in _list:
|
49 |
+
data = json.loads(i)
|
50 |
+
yield _key, data
|
51 |
+
_key += 1
|
52 |
+
|
53 |
+
def _info(self):
|
54 |
+
#names = ["B-LOC", "B-MISC", "B-ORG", "B-PER", "I-LOC", "I-MISC", "I-ORG", "I-PER", "O"]
|
55 |
+
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',
|
56 |
+
'B-PASSPORT','B-PAN_NUMBER','B-ID_NUM','B-AADHAR_ID']
|
57 |
+
return datasets.DatasetInfo(
|
58 |
+
description=_DESCRIPTION,
|
59 |
+
features=datasets.Features(
|
60 |
+
{
|
61 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
62 |
+
"tags": datasets.Sequence(datasets.features.ClassLabel(names=names)),
|
63 |
+
}
|
64 |
+
),
|
65 |
+
supervised_keys=None,
|
66 |
+
)
|