holylovenia commited on
Commit
9501e26
1 Parent(s): afdf9b8

Upload facqa.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. facqa.py +155 -0
facqa.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+ from pathlib import Path
3
+ from typing import Dict, List, Tuple
4
+
5
+ import datasets
6
+ import pandas as pd
7
+
8
+ from nusacrowd.nusa_datasets.facqa.utils.facqa_utils import (getAnswerString, listToString)
9
+ from nusacrowd.utils import schemas
10
+ from nusacrowd.utils.configs import NusantaraConfig
11
+ from nusacrowd.utils.constants import Tasks
12
+
13
+ _CITATION = """
14
+ @inproceedings{purwarianti2007machine,
15
+ title={A Machine Learning Approach for Indonesian Question Answering System},
16
+ author={Ayu Purwarianti, Masatoshi Tsuchiya, and Seiichi Nakagawa},
17
+ booktitle={Proceedings of Artificial Intelligence and Applications },
18
+ pages={573--578},
19
+ year={2007}
20
+ }
21
+ """
22
+
23
+ _LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
24
+ _LOCAL = False
25
+
26
+ _DATASETNAME = "facqa"
27
+
28
+ _DESCRIPTION = """
29
+ FacQA: The goal of the FacQA dataset is to find the answer to a question from a provided short passage from a news article.
30
+ Each row in the FacQA dataset consists of a question, a short passage, and a label phrase, which can be found inside the
31
+ corresponding short passage. There are six categories of questions: date, location, name,
32
+ organization, person, and quantitative.
33
+ """
34
+
35
+ _HOMEPAGE = "https://github.com/IndoNLP/indonlu"
36
+
37
+ _LICENSE = "CC-BY-SA 4.0"
38
+
39
+ _URLS = {
40
+ _DATASETNAME: {
41
+ "test": "https://raw.githubusercontent.com/IndoNLP/indonlu/master/dataset/facqa_qa-factoid-itb/test_preprocess.csv",
42
+ "train": "https://raw.githubusercontent.com/IndoNLP/indonlu/master/dataset/facqa_qa-factoid-itb/train_preprocess.csv",
43
+ "validation": "https://raw.githubusercontent.com/IndoNLP/indonlu/master/dataset/facqa_qa-factoid-itb/valid_preprocess.csv",
44
+ }
45
+ }
46
+
47
+ _SUPPORTED_TASKS = [Tasks.QUESTION_ANSWERING]
48
+
49
+ _SOURCE_VERSION = "1.0.0"
50
+
51
+ _NUSANTARA_VERSION = "1.0.0"
52
+
53
+
54
+ class FacqaDataset(datasets.GeneratorBasedBuilder):
55
+ """FacQA dataset is a labeled dataset for indonesian question answering task"""
56
+
57
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
58
+ NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
59
+
60
+ BUILDER_CONFIGS = [
61
+ NusantaraConfig(
62
+ name="facqa_source",
63
+ version=SOURCE_VERSION,
64
+ description="FacQA source schema",
65
+ schema="source",
66
+ subset_id="facqa",
67
+ ),
68
+ NusantaraConfig(
69
+ name="facqa_nusantara_qa",
70
+ version=NUSANTARA_VERSION,
71
+ description="FacQA Nusantara schema",
72
+ schema="nusantara_qa",
73
+ subset_id="facqa",
74
+ ),
75
+ ]
76
+
77
+ DEFAULT_CONFIG_NAME = "facqa_source"
78
+
79
+ def _info(self) -> datasets.DatasetInfo:
80
+ if self.config.schema == "source":
81
+ features = datasets.Features(
82
+ {
83
+ "index": datasets.Value("int64"),
84
+ "question": [datasets.Value("string")],
85
+ "passage": [datasets.Value("string")],
86
+ "seq_label": [datasets.Value("string")],
87
+ }
88
+ )
89
+ elif self.config.schema == "nusantara_qa":
90
+ features = schemas.qa_features
91
+
92
+ return datasets.DatasetInfo(
93
+ description=_DESCRIPTION,
94
+ features=features,
95
+ homepage=_HOMEPAGE,
96
+ license=_LICENSE,
97
+ citation=_CITATION,
98
+ )
99
+
100
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
101
+ """Returns SplitGenerators."""
102
+ urls = _URLS[_DATASETNAME]
103
+ train_csv_path = Path(dl_manager.download_and_extract(urls["train"]))
104
+ validation_csv_path = Path(dl_manager.download_and_extract(urls["validation"]))
105
+ test_csv_path = Path(dl_manager.download_and_extract(urls["test"]))
106
+ data_files = {
107
+ "train": train_csv_path,
108
+ "validation": validation_csv_path,
109
+ "test": test_csv_path,
110
+ }
111
+ return [
112
+ datasets.SplitGenerator(
113
+ name=datasets.Split.TRAIN,
114
+ gen_kwargs={
115
+ "filepath": data_files["train"],
116
+ "split": "train",
117
+ },
118
+ ),
119
+ datasets.SplitGenerator(
120
+ name=datasets.Split.TEST,
121
+ gen_kwargs={
122
+ "filepath": data_files["test"],
123
+ "split": "test",
124
+ },
125
+ ),
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.VALIDATION,
128
+ gen_kwargs={
129
+ "filepath": data_files["validation"],
130
+ "split": "dev",
131
+ },
132
+ ),
133
+ ]
134
+
135
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
136
+ """Yields examples as (key, example) tuples."""
137
+ df = pd.read_csv(filepath, sep=",", header="infer").reset_index()
138
+ if self.config.schema == "source":
139
+ for row in df.itertuples():
140
+ entry = {"index": row.index, "question": ast.literal_eval(row.question), "passage": ast.literal_eval(row.passage), "seq_label": ast.literal_eval(row.seq_label)}
141
+ yield row.index, entry
142
+
143
+ elif self.config.schema == "nusantara_qa":
144
+ for row in df.itertuples():
145
+ entry = {
146
+ "id": str(row.index),
147
+ "question_id": str(row.index),
148
+ "document_id": str(row.index),
149
+ "question": listToString(ast.literal_eval(row.question)),
150
+ "type": "extractive",
151
+ "choices": [],
152
+ "context": listToString(ast.literal_eval(row.passage)),
153
+ "answer": [getAnswerString(ast.literal_eval(row.passage), ast.literal_eval(row.seq_label))],
154
+ }
155
+ yield row.index, entry