Create utcner.py
Browse files
utcner.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
|
| 3 |
+
import datasets
|
| 4 |
+
from datasets.tasks import TextClassification
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
logger = datasets.logging.get_logger(__name__)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
_CITATION = """Citation"""
|
| 11 |
+
_DESCRIPTION = """Description"""
|
| 12 |
+
|
| 13 |
+
_DOWNLOAD_URLS = {
|
| 14 |
+
"train": "https://huggingface.co/datasets/mahdiyehebrahimi/utcner/raw/main/utc_train_ner.csv",
|
| 15 |
+
"test": "https://huggingface.co/datasets/mahdiyehebrahimi/utcner/raw/main/utc_test_ner.csv",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class DatasetNameConfig(datasets.BuilderConfig):
|
| 20 |
+
def __init__(self, **kwargs):
|
| 21 |
+
super(DatasetNameConfig, self).__init__(**kwargs)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class DatasetName(datasets.GeneratorBasedBuilder):
|
| 25 |
+
BUILDER_CONFIGS = [
|
| 26 |
+
DatasetNameConfig(
|
| 27 |
+
name="utcner",
|
| 28 |
+
version=datasets.Version("1.1.1"),
|
| 29 |
+
description=_DESCRIPTION,
|
| 30 |
+
),
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
def _info(self):
|
| 34 |
+
text_column = "text"
|
| 35 |
+
label_column = "label"
|
| 36 |
+
# TODO PROVIDE THE LABELS HERE
|
| 37 |
+
label_names = ['UndergraduateRegistrationExceptions',
|
| 38 |
+
'CentralAuthentication&Email',
|
| 39 |
+
'Senior(Registration,Deletion,Leave)',
|
| 40 |
+
'Senior(Professor,Seminar,Proposal,Defense)',
|
| 41 |
+
'Admissionwithoutatest', 'Calculateandchargetheinternet',
|
| 42 |
+
'OfficeAutomation', 'Ph.D.(Admission,Registration,Removal,Leave)',
|
| 43 |
+
'Ph.D.(Comprehensive,Research1and2,Opportunity)', 'Yekta|Nikan']
|
| 44 |
+
return datasets.DatasetInfo(
|
| 45 |
+
description=_DESCRIPTION,
|
| 46 |
+
features=datasets.Features(
|
| 47 |
+
{text_column: datasets.Value("string"), label_column: datasets.features.ClassLabel(names=label_names)}
|
| 48 |
+
),
|
| 49 |
+
homepage="https://huggingface.co/datasets/mahdiyehebrahimi/utcner",
|
| 50 |
+
citation=_CITATION,
|
| 51 |
+
task_templates=[TextClassification(text_column=text_column, label_column=label_column)],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def _split_generators(self, dl_manager):
|
| 55 |
+
"""
|
| 56 |
+
Return SplitGenerators.
|
| 57 |
+
"""
|
| 58 |
+
train_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["train"])
|
| 59 |
+
test_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["test"])
|
| 60 |
+
|
| 61 |
+
return [
|
| 62 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}),
|
| 63 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}),
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# TODO
|
| 67 |
+
def _generate_examples(self, filepath):
|
| 68 |
+
"""
|
| 69 |
+
Per each file_path read the csv file and iterate it.
|
| 70 |
+
For each row yield a tuple of (id, {"text": ..., "label": ..., ...})
|
| 71 |
+
Each call to this method yields an output like below:
|
| 72 |
+
```
|
| 73 |
+
(123, {"text": "I liked it", "label": "positive"})
|
| 74 |
+
```
|
| 75 |
+
"""
|
| 76 |
+
label2id = self.info.features[self.info.task_templates[0].label_column].str2int
|
| 77 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
| 78 |
+
with open(filepath, encoding="utf-8") as csv_file:
|
| 79 |
+
csv_reader = csv.reader(csv_file, quotechar='"', skipinitialspace=True)
|
| 80 |
+
|
| 81 |
+
# Uncomment below line to skip the first row if your csv file has a header row
|
| 82 |
+
next(csv_reader, None)
|
| 83 |
+
|
| 84 |
+
for id_, row in enumerate(csv_reader):
|
| 85 |
+
text, label = row
|
| 86 |
+
label = label2id(label)
|
| 87 |
+
# Optional preprocessing here
|
| 88 |
+
yield id_, {"text": text, "label": label}
|