Datasets:

Modalities:
Video
Languages:
Chinese
Libraries:
Datasets
License:
AIMClab commited on
Commit
2e28d48
·
1 Parent(s): 9331243

Upload ChinaOpen.py

Browse files
Files changed (1) hide show
  1. ChinaOpen.py +101 -0
ChinaOpen.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import json
3
+
4
+ import datasets
5
+ from datasets.tasks import QuestionAnsweringExtractive
6
+
7
+
8
+ logger = datasets.logging.get_logger(__name__)
9
+
10
+
11
+ #_URL = "./ChinaOpen-1k.zip"
12
+ _URLS = {
13
+ # "train": _URL + "train-v1.1.json",
14
+ "./ChinaOpen-1k.zip"
15
+ }
16
+
17
+
18
+ class ChinaOpenConfig(datasets.BuilderConfig):
19
+ """BuilderConfig for SQUAD."""
20
+
21
+ def __init__(self, **kwargs):
22
+ """BuilderConfig for SQUAD.
23
+ Args:
24
+ **kwargs: keyword arguments forwarded to super.
25
+ """
26
+ super(ChinaOpenConfig, self).__init__(**kwargs)
27
+
28
+
29
+ class ChinaOpen(datasets.GeneratorBasedBuilder):
30
+ """SQUAD: The Stanford Question Answering Dataset. Version 1.1."""
31
+
32
+ BUILDER_CONFIGS = [
33
+ ChinaOpenConfig(
34
+ name="plain_text",
35
+ version=datasets.Version("1.0.0", ""),
36
+ description="Plain text",
37
+ ),
38
+ ]
39
+
40
+ def _info(self):
41
+ return datasets.DatasetInfo(
42
+ description=_DESCRIPTION,
43
+ features=datasets.Features(
44
+ {
45
+ "id": datasets.Value("string"),
46
+ "title": datasets.Value("string"),
47
+ "context": datasets.Value("string"),
48
+ "question": datasets.Value("string"),
49
+ "answers": datasets.features.Sequence(
50
+ {
51
+ "text": datasets.Value("string"),
52
+ "answer_start": datasets.Value("int32"),
53
+ }
54
+ ),
55
+ }
56
+ ),
57
+ # No default supervised_keys (as we have to pass both question
58
+ # and context as input).
59
+ supervised_keys=None,
60
+ citation=_CITATION,
61
+ task_templates=[
62
+ QuestionAnsweringExtractive(
63
+ question_column="question", context_column="context", answers_column="answers"
64
+ )
65
+ ],
66
+ )
67
+
68
+ def _split_generators(self, dl_manager):
69
+ downloaded_files = dl_manager.download_and_extract(_URLS)
70
+
71
+ return [
72
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
73
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
74
+ ]
75
+
76
+ def _generate_examples(self, filepath):
77
+ """This function returns the examples in the raw (text) form."""
78
+ logger.info("generating examples from = %s", filepath)
79
+ key = 0
80
+ with open(filepath, encoding="utf-8") as f:
81
+ squad = json.load(f)
82
+ for article in squad["data"]:
83
+ title = article.get("title", "")
84
+ for paragraph in article["paragraphs"]:
85
+ context = paragraph["context"] # do not strip leading blank spaces GH-2585
86
+ for qa in paragraph["qas"]:
87
+ answer_starts = [answer["answer_start"] for answer in qa["answers"]]
88
+ answers = [answer["text"] for answer in qa["answers"]]
89
+ # Features currently used are "context", "question", and "answers".
90
+ # Others are extracted here for the ease of future expansions.
91
+ yield key, {
92
+ "title": title,
93
+ "context": context,
94
+ "question": qa["question"],
95
+ "id": qa["id"],
96
+ "answers": {
97
+ "answer_start": answer_starts,
98
+ "text": answers,
99
+ },
100
+ }
101
+ key += 1