Datasets:

Modalities:
Text
Libraries:
Datasets
kleinay commited on
Commit
5499db7
·
1 Parent(s): 19851dc

Support 'domains, 'load_from' and redistribute dev and test while loading

Browse files
Files changed (1) hide show
  1. qanom.py +34 -5
qanom.py CHANGED
@@ -16,7 +16,7 @@
16
 
17
 
18
  from dataclasses import dataclass
19
- from typing import Optional, Tuple
20
  import datasets
21
  from pathlib import Path
22
  import pandas as pd
@@ -76,13 +76,17 @@ _URLs = {
76
 
77
  SpanFeatureType = datasets.Sequence(datasets.Value("int32"), length=2)
78
 
 
 
79
  @dataclass
80
  class QANomBuilderConfig(datasets.BuilderConfig):
81
  """ Allow the loader to re-distribute the original dev and test splits between train, dev and test. """
82
  redistribute_dev: Tuple[float, float, float] = (0., 1., 0.)
83
  redistribute_test: Tuple[float, float, float] = (0., 0., 1.)
84
  load_from: str = "jsonl" # "csv" or "jsonl"
85
-
 
 
86
 
87
  # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
88
  class Qanom(datasets.GeneratorBasedBuilder):
@@ -93,7 +97,7 @@ class Qanom(datasets.GeneratorBasedBuilder):
93
  are for canidate nominalization which are judged to be non-predicates ("is_verbal"==False) or predicates with no QAs.
94
  In these cases, the qa fields (question, answers, answer_ranges) would be empty lists. """
95
 
96
- VERSION = datasets.Version("1.1.0")
97
 
98
  BUILDER_CONFIG_CLASS = QANomBuilderConfig
99
 
@@ -154,7 +158,22 @@ class Qanom(datasets.GeneratorBasedBuilder):
154
  """Returns SplitGenerators."""
155
 
156
  assert self.config.load_from in ("csv", "jsonl")
157
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  self.corpus_base_path = Path(dl_manager.download_and_extract(_URLs[f"qanom_{self.config.load_from}"]))
159
  if self.config.load_from == "csv":
160
  # prepare wiktionary for verb inflections inside 'self.verb_inflections'
@@ -239,6 +258,11 @@ class Qanom(datasets.GeneratorBasedBuilder):
239
  sent_obj = json.loads(line.strip())
240
  tokens = sent_obj['sentenceTokens']
241
  sentence = ' '.join(tokens)
 
 
 
 
 
242
  for predicate_idx, verb_obj in sent_obj['verbEntries'].items():
243
  verb_forms = verb_obj['verbInflectedForms']
244
  predicate = tokens[int(predicate_idx)]
@@ -268,7 +292,7 @@ class Qanom(datasets.GeneratorBasedBuilder):
268
 
269
  yield qa_counter, {
270
  "sentence": sentence,
271
- "sent_id": sent_obj['sentenceId'],
272
  "predicate_idx": predicate_idx,
273
  "predicate": predicate,
274
  "is_verbal": True,
@@ -313,6 +337,11 @@ class Qanom(datasets.GeneratorBasedBuilder):
313
  for counter, row in df.iterrows():
314
  # Each record (row) in csv is a QA or is stating a predicate/non-predicate with no QAs
315
 
 
 
 
 
 
316
  # Prepare question (slots)
317
  na_to_underscore = lambda s: "_" if pd.isna(s) else str(s)
318
  question = [] if pd.isna(row.question) else list(map(na_to_underscore, [
 
16
 
17
 
18
  from dataclasses import dataclass
19
+ from typing import Optional, Tuple, Union, Iterable, Set
20
  import datasets
21
  from pathlib import Path
22
  import pandas as pd
 
76
 
77
  SpanFeatureType = datasets.Sequence(datasets.Value("int32"), length=2)
78
 
79
+ SUPPOERTED_DOMAINS = {"wikinews", "wikipedia"}
80
+
81
  @dataclass
82
  class QANomBuilderConfig(datasets.BuilderConfig):
83
  """ Allow the loader to re-distribute the original dev and test splits between train, dev and test. """
84
  redistribute_dev: Tuple[float, float, float] = (0., 1., 0.)
85
  redistribute_test: Tuple[float, float, float] = (0., 0., 1.)
86
  load_from: str = "jsonl" # "csv" or "jsonl"
87
+ domains: Union[str, Iterable[str]] = "all" # can provide also a subset of acceptable domains.
88
+ # Acceptable domains are {"wikipedia", "wikinews"} for dev and test (qasrl-2020)
89
+ # and {"wikipedia", "wikinews", "TQA"} for train (qasrl-2018)
90
 
91
  # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
92
  class Qanom(datasets.GeneratorBasedBuilder):
 
97
  are for canidate nominalization which are judged to be non-predicates ("is_verbal"==False) or predicates with no QAs.
98
  In these cases, the qa fields (question, answers, answer_ranges) would be empty lists. """
99
 
100
+ VERSION = datasets.Version("1.2.0")
101
 
102
  BUILDER_CONFIG_CLASS = QANomBuilderConfig
103
 
 
158
  """Returns SplitGenerators."""
159
 
160
  assert self.config.load_from in ("csv", "jsonl")
161
+
162
+ # Handle domain selection
163
+ domains: Set[str] = []
164
+ if self.config.domains == "all":
165
+ domains = SUPPOERTED_DOMAINS
166
+ elif isinstance(self.config.domains, str):
167
+ if self.config.domains in SUPPOERTED_DOMAINS:
168
+ domains = {self.config.domains}
169
+ else:
170
+ raise ValueError(f"Unrecognized domain '{self.config.domains}'; only {SUPPOERTED_DOMAINS} are supported")
171
+ else:
172
+ domains = set(self.config.domains) & SUPPOERTED_DOMAINS
173
+ if len(domains) == 0:
174
+ raise ValueError(f"Unrecognized domains '{self.config.domains}'; only {SUPPOERTED_DOMAINS} are supported")
175
+ self.config.domains = domains
176
+
177
  self.corpus_base_path = Path(dl_manager.download_and_extract(_URLs[f"qanom_{self.config.load_from}"]))
178
  if self.config.load_from == "csv":
179
  # prepare wiktionary for verb inflections inside 'self.verb_inflections'
 
258
  sent_obj = json.loads(line.strip())
259
  tokens = sent_obj['sentenceTokens']
260
  sentence = ' '.join(tokens)
261
+ sent_id = sent_obj['sentenceId']
262
+ # consider only selected domains
263
+ sent_domain = sent_id.split(":")[1]
264
+ if sent_domain not in self.config.domains:
265
+ continue
266
  for predicate_idx, verb_obj in sent_obj['verbEntries'].items():
267
  verb_forms = verb_obj['verbInflectedForms']
268
  predicate = tokens[int(predicate_idx)]
 
292
 
293
  yield qa_counter, {
294
  "sentence": sentence,
295
+ "sent_id": sent_id,
296
  "predicate_idx": predicate_idx,
297
  "predicate": predicate,
298
  "is_verbal": True,
 
337
  for counter, row in df.iterrows():
338
  # Each record (row) in csv is a QA or is stating a predicate/non-predicate with no QAs
339
 
340
+ # consider only selected domains
341
+ sent_domain = row.qasrl_id.split(":")[1]
342
+ if sent_domain not in self.config.domains:
343
+ continue
344
+
345
  # Prepare question (slots)
346
  na_to_underscore = lambda s: "_" if pd.isna(s) else str(s)
347
  question = [] if pd.isna(row.question) else list(map(na_to_underscore, [