lucasmccabe commited on
Commit
0629b4d
1 Parent(s): 824b3b4

feat: dataloader

Browse files
Files changed (1) hide show
  1. logiqa.py +106 -0
logiqa.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """LogiQA: A Challenge Dataset for Machine Reading Comprehension with Logical Reasoning"""
2
+
3
+ import re
4
+ import datasets
5
+
6
+ logger = datasets.logging.get_logger(__name__)
7
+
8
+
9
+ _HOMEPAGE = "https://github.com/lgw863/LogiQA-dataset"
10
+
11
+ _DESCRIPTION = """\
12
+ LogiQA is constructed from the logical comprehension problems from \
13
+ publically available questions of the National Civil Servants Examination \
14
+ of China, which are designed to test the civil servant candidates’ critical \
15
+ thinking and problem solving. This dataset includes the English versions only; \
16
+ the Chinese versions are available via the homepage/original source."""
17
+
18
+ _CITATION = """\
19
+ @article{liu2020logiqa,
20
+ title={Logiqa: A challenge dataset for machine reading comprehension with logical reasoning},
21
+ author={Liu, Jian and Cui, Leyang and Liu, Hanmeng and Huang, Dandan and Wang, Yile and Zhang, Yue},
22
+ journal={arXiv preprint arXiv:2007.08124},
23
+ year={2020}
24
+ }
25
+ """
26
+
27
+ _URLS = {
28
+ "en_train": "https://raw.githubusercontent.com/lgw863/LogiQA-dataset/master/Train.txt",
29
+ "en_test": "https://raw.githubusercontent.com/lgw863/LogiQA-dataset/master/Test.txt",
30
+ "en_eval": "https://raw.githubusercontent.com/lgw863/LogiQA-dataset/master/Eval.txt",
31
+ }
32
+
33
+ def _process_answer(answer):
34
+ if not any(answer.startswith(x) for x in "ABCD"):
35
+ return answer
36
+ else:
37
+ return answer[3:]
38
+
39
+ def _process_sentences(text):
40
+ text = text.replace("\n", "")
41
+ sents = text.split(".")
42
+ text = ""
43
+ for sent in sents:
44
+ if len(sent) == 0:
45
+ continue
46
+ if len(text) == 0:
47
+ text += sent
48
+ elif sent[0].isnumeric():
49
+ text += "."+sent
50
+ else:
51
+ text += ". "+sent
52
+ text = text.replace(" ", " ")
53
+ text = text.replace("\\'", "'")
54
+ while text.endswith(" "):
55
+ text = text[:-1]
56
+ if re.match('^[A-Z][\w\s]+[?.!]$', text) is None:
57
+ text += "."
58
+ text = text.replace("?.", "?")
59
+ text = text.replace("!.", "!")
60
+ text = text.replace("..", ".")
61
+ return text
62
+
63
+ class LogiQA(datasets.GeneratorBasedBuilder):
64
+ def _info(self):
65
+ features = datasets.Features(
66
+ {
67
+ "context": datasets.Value("string"),
68
+ "query": datasets.Value("string"),
69
+ "options": datasets.features.Sequence(datasets.Value("string")),
70
+ "correct_option": datasets.Value("int32")
71
+ }
72
+ )
73
+ return datasets.DatasetInfo(
74
+ description=_DESCRIPTION,
75
+ features=features,
76
+ homepage=_HOMEPAGE,
77
+ citation=_CITATION,
78
+ )
79
+
80
+ def _split_generators(self, dl_manager):
81
+ downloaded_files = dl_manager.download_and_extract(_URLS)
82
+ return [
83
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["en_train"]}),
84
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["en_eval"]}),
85
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["en_test"]}),
86
+ ]
87
+
88
+ def _generate_examples(self, filepath):
89
+ logger.info("generating examples from = %s", filepath)
90
+ with open(filepath, encoding="utf-8") as f:
91
+ logiqa = f.readlines()
92
+ logiqa = [_process_sentences(s) for s in logiqa]
93
+
94
+ for key in range(int(len(logiqa)/8)):
95
+ row = 8*key
96
+ correct_answer = logiqa[row+1].replace(".","")
97
+ context = logiqa[row+2]
98
+ query = logiqa[row+3]
99
+ answers = logiqa[row+4:row+8]
100
+
101
+ yield key, {
102
+ "context": context,
103
+ "query": query,
104
+ "options": [_process_answer(answers[i]) for i in range(4)],
105
+ "correct_option": "abcd".index(correct_answer)
106
+ }