lizc commited on
Commit
54d556c
1 Parent(s): 2ab317b

add script

Browse files
Files changed (1) hide show
  1. feedbackQA.py +128 -0
feedbackQA.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Lint as: python3
17
+ """FeedbackQA: An Retrieval-based Question Answering Dataset with User Feedback"""
18
+
19
+
20
+ import json
21
+
22
+ import datasets
23
+ import os
24
+
25
+ logger = datasets.logging.get_logger(__name__)
26
+
27
+
28
+ _CITATION = """
29
+ """
30
+
31
+ _DESCRIPTION = """\
32
+ FeedbackQA is a retrieval-based QA dataset \
33
+ that contains interactive feedback from users. \
34
+ It has two parts: the first part contains a conventional RQA dataset, \
35
+ whilst this repo contains the second part, which contains feedback(ratings and natural language explanations) for QA pairs.
36
+ """
37
+
38
+ _URL = "https://drive.google.com/drive/folders/1mIcxZZ643k6SVJnZw1FmEOhndaFx4_PG?usp=sharing"
39
+ #_URLS = {
40
+ # "train": _URL + "train-v1.1.json",
41
+ # "dev": _URL + "dev-v1.1.json",
42
+ #}
43
+
44
+
45
+ class FeedbackConfig(datasets.BuilderConfig):
46
+ """BuilderConfig for FeedbackQA."""
47
+
48
+ def __init__(self, **kwargs):
49
+ """BuilderConfig for FeedbackQA.
50
+
51
+ Args:
52
+ **kwargs: keyword arguments forwarded to super.
53
+ """
54
+ super(FeedbackConfig, self).__init__(**kwargs)
55
+
56
+
57
+ class FeedbackQA(datasets.GeneratorBasedBuilder):
58
+ """FeedbackQA: retrieval-based QA dataset that contains interactive feedback from users."""
59
+
60
+ BUILDER_CONFIGS = [
61
+ FeedbackConfig(
62
+ name="plain_text",
63
+ version=datasets.Version("1.0.0", ""),
64
+ description="Plain text",
65
+ ),
66
+ ]
67
+
68
+ def _info(self):
69
+ return datasets.DatasetInfo(
70
+ description=_DESCRIPTION,
71
+ features=datasets.Features(
72
+ {
73
+ #"id": datasets.Value("string"),
74
+ #"title": datasets.Value("string"),
75
+ "question": datasets.Value("string"),
76
+ "answer": datasets.Value("string"),
77
+ "feedback": datasets.features.Sequence(
78
+ {
79
+ "rating": datasets.Value("string"),
80
+ "explanation": datasets.Value("string"),
81
+ }
82
+ ),
83
+ }
84
+ ),
85
+ # No default supervised_keys (as we have to pass both question
86
+ # and context as input).
87
+ supervised_keys=None,
88
+ homepage="https://mcgill-nlp.github.io/feedbackQA_data/",
89
+ citation=_CITATION
90
+ )
91
+
92
+ def _split_generators(self, dl_manager):
93
+ downloaded_files_path = dl_manager.download_and_extract(_URL)
94
+ train_file = os.path.join(downloaded_files_path, 'feedback_train.json')
95
+ valid_file = os.path.join(downloaded_files_path, 'feedback_valid.json')
96
+ test_file = os.path.join(downloaded_files_path, 'feedback_test.json')
97
+
98
+ return [
99
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_file}),
100
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": valid_file}),
101
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_file}),
102
+ ]
103
+
104
+ def _generate_examples(self, filepath):
105
+ """This function returns the examples in the raw (text) form."""
106
+ logger.info("generating examples from = %s", filepath)
107
+ key = 0
108
+ with open(filepath, encoding="utf-8") as f:
109
+ fbqa = json.load(f)
110
+ for dict_item in fbqa:
111
+ question = dict_item['question']
112
+ passage_text = ''
113
+ if dict_item['passage']['reference']['page_title']:
114
+ passage_text += dict_item['passage']['reference']['page_title'] + '\n'
115
+ if dict_item['passage']['reference']['section_headers']:
116
+ passage_text += '\n'.join(dict_item['passage']['reference']['section_headers']) + '\n'
117
+ if dict_item['passage']['reference']['section_content']:
118
+ passage_text += dict_item['passage']['reference']['section_content']
119
+
120
+ yield key, {
121
+ "question": question,
122
+ "answer": passage_text,
123
+ "feedback": {
124
+ "rating": dict_item['rating'],
125
+ "explanation": dict_item['feedback'],
126
+ },
127
+ }
128
+ key += 1