File size: 3,806 Bytes
2a100dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""LogiQA: A Challenge Dataset for Machine Reading Comprehension with Logical Reasoning"""

import re
import datasets

logger = datasets.logging.get_logger(__name__)


_HOMEPAGE = "https://github.com/lgw863/LogiQA-dataset"

_DESCRIPTION = """\
LogiQA is constructed from the logical comprehension problems from \
publically available questions of the National Civil Servants Examination \
of China, which is designed to test the civil servant candidates’ critical \
thinking and problem-solving. This dataset includes the Chinese versions only"""

_CITATION = """\
@article{liu2020logiqa,
  title={Logiqa: A challenge dataset for machine reading comprehension with logical reasoning},
  author={Liu, Jian and Cui, Leyang and Liu, Hanmeng and Huang, Dandan and Wang, Yile and Zhang, Yue},
  journal={arXiv preprint arXiv:2007.08124},
  year={2020}
}
"""

_URLS = {
    "zh_train": "https://raw.githubusercontent.com/lgw863/LogiQA-dataset/master/zh_train.txt",
    "zh_test": "https://raw.githubusercontent.com/lgw863/LogiQA-dataset/master/zh_test.txt",
    "zh_eval": "https://raw.githubusercontent.com/lgw863/LogiQA-dataset/master/zh_eval.txt",
}

def _process_answer(answer):
    if not any(answer.startswith(x) for x in "ABCD"):
        return answer
    else:
        return answer[3:]

def _process_sentences(text):
    text = text.replace("\n", "")
    sents = text.split(".")
    text = ""
    for sent in sents:
        if len(sent) == 0:
            continue
        if len(text) == 0:
            text += sent
        elif sent[0].isnumeric():
            text += "."+sent
        else:
            text += ". "+sent
    text = text.replace("  ", " ")
    text = text.replace("\\'", "'")
    while text.endswith(" "):
        text = text[:-1]
    if re.match('^[A-Z][\w\s]+[?.!]$', text) is None:
        text += "."
    text = text.replace("?.", "?")
    text = text.replace("!.", "!")
    text = text.replace("..", ".")
    return text

class LogiQA(datasets.GeneratorBasedBuilder):
    def _info(self):
        features = datasets.Features(
            {
                "context": datasets.Value("string"),
                "query": datasets.Value("string"),
                "options": datasets.features.Sequence(datasets.Value("string")),
                "correct_option": datasets.Value("int32")
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(_URLS)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["zh_train"]}),
            datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["zh_eval"]}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["zh_test"]}),
        ]

    def _generate_examples(self, filepath):
        logger.info("generating examples from = %s", filepath)
        with open(filepath, encoding="utf-8") as f:
            logiqa = f.readlines()
            logiqa = [_process_sentences(s) for s in logiqa]

            for key in range(int(len(logiqa)/8)):
                row = 8*key
                correct_answer = logiqa[row+1].replace(".","")
                context = logiqa[row+2]
                query = logiqa[row+3]
                answers = logiqa[row+4:row+8]

                yield key, {
                    "context": context,
                    "query": query,
                    "options": [_process_answer(answers[i]) for i in range(4)],
                    "correct_option": "abcd".index(correct_answer)
                }