jupyterjazz commited on
Commit
3f5dd72
1 Parent(s): 296083a

Upload xpqa.py

Browse files
Files changed (1) hide show
  1. xpqa.py +64 -0
xpqa.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ from typing import List
3
+
4
+ import datasets
5
+
6
+ LANGUAGES = ["ar", "de", "es", "fr", "hi", "it", "ja", "ko", "pl", "pt", "ta", "zh"]
7
+ DATA_PATH = "test.csv"
8
+
9
+
10
+ class XPQAConfig(datasets.BuilderConfig):
11
+ def __init__(self, language, **kwargs):
12
+ super().__init__(**kwargs)
13
+ self.language = language
14
+
15
+
16
+ class XPQA(datasets.GeneratorBasedBuilder):
17
+ BUILDER_CONFIG_CLASS = XPQAConfig
18
+
19
+ BUILDER_CONFIGS = [
20
+ XPQAConfig(name=language, language=language) for language in LANGUAGES
21
+ ]
22
+
23
+ def _info(self):
24
+ return datasets.DatasetInfo(
25
+ description="xPQA is a large-scale annotated cross-lingual Product QA dataset.",
26
+ features=datasets.Features(
27
+ {
28
+ "id": datasets.Value("string"),
29
+ "question": datasets.Value("string"),
30
+ "answer": datasets.Value("string"),
31
+ }
32
+ ),
33
+ homepage="https://github.com/amazon-science/contextual-product-qa/tree/main?tab=readme-ov-file#xpqa",
34
+ citation="https://arxiv.org/abs/2305.09249",
35
+ )
36
+
37
+ def _split_generators(
38
+ self, dl_manager: datasets.DownloadManager
39
+ ) -> List[datasets.SplitGenerator]:
40
+ downloaded_file = dl_manager.download_and_extract(DATA_PATH)
41
+ return [
42
+ datasets.SplitGenerator(
43
+ name=datasets.Split.TEST,
44
+ gen_kwargs={"filepath": downloaded_file}
45
+ ),
46
+ ]
47
+
48
+ def _generate_examples(self, filepath):
49
+ id_ = 0
50
+ with open(filepath, newline="") as csvfile:
51
+ csvreader = csv.reader(csvfile, delimiter=",")
52
+ header = next(csvreader)
53
+ lang_pos = header.index("lang")
54
+ answer_pos = header.index("answer")
55
+ question_pos = header.index("question")
56
+ label_pos = header.index("label")
57
+ for row in csvreader:
58
+ if row[lang_pos] == self.config.language and row[label_pos] == "2":
59
+ answer = row[answer_pos]
60
+ question = row[question_pos]
61
+ if not answer or not question:
62
+ continue
63
+ yield id_, {"id": id_, "question": question, "answer": answer}
64
+ id_ += 1