PiC commited on
Commit
cc5ec32
·
1 Parent(s): dd32acd

Create phrase_sense_disambiguation.py

Browse files
Files changed (1) hide show
  1. phrase_sense_disambiguation.py +141 -0
phrase_sense_disambiguation.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """PiC: A Phrase-in-Context Dataset for Phrase Understanding and Semantic Search."""
18
+
19
+
20
+ import json
21
+ import os.path
22
+
23
+ import datasets
24
+ from datasets.tasks import QuestionAnsweringExtractive
25
+
26
+
27
+ logger = datasets.logging.get_logger(__name__)
28
+
29
+
30
+ _CITATION = """\
31
+
32
+ """
33
+
34
+ _DESCRIPTION = """\
35
+
36
+ """
37
+
38
+ _HOMEPAGE = ""
39
+
40
+ _LICENSE = "CC-BY-4.0"
41
+
42
+ _URL = "https://auburn.edu/~tmp0038/PiC/"
43
+ _SPLITS = {
44
+ "train": "train-v1.0.json",
45
+ "dev": "dev-v1.0.json",
46
+ "test": "test-v1.0.json",
47
+ }
48
+
49
+ _PSD = "PSD"
50
+
51
+
52
+ class PSDConfig(datasets.BuilderConfig):
53
+ """BuilderConfig for Phrase Sense Disambiguation in PiC."""
54
+
55
+ def __init__(self, **kwargs):
56
+ """BuilderConfig for Phrase Sense Disambiguation in PiC.
57
+ Args:
58
+ **kwargs: keyword arguments forwarded to super.
59
+ """
60
+ super(PSDConfig, self).__init__(**kwargs)
61
+
62
+
63
+ class PhraseSenseDisambiguation(datasets.GeneratorBasedBuilder):
64
+ """Phrase Sense Disambiguation in PiC dataset. Version 1.0."""
65
+
66
+ BUILDER_CONFIGS = [
67
+ PSDConfig(
68
+ name=_PSD,
69
+ version=datasets.Version("1.0.0"),
70
+ description="The PiC Dataset for Phrase Sense Disambiguation at short passage level (~22 sentences)"
71
+ )
72
+ ]
73
+
74
+ def _info(self):
75
+ return datasets.DatasetInfo(
76
+ description=_DESCRIPTION,
77
+ features=datasets.Features(
78
+ {
79
+ "id": datasets.Value("string"),
80
+ "title": datasets.Value("string"),
81
+ "context": datasets.Value("string"),
82
+ "question": datasets.Value("string"),
83
+ "answers": datasets.Sequence(
84
+ {
85
+ "text": datasets.Value("string"),
86
+ "answer_start": datasets.Value("int32"),
87
+ }
88
+ ),
89
+ }
90
+ ),
91
+ # No default supervised_keys (as we have to pass both question and context as input).
92
+ supervised_keys=None,
93
+ homepage=_HOMEPAGE,
94
+ license=_LICENSE,
95
+ citation=_CITATION,
96
+ task_templates=[
97
+ QuestionAnsweringExtractive(
98
+ question_column="question", context_column="context", answers_column="answers"
99
+ )
100
+ ],
101
+ )
102
+
103
+ def _split_generators(self, dl_manager):
104
+ urls_to_download = {
105
+ "train": os.path.join(_URL, self.config.name, _SPLITS["train"]),
106
+ "dev": os.path.join(_URL, self.config.name, _SPLITS["dev"]),
107
+ "test": os.path.join(_URL, self.config.name, _SPLITS["test"])
108
+ }
109
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
110
+
111
+ return [
112
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
113
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
114
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
115
+ ]
116
+
117
+ def _generate_examples(self, filepath):
118
+ """This function returns the examples in the raw (text) form."""
119
+ logger.info("generating examples from = %s", filepath)
120
+ key = 0
121
+ with open(filepath, encoding="utf-8") as f:
122
+ pic_pr = json.load(f)
123
+ for example in pic_pr["data"]:
124
+ title = example.get("title", "")
125
+
126
+ answer_starts = [answer["answer_start"] for answer in example["answers"]]
127
+ answers = [answer["text"] for answer in example["answers"]]
128
+
129
+ # Features currently used are "context", "question", and "answers".
130
+ # Others are extracted here for the ease of future expansions.
131
+ yield key, {
132
+ "title": title,
133
+ "context": example["context"],
134
+ "question": example["question"],
135
+ "id": example["id"],
136
+ "answers": {
137
+ "answer_start": answer_starts,
138
+ "text": answers,
139
+ },
140
+ }
141
+ key += 1