-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.py
202 lines (176 loc) · 7.24 KB
/
analyzer.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import re
import json
import os
import time
def trans():
l = os.listdir('html')
for v in l:
fin = open(os.path.join('html', v), 'r', encoding = 'gbk')
fout = open(os.path.join('examP', v), 'w', encoding = 'utf8')
html = fin.read()
alltext = re.findall('<div class="content-txt">(.*?)<div class="clear-both"></div>', html, re.S)
try:
print('<div class="content-txt">\n%s\n</div>' % alltext[0], file = fout)
except:
print('err')
def deletesomething():
l = os.listdir('examP')
for v in l:
fin = open(os.path.join('examP', v), 'r')
html = fin.read()
fout = open(os.path.join('tmp', v), 'w')
'''
html = re.sub('<div class="describe"><i></i>.*?</div>', '', html)
html = re.sub('<p style="text-indent:2em;"><font color="#0000FF">司法考试频道为大家推出【<a href="http://union.chinaacc.com/union/advertHit/advertHit.shtm\?advertID=508&agentID=68&toUrl=http://www.chinalawedu.com/project/2014sfks\.shtml#bc" target="_blank">2017年司法考试一次课程!</a>】考生可点击以下入口进入免费试听页面!足不出户就可以边听课边学习,为大家的取证梦想助力!</font></p><br /><p style="text-indent:2em;"><a href="http://union.chinaacc.com/union/advertHit/advertHit.shtm\?advertID=2256&agentID=68&toUrl=http://m.chinalawedu.com/project/2014sfks.shtml" target="_blank"><strong><font style="FONT-SIZE: 18px" color="#ff0000"><font color="#0000ff">【手机用户】→</font>点击进入免费试听>></font></strong></a></p><br /><p style="text-indent:2em;"><a href="http://union.chinaacc.com/union/advertHit/advertHit.shtm\?advertID=2256&agentID=68&toUrl=http://www.chinalawedu.com/project/2014sfks.shtml" target="_blank"><font style="FONT-SIZE: 18px"><strong><font color="#0000ff">【电脑用户】→</font><font color="red">点击进入免费试听>></font></strong></font></a></p><br /><a href="http://union.chinaacc.com/union/advertHit/advertHit.shtm\?advertID=1659&agentID=68" target="_blank"><img name="AdsHttp" src="http://img.cdeledu.com/ADVC/2016/0902/1472779573036-0.jpg" width="500" height="189" alt="" border="0" /></a>', '', html)
html = re.sub('<div class="content-txt">', '', html)
html = re.sub('</div>', '', html)
# html = re.sub('<p>', '', html)
# html = re.sub('</p>', '', html)
html = re.sub('【导语】.*?<.*?>', '', html)
html = re.sub('导语.*<.*?>', '', html)
html = html.replace(r'\n', '')
html = html.replace('\u3000', '')
'''
html = re.sub('[一二三][、.](.*?)<.*?>', '', html)
print(html, file = fout)
def init():
answerSign = re.compile(r'[\[【]?(?:参考)?(?:正确)?答案[】\]]?')
answerDraw = re.compile(r'([\[【]?(?:参考)?(?:正确)?答案[】\]]?.*?)\n')
# repre0 = re.compile(r'>(\d+[\.、].+?)<.*?>(A.*?)<.*?>(B.*?)<.*?>(C.*?)<.*?>(D.*?)<.*?>([\[【]?(?:参考)?(?:正确)?答案[】\]]?.*?)<')
repre0 = re.compile(r'>(\d+[\.、].+?)<(?:br/|p)>(A.*?)<(?:br/|p)>(B.*?)<(?:br/|p)>(C.*?)<(?:br/|p)>(D.*?)<(?:br/|p)>([\[【]?(?:参考)?(?:正确)?答案[】\]]?.*?)<')
# repre1 = re.compile(r'>(\d+[\.、].+?)<.*?>(A.*?)<.*?>(B.*?)<.*?>(C.*?)<.*?>(D.*?)<.*?>')
repre1 = re.compile(r'>(\d+[\.、].+?)<(?:br/|p)>(A.*?)<(?:br|p)>(B.*?)<(?:br|p)>(C.*?)<(?:br|p)>(D.*?)<(?:br|p)>')
return answerSign, answerDraw, repre0, repre1
# answerSign, answerDraw, repre0, repre1 = init()
def draw_Test(alltext):
a = len(answerSign.findall(alltext))
if a > 1:
return None
# drawout = repre0.findall(alltext)
# return drawout
elif a == 1:
return None
drawout = repre1.findall(alltext)
drawout.append(answerDraw.findall(alltext))
# print(drawout)
return drawout
elif a == 0:
# print('this file gg')
pass
def draw2():
l = os.listdir('examP')
b = re.compile('答案')
a = re.compile('>第?\d+[.、题]?(.+?)<(?:br/|p)>.*?(A.+?)<(?:br/|p)>.*?(B.+?)<(?:br/|p)>.*?(C.+?)<(?:br/|p)>.*?(D.+?)<(?:br/|p)>.*?(【?(?:参考|正确)?答案】?.*?)<(?:br/|p)>')
fout = open('drawout2.json', 'w')
for v in l:
print(v)
fin = open(os.path.join('examP', v), 'r')
html = fin.read()
if len(b.findall(html)) > 1:
os.system('mv examP/%s done/%s' % (v, v))
# print(json.dumps(a.findall(html), ensure_ascii = False), file = fout)
# draw2()
def draw3():
l = os.listdir('examP')
a = re.compile('>第?(\d+)[.、题]?(.+?)<(?:br/|p)>.*?(A.+?)<(?:br/|p)>.*?(B.+?)<(?:br/|p)>.*?(C.+?)<(?:br/|p)>.*?(D.+?)<(?:br/|p)>')
ans = re.compile('(\d+)[.、]?([ABCD]+)')
fout = open('drawout3.json', 'w')
for v in l:
print(v)
fin = open(os.path.join('examP', v), 'r')
html = fin.read()
text = a.findall(html)
answer = ans.findall(html)
tmpdic = {}
for v in text:
tmpdic[v[0]] = list(v[1:])
for v in answer:
try:
tmpdic[v[0]].append(v[1])
except:
pass
# print(tmpdic)
print(json.dumps([tmpdic[key] for key in tmpdic], ensure_ascii = False), file = fout)
# print(answer)
# print(text)
# draw3()
def formalize_1():
op = re.compile('[ABCD]')
fin = open('drawout3.json', 'r')
fout = open('2.json', 'w')
lines = fin.readlines()
for line in lines:
d = json.loads(line)
if len(d) == 1:
print(d)
else:
for v in d:
tmp = {}
tmp['statement'] = re.sub(r'\d[.、]?', '', v[0])
tmp['option_list'] = {}
if v[1][1] == '.' or v[1][1] == '、':
tmp['option_list']['A'] = v[1][2:]
else:
tmp['option_list']['A'] = v[1][1:]
if v[2][1] == '.' or v[2][1] == '、':
tmp['option_list']['B'] = v[2][2:]
else:
tmp['option_list']['B'] = v[2][1:]
if v[3][1] == '.' or v[3][1] == '、':
tmp['option_list']['C'] = v[3][2:]
else:
tmp['option_list']['C'] = v[3][1:]
if v[4][1] == '.' or v[4][1] == '、':
tmp['option_list']['D'] = v[4][2:]
else:
tmp['option_list']['D'] = v[4][1:]
try:
tmp['answer'] = op.findall(v[5])
print(json.dumps(tmp, ensure_ascii = False), file = fout)
except:
pass
formalize_1()
'''
if __name__ == '__main__':
l = os.listdir('examP')
# fout = open('drawout.json', 'w')
number = 0
for v in l:
print(v)
fin = open(os.path.join('examP', v), 'r')
html = fin.read()
if '卷四' in v or '卷4' in v or '2017年国家司法考试《卷一》模拟试题及答案' in v:
pass
elif '题及答案' in v:
tmp = draw_Test(html)
# if tmp:
# os.system('mv examP/%s done/%s' % (v, v))
if not tmp is None and len(tmp) > 0:
number += len(tmp)
print(v)
print(json.dumps(tmp, ensure_ascii = False), file = fout)
# print(number)
'''
'''
l = os.listdir('examP')
a = re.compile('答案')
for v in l:
fin = open(os.path.join('examP', v), 'r')
html = fin.read()
fin.close()
if len(a.findall(html)) == 0:
os.system('mv examP/%s withoutanswer/%s' % (v, v))
# if '卷四' in v or '卷4' in v:
# os.system('mv examP/%s exam4/%s' % (v, v))
'''
# analyzer()
'''
2017年司法考试(卷三)模拟选择试题及答案.html
['【参考答案】']
2017年司法考试《中国法制史》单选试题及答案.html
['【参考答案】']
2017年司法考试《中国法制史》模拟练习题及答案.html
['【参考答案】']
2017年司法考试《中国法制史》模拟试题及答案.html
['【参考答案】']
'''