-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathquiz_store.py
158 lines (141 loc) · 4.82 KB
/
quiz_store.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
# Copyright (c) 2020 shmilee
import re
import sys
import unicodedata
PAT_MAIN = re.compile(r'''
\s*[((]{1}(?P<answer>(?:[ABCDabcd]{1,4}|[√×]{1}))[))]{1} # answer
\s*(?P<number>\d+)\s*\.\s* # number
(?P<content>.*) # question
\s*[\n]{0,1}
''', re.VERBOSE)
PAT_MAIN = re.compile(r'''
\s*(?P<number>\d+)\s*\.\s* # number
(?P<content>.*[((]{1}\s*(?P<answer>(?:[ABCDabcd]{1,4}|[√×]{1}))\s*[))]{1}.*) # question # answer
\s*[\n]{0,1}
''', re.VERBOSE)
PAT_MAIN = re.compile(r'''
\s*(?P<number>\d+)\s*[\.、]\s* # number
(?P<content>.* # question
[\((]{1}\s*)
(?P<answer>[ABCDabcd]{1,4}) # answer
(?P<content_tail>\s*[))]{1}.*) # question
\s*[\n]{0,1}
''', re.VERBOSE)
PAT_MAIN2 = re.compile(r'''
\s*(?P<number>\d+)\s*[\.、]\s* # number
(?P<content>.* # question
[\((]{1}\s*)
(?P<answer>[对错]{1}) # answer
(,(?P<analysis>.*)){0,1} # answer
(?P<content_tail>\s*[))]{1}.*) # question
\s*[\n]{0,1}
''', re.VERBOSE)
PAT_OPTION = re.compile(r'''
(\s*(?P<opt>[ABCDEabcde]){1}\.\s*
(?P<opt_content>.*)){1,5}
\s*[\n]{0,1}
''', re.VERBOSE)
PAT_OPTION = re.compile(r'''
(\s*[Aa]{1}\s*[\.、.]\s*
(?P<optA>[^BCDE]*)){0,1}
(\s*[Bb]{1}\s*[\.、.]\s*
(?P<optB>[^CDE]*)){0,1}
(\s*[Cc]{1}\s*[\.、.]\s*
(?P<optC>[^DE]*)){0,1}
(\s*[Dd]{1}\s*[\.、.]\s*
(?P<optD>[^E]*)){0,1}
(\s*[Ee]{1}\s*[\.、.]\s* # optional E
(?P<optE>.*)){0,1}
\s*[\n]{0,1}
''', re.VERBOSE)
class Question(object):
def __init__(self, content, answer, **kwargs):
self.content = unicodedata.normalize('NFKC', content).strip()
c_tail = kwargs.get('content_tail', '')
if c_tail:
c_tail = unicodedata.normalize('NFKC', c_tail).strip()
self.content = self.content + ' ' + c_tail
self.answer = answer.strip()
if self.answer in ('√', '×', '对', '错'):
self.qtype = 'TorF'
elif re.match(r'[ABCDabcd]{1,4}', self.answer):
self.qtype = 'Choice'
else:
self.qtype = None
com_attrs, opt_attrs = ['number', 'analysis', 'level'], []
if self.qtype == 'Choice':
opt_attrs = ['optA', 'optB', 'optC', 'optD', 'optE']
for attr in com_attrs+ opt_attrs:
val = kwargs.get(attr, None)
if val is not None:
if attr not in ['number', 'level']:
val = unicodedata.normalize('NFKC', val).strip()
else:
val = val.strip()
setattr(self, attr, val)
def show(self, fmt='cx1'):
if fmt == 'cx1': # chaoxing 智能
num = '1' if self.number is None else self.number
out = '%s、%s\n' % (num, self.content)
if self.qtype == 'Choice':
for attr in ('optA', 'optB', 'optC', 'optD', 'optE'):
opt = getattr(self, attr)
if opt is not None:
out += '%s.%s\n' %(attr[3], opt)
out += '答案:%s\n' % self.answer
if self.analysis is not None:
out += '答案解析:%s\n' % self.analysis
if self.level is not None:
out += '难易度:%s\n' % self.level
print(out)
elif fmt == '':
pass
def get_objs_from_txt(path):
txt = open(path, 'r')
data = txt.readlines()
i = 0
length = len(data)
results = []
while i < length:
m = PAT_MAIN.match(data[i])
if not m:
m = PAT_MAIN2.match(data[i])
if m:
kwargs = m.groupdict()
answer = kwargs.get('answer', None)
if answer in ('√', '×', '对', '错'):
results.append(Question(**kwargs))
elif re.match(r'[ABCDEabcde]{1,5}', answer):
ii = i
for j in range(1,6):
m2 = PAT_OPTION.match(data[ii+j])
opts = {}
for k, v in m2.groupdict().items():
if v is not None:
opts[k] = v
if opts:
i = i + 1
kwargs.update(opts)
else:
#print('WARN: line %d lost options!' % (ii+j))
break
results.append(Question(**kwargs))
i = i + 1
return results
def test_main(path):
results = get_objs_from_txt(path)
for q in results:
q.show()
try:
path = sys.argv[1]
except IndexError as e:
path = './zfy/01test.txt'
path = './zfy/党史知识竞赛题库.txt'
test_main(path)
'''
test=docx.Document('./2020汽车修理类 学测考试专业理论试题 01.docx')
p=test.paragraphs[1]
PAT_MAIN.match(p.text)
PAT_OPTION.match(p.text)
'''