|
import json |
|
import datasets |
|
import os |
|
|
|
_CITATION = """\\ |
|
@article{shahshahani2018peyma, |
|
title={PEYMA: A Tagged Corpus for Persian Named Entities}, |
|
author={Mahsa Sadat Shahshahani and Mahdi Mohseni and Azadeh Shakery and Heshaam Faili}, |
|
year=2018, |
|
journal={ArXiv}, |
|
volume={abs/1801.09936} |
|
} |
|
""" |
|
_DESCRIPTION = """PEYMA dataset includes 7,145 sentences with a total of 302,530 tokens from which 41,148 tokens are tagged with seven different classes.""" |
|
|
|
_DATA_PATH = { |
|
'train': os.path.join('data', 'train.txt'), |
|
'test': os.path.join('data', 'test.txt'), |
|
'val': os.path.join('data', 'dev.txt') |
|
} |
|
|
|
class PEYMAConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for PEYMA.""" |
|
def __init__(self, **kwargs): |
|
super(PEYMAConfig, self).__init__(**kwargs) |
|
|
|
|
|
class PEYMA(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
PEYMAConfig(name="PEYMA", version=datasets.Version("1.0.0"), description="persian ner dataset"), |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=datasets.Features( |
|
{ |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"tags": datasets.Sequence( |
|
datasets.ClassLabel( |
|
names=[ |
|
"O", |
|
"B_DAT", |
|
"B_LOC", |
|
"B_MON", |
|
"B_ORG", |
|
"B_PCT", |
|
"B_PER", |
|
"B_TIM", |
|
"I_DAT", |
|
"I_LOC", |
|
"I_MON", |
|
"I_ORG", |
|
"I_PCT", |
|
"I_PER", |
|
"I_TIM", |
|
] |
|
) |
|
), |
|
} |
|
), |
|
supervised_keys=('tokens', 'tags'), |
|
|
|
homepage="https://hooshvare.github.io/docs/datasets/ner#peyma", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"filepath": dl_manager.download_and_extract(_DATA_PATH["train"]), |
|
"split": "train", |
|
},), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"filepath": dl_manager.download_and_extract(_DATA_PATH["test"]), |
|
"split": "test"},), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"filepath": dl_manager.download_and_extract(_DATA_PATH["val"]), |
|
"split": "validation", |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath, split): |
|
with open(filepath, "r", encoding="utf-8") as f: |
|
id_ = 0 |
|
tokens = [] |
|
ner_labels = [] |
|
for line in f: |
|
stripped_line = line.strip(" \n") |
|
if len(stripped_line) == 0: |
|
|
|
|
|
if len(tokens) > 0 and len(ner_labels) > 0: |
|
yield id_, { |
|
"tokens": tokens, |
|
"tags": ner_labels, |
|
} |
|
else: |
|
|
|
|
|
continue |
|
|
|
id_ += 1 |
|
tokens = [] |
|
ner_labels = [] |
|
else: |
|
try: |
|
token, ner_label = line.split("|") |
|
tokens.append(token) |
|
ner_labels.append(ner_label) |
|
except: |
|
continue |
|
|
|
|
|
|
|
|