forked from steveyg/AnswerHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (48 loc) · 1.9 KB
/
main.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import solve_utils
import problem_utils
import config
import time
import re
from sys import stdout
time_start = time.time()
question, answers = problem_utils.get_result()
print(u"问题 :" + question)
if not answers:
raise ValueError(u'未能识别出答案选项'.encode(stdout.encoding))
# 选项
for i in range(0, len(answers)):
print(u"选项" + str(i + 1) + u" : " + answers[i])
if config.OPEN_BROWSER:
solve_utils.open_webpage(question)
# 判断否定
is_opposite = (question.find(u"不") != -1)
# 验证否定是否合法
if is_opposite:
# 不字前后没有未闭合的双引号或者书名号
if re.match(u'^[^"《]*?((("[^"]+")|(《[^》]+》))[^"《]*)*不([^"》]*(("[^"]+")|(《[^》]+》)))*[^"》]*$',
re.sub(u'“|”', u'"', question)) is None:
is_opposite = False
# 两种方式进行判断
words_count = solve_utils.words_count(question, answers)
search_count = solve_utils.search_count(question, answers)
print(u'%-15s' * 3 % (u'', u'词频', u'结果数'))
for answer, word_count, search_num in zip(answers, words_count, search_count):
print(u'%-15s' * 3 % (answer, word_count, search_num))
# 根据两种方式的结果选出最佳答案
select1 = 0
select2 = 0
if is_opposite:
print(u"注意否定")
select1 = solve_utils.find_min_index(words_count)
select2 = solve_utils.find_min_index(search_count)
else:
select1 = solve_utils.find_max_index(words_count)
select2 = solve_utils.find_max_index(search_count)
if select1 == select2:
print(u"推荐答案为 " + answers[select1])
else:
print(u'推荐答案:%s 参考答案:%s' % ((answers[select2], answers[select1])
if solve_utils.has_repeat(words_count, select1) > 1 else (answers[select1], answers[select2])))
print(u'耗时:%s s' % (time.time() - time_start))