Datasets:
Tasks:
Text Classification
Languages:
Persian
import csv | |
import datasets | |
from datasets.tasks import TextClassification | |
logger = datasets.logging.get_logger(__name__) | |
_CITATION = """Citation""" | |
_DESCRIPTION = """Description""" | |
_DOWNLOAD_URLS = { | |
"utc_train_text": "https://huggingface.co/datasets/mahdiyehebrahimi/utc/raw/main/utc_train_text.csv", | |
"utc_test_text": "https://huggingface.co/datasets/mahdiyehebrahimi/utc/raw/main/utc_test_text.csv", | |
"utc_train_ner": "https://huggingface.co/datasets/mahdiyehebrahimi/utc/raw/main/utc_train_ner.csv", | |
"utc_test_ner": "https://huggingface.co/datasets/mahdiyehebrahimi/utc/raw/main/utc_test_ner.csv", | |
"utc_train_concate": "https://huggingface.co/datasets/mahdiyehebrahimi/utc/raw/main/utc_train_concate.csv", | |
"utc_test_concate": "https://huggingface.co/datasets/mahdiyehebrahimi/utc/raw/main/utc_test_concate.csv", | |
} | |
class DatasetNameConfig(datasets.BuilderConfig): | |
def __init__(self, **kwargs): | |
super(DatasetNameConfig, self).__init__(**kwargs) | |
class DatasetName(datasets.GeneratorBasedBuilder): | |
BUILDER_CONFIGS = [ | |
DatasetNameConfig( | |
name="utc", | |
version=datasets.Version("1.1.1"), | |
description=_DESCRIPTION, | |
), | |
] | |
def _info(self): | |
text_column = "text" | |
label_column = "label" | |
# TODO PROVIDE THE LABELS HERE | |
label_names = ['UndergraduateRegistrationExceptions', | |
'CentralAuthentication&Email', | |
'Senior(Registration,Deletion,Leave)', | |
'Senior(Professor,Seminar,Proposal,Defense)', | |
'Admissionwithoutatest', 'Calculateandchargetheinternet', | |
'OfficeAutomation', 'Ph.D.(Admission,Registration,Removal,Leave)', | |
'Ph.D.(Comprehensive,Research1and2,Opportunity)', 'Yekta|Nikan'] | |
return datasets.DatasetInfo( | |
description=_DESCRIPTION, | |
features=datasets.Features( | |
{text_column: datasets.Value("string"), label_column: datasets.features.ClassLabel(names=label_names)} | |
), | |
homepage="https://huggingface.co/datasets/mahdiyehebrahimi/utc", | |
citation=_CITATION, | |
task_templates=[TextClassification(text_column=text_column, label_column=label_column)], | |
) | |
def _split_generators(self, dl_manager): | |
""" | |
Return SplitGenerators. | |
""" | |
utc_train_text_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["utc_train_text"]) | |
utc_test_text_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["utc_test_text"]) | |
utc_train_ner_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["utc_train_ner"]) | |
utc_test_ner_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["utc_test_ner"]) | |
utc_train_concate_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["utc_train_concate"]) | |
utc_test_concate_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["utc_test_concate"]) | |
return [ | |
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": utc_train_text_path}), | |
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": utc_test_text_path}), | |
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": utc_train_ner_path}), | |
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": utc_test_ner_path}), | |
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": utc_train_concate_path}), | |
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": utc_test_concate_path}), | |
] | |
# TODO | |
def _generate_examples(self, filepath): | |
""" | |
Per each file_path read the csv file and iterate it. | |
For each row yield a tuple of (id, {"text": ..., "label": ..., ...}) | |
Each call to this method yields an output like below: | |
``` | |
(123, {"text": "I liked it", "label": "positive"}) | |
``` | |
""" | |
label2id = self.info.features[self.info.task_templates[0].label_column].str2int | |
logger.info("⏳ Generating examples from = %s", filepath) | |
with open(filepath, encoding="utf-8") as csv_file: | |
csv_reader = csv.reader(csv_file, quotechar='"', skipinitialspace=True) | |
# Uncomment below line to skip the first row if your csv file has a header row | |
next(csv_reader, None) | |
for id_, row in enumerate(csv_reader): | |
text, label = row | |
label = label2id(label) | |
# Optional preprocessing here | |
yield id_, {"text": text, "label": label} |