import json import pandas as pd import datasets import requests 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. """ _DRIVE_URL = "https://drive.google.com/uc?export=download&id=1WZxpFRtEs5HZWyWQ2Pyg9CCuIBs1Kmvx" 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( # This is the description that will appear on the datasets page. description=_DESCRIPTION, # datasets.features.FeatureConnectors features=datasets.Features( { "token": datasets.Value("string"), "label": datasets.Value("string") } ), supervised_keys=None, # Homepage of the dataset for documentation homepage="https://hooshvare.github.io/docs/datasets/ner#peyma", citation=_CITATION, ) def custom_dataset(self, src_url, dest_path): response = requests.get(src_url) response.raise_for_status() with open(dest_path, 'wb') as f: f.write(response.content) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # dl_manager is a datasets.download.DownloadManager that can be used to # download and extract URLs downloaded_file = dl_manager.download_custom(_DRIVE_URL, self.custom_dataset) extracted_file = dl_manager.extract(downloaded_file) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(extracted_file, 'peyma/train.txt')}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(extracted_file, 'peyma/test.txt')}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(extracted_file, 'peyma/dev.txt')}), ] def _generate_examples(self, filepath): try: df = pd.read_csv(filepath, error_bad_lines=False, engine='python', sep='|', names=["token", "label"]) for idx, row in enumerate(reader): yield idx, { "token": row["token"], "label": row["label"] } except Exception as e: print(e)