jiacheng-ye commited on
Commit
2a100dd
1 Parent(s): 9adbcc7

Create logiqa.py

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