| |
| """QC question classification dataset.""" |
|
|
|
|
| import csv |
|
|
| import datasets |
| from datasets.tasks import TextClassification |
|
|
|
|
| _DESCRIPTION = """\ |
| This data collection contains all the data used in our learning question classification experiments(see [1]), which has question class definitions, the training and testing question sets, examples of preprocessing the questions, feature definition scripts and examples of semantically related word features. |
| This work has been done by Xin Li and Dan Roth and supported by [2]. |
| """ |
|
|
| _CITATION = """""" |
|
|
| _TRAIN_DOWNLOAD_URL = "https://huggingface.co/datasets/vmalperovich/QC/raw/main/train.csv" |
| _TEST_DOWNLOAD_URL = "https://huggingface.co/datasets/vmalperovich/QC/raw/main/test.csv" |
|
|
|
|
| CATEGORY_MAPPING = {'ENTY_cremat': 0, |
| 'DESC_manner': 1, |
| 'ENTY_animal': 2, |
| 'ABBR_exp': 3, |
| 'HUM_ind': 4, |
| 'HUM_gr': 5, |
| 'HUM_title': 6, |
| 'DESC_def': 7, |
| 'NUM_date': 8, |
| 'DESC_reason': 9, |
| 'ENTY_event': 10, |
| 'LOC_state': 11, |
| 'DESC_desc': 12, |
| 'NUM_count': 13, |
| 'ENTY_other': 14, |
| 'ENTY_letter': 15, |
| 'LOC_other': 16, |
| 'ENTY_religion': 17, |
| 'ENTY_food': 18, |
| 'LOC_country': 19, |
| 'ENTY_color': 20, |
| 'ENTY_termeq': 21, |
| 'LOC_city': 22, |
| 'ENTY_body': 23, |
| 'ENTY_dismed': 24, |
| 'LOC_mount': 25, |
| 'NUM_money': 26, |
| 'ENTY_product': 27, |
| 'NUM_period': 28, |
| 'ENTY_substance': 29, |
| 'ENTY_sport': 30, |
| 'ENTY_plant': 31, |
| 'ENTY_techmeth': 32, |
| 'NUM_volsize': 33, |
| 'HUM_desc': 34, |
| 'ENTY_instru': 35, |
| 'ABBR_abb': 36, |
| 'NUM_other': 37, |
| 'NUM_speed': 38, |
| 'ENTY_word': 39, |
| 'ENTY_lang': 40, |
| 'NUM_perc': 41, |
| 'NUM_code': 42, |
| 'NUM_dist': 43, |
| 'NUM_temp': 44, |
| 'ENTY_symbol': 45, |
| 'NUM_ord': 46, |
| 'ENTY_veh': 47, |
| 'NUM_weight': 48, |
| 'ENTY_currency': 49} |
|
|
| class AGNews(datasets.GeneratorBasedBuilder): |
| """AG News topic classification dataset.""" |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=datasets.Features( |
| { |
| "text": datasets.Value("string"), |
| "label": datasets.features.ClassLabel(names=list(CATEGORY_MAPPING.keys())), |
| } |
| ), |
| homepage="https://cogcomp.seas.upenn.edu/Data/QA/QC/", |
| citation=_CITATION, |
| task_templates=[TextClassification(text_column="text", label_column="label")], |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL) |
| test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL) |
| return [ |
| datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}), |
| datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}), |
| ] |
|
|
| def _generate_examples(self, filepath): |
| """Generate QC News examples.""" |
| with open(filepath, encoding="utf-8") as csv_file: |
| csv_reader = csv.reader( |
| csv_file, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True |
| ) |
| _ = next(csv_reader) |
| for id_, row in enumerate(csv_reader): |
| text, label = row |
| label = CATEGORY_MAPPING[label] |
| yield id_, {"text": text, "label": label} |