| | import json |
| | import csv |
| | import datasets |
| | import requests |
| | import os |
| |
|
| | _CITATION = """\\ |
| | @article{ParsBERT, |
| | title={ParsBERT: Transformer-based Model for Persian Language Understanding}, |
| | author={Mehrdad Farahani, Mohammad Gharachorloo, Marzieh Farahani, Mohammad Manthouri}, |
| | journal={ArXiv}, |
| | year={2020}, |
| | volume={abs/2005.12515} |
| | } |
| | """ |
| | _DESCRIPTION = """\\\\\\\\ |
| | A total of 8,515 articles scraped from Digikala Online Magazine. This dataset includes seven different classes. |
| | """ |
| |
|
| | _DRIVE_URL = "https://drive.google.com/uc?export=download&id=1YgrCYY-Z0h2z0-PfWVfOGt1Tv0JDI-qz" |
| |
|
| | class DigiMagConfig(datasets.BuilderConfig): |
| | """BuilderConfig for DigiMag.""" |
| | def __init__(self, **kwargs): |
| | super(DigiMagConfig, self).__init__(**kwargs) |
| | |
| | class DigiMag(datasets.GeneratorBasedBuilder): |
| | BUILDER_CONFIGS = [ |
| | DigiMagConfig(name="DigiMag", version=datasets.Version("1.0.0"), description="persian classification dataset on digimag articles"), |
| | ] |
| | def _info(self): |
| | return datasets.DatasetInfo( |
| | |
| | description=_DESCRIPTION, |
| | |
| | features=datasets.Features( |
| | { |
| | "content": datasets.Value("string"), |
| | "label": datasets.Value("string"), |
| | "label_id": datasets.Value(dtype='int64') |
| | } |
| | ), |
| | supervised_keys=None, |
| | |
| | homepage="https://hooshvare.github.io/docs/datasets/tc#digikala-magazine-digimag", |
| | 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.""" |
| | |
| | |
| | |
| | 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, 'digimag/train.csv')}), |
| | datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(extracted_file, 'digimag/test.csv')}), |
| | datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(extracted_file, 'digimag/dev.csv')}), |
| | ] |
| |
|
| | def _generate_examples(self, filepath): |
| | try: |
| | with open(filepath, encoding="utf-8") as f: |
| | reader = csv.DictReader(f, delimiter="\t") |
| | for idx, row in enumerate(reader): |
| | yield idx, { |
| | "content": row["content"], |
| | "label": row["label"], |
| | "label_id": row["label_id"], |
| | } |
| | except Exception as e: |
| | print(e) |
| |
|