PiC commited on
Commit
f7d11f8
·
1 Parent(s): b8d0108

Create phrase_similarity.py

Browse files
Files changed (1) hide show
  1. phrase_similarity.py +120 -0
phrase_similarity.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
25
+
26
+ logger = datasets.logging.get_logger(__name__)
27
+
28
+
29
+ _CITATION = """\
30
+
31
+ """
32
+
33
+ _DESCRIPTION = """\
34
+
35
+ """
36
+
37
+ _HOMEPAGE = ""
38
+
39
+ _LICENSE = "CC-BY-4.0"
40
+
41
+ _URL = "https://auburn.edu/~tmp0038/PiC/"
42
+ _SPLITS = {
43
+ "train": "train-v1.0.json",
44
+ "dev": "dev-v1.0.json",
45
+ "test": "test-v1.0.json",
46
+ }
47
+
48
+ _PS = "PS"
49
+
50
+
51
+ class PSConfig(datasets.BuilderConfig):
52
+ """BuilderConfig for Phrase Similarity in PiC."""
53
+
54
+ def __init__(self, **kwargs):
55
+ """BuilderConfig for Phrase Similarity in PiC.
56
+ Args:
57
+ **kwargs: keyword arguments forwarded to super.
58
+ """
59
+ super(PSConfig, self).__init__(**kwargs)
60
+
61
+
62
+ class PhraseSimilarity(datasets.GeneratorBasedBuilder):
63
+ """Phrase Similarity in PiC dataset. Version 1.0."""
64
+
65
+ BUILDER_CONFIGS = [
66
+ PSConfig(
67
+ name=_PS,
68
+ version=datasets.Version("1.0.0"),
69
+ description="The PiC Dataset for Phrase Similarity"
70
+ )
71
+ ]
72
+
73
+ def _info(self):
74
+ return datasets.DatasetInfo(
75
+ description=_DESCRIPTION,
76
+ features=datasets.Features(
77
+ {
78
+ "phrase1": datasets.Value("string"),
79
+ "phrase2": datasets.Value("string"),
80
+ "sentence1": datasets.Value("string"),
81
+ "sentence2": datasets.Value("string"),
82
+ "label": datasets.ClassLabel(num_classes=2, names=["negative", "positive"])
83
+ }
84
+ ),
85
+ # No default supervised_keys (as we have to pass both question and context as input).
86
+ supervised_keys=None,
87
+ homepage=_HOMEPAGE,
88
+ license=_LICENSE,
89
+ citation=_CITATION,
90
+ )
91
+
92
+ def _split_generators(self, dl_manager):
93
+ urls_to_download = {
94
+ "train": os.path.join(_URL, self.config.name, _SPLITS["train"]),
95
+ "dev": os.path.join(_URL, self.config.name, _SPLITS["dev"]),
96
+ "test": os.path.join(_URL, self.config.name, _SPLITS["test"])
97
+ }
98
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
99
+
100
+ return [
101
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
102
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
103
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
104
+ ]
105
+
106
+ def _generate_examples(self, filepath):
107
+ """This function returns the examples in the raw (text) form."""
108
+ logger.info("generating examples from = %s", filepath)
109
+ key = 0
110
+ with open(filepath, encoding="utf-8") as f:
111
+ pic_ps = json.load(f)
112
+ for example in pic_ps["data"]:
113
+ yield key, {
114
+ "phrase1": example["phrase1"],
115
+ "phrase2": example["phrase2"],
116
+ "sentence1": example["sentence1"],
117
+ "sentence2": example["sentence2"],
118
+ "label": example["label"]
119
+ }
120
+ key += 1