|
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 dataset of various news articles scraped from different online news agencies’ websites. The total number of articles is 16,438, spread over eight different classes. |
|
""" |
|
|
|
_DRIVE_URL = "https://drive.google.com/uc?export=download&id=1B6xotfXCcW9xS1mYSBQos7OCg0ratzKC" |
|
|
|
class PersianNewsConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for PersianNews Dataset.""" |
|
def __init__(self, **kwargs): |
|
super(PersianNewsConfig, self).__init__(**kwargs) |
|
|
|
|
|
class PersianNews(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
PersianNewsConfig(name="Persian News", version=datasets.Version("1.0.0"), description="persian classification dataset on online agencie's 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#persian-news", |
|
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, 'persian_news/train.csv')}), |
|
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(extracted_file, 'persian_news/test.csv')}), |
|
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(extracted_file, 'persian_news/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) |
|
|