|
@@ -3,6 +3,7 @@
|
|
|
# github https://github.com/runhey
|
|
# github https://github.com/runhey
|
|
|
import csv
|
|
import csv
|
|
|
import re
|
|
import re
|
|
|
|
|
+import difflib
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
@@ -44,23 +45,39 @@ class Answer:
|
|
|
:param options:
|
|
:param options:
|
|
|
:return:
|
|
:return:
|
|
|
"""
|
|
"""
|
|
|
|
|
+ def decide_by_question_foreach(ques: str, ops: list[str]):
|
|
|
|
|
+ # 这个是最耗时的操作,遍历数据库中的所有题目,找到最相识的题目和选项
|
|
|
|
|
+ for key, values in self.data.items():
|
|
|
|
|
+ question_match_ratio = difflib.SequenceMatcher(None, ques, key).ratio()
|
|
|
|
|
+ if question_match_ratio < 0.7:
|
|
|
|
|
+ continue
|
|
|
|
|
+ for value in values:
|
|
|
|
|
+ for index, option in enumerate(ops):
|
|
|
|
|
+ option_match_ratio = difflib.SequenceMatcher(None, option, value).ratio()
|
|
|
|
|
+ if option_match_ratio >= 0.5:
|
|
|
|
|
+ logger.warning('The worst case: no match found for question and option')
|
|
|
|
|
+ logger.warning('Now traversing the entire database to find the most similar question and option')
|
|
|
|
|
+ logger.warning(f'Most similar question: {key} and most similar option: {value}')
|
|
|
|
|
+ return index + 1
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
def decide_by_options(question: str, ops: list[str]):
|
|
def decide_by_options(question: str, ops: list[str]):
|
|
|
# 瞎猫当死耗子
|
|
# 瞎猫当死耗子
|
|
|
- opts = {}
|
|
|
|
|
|
|
+ matches = {}
|
|
|
for index, option in enumerate(ops):
|
|
for index, option in enumerate(ops):
|
|
|
- for char in option:
|
|
|
|
|
- pass
|
|
|
|
|
- if option in self.data_options.keys():
|
|
|
|
|
- cnts = [count_intersection(question, ques) for ques in self.data_options[option] ]
|
|
|
|
|
- cnts.sort(reverse=True)
|
|
|
|
|
- opts[index + 1] = cnts[0]
|
|
|
|
|
- #
|
|
|
|
|
- if opts:
|
|
|
|
|
- opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
|
- if opts:
|
|
|
|
|
|
|
+ if option not in self.data_options.keys():
|
|
|
|
|
+ continue
|
|
|
|
|
+ for ques in self.data_options[option]:
|
|
|
|
|
+ ques_match_ratio = difflib.SequenceMatcher(None, ques, question).ratio()
|
|
|
|
|
+ if ques_match_ratio > 0.8:
|
|
|
|
|
+ matches[index + 1] = ques_match_ratio
|
|
|
|
|
+ if matches:
|
|
|
|
|
+ opts = sorted(matches.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
|
+ logger.warning('No match found for question,\n Now traversing the entire database to find the most similar question')
|
|
|
|
|
+ logger.warning(f'Most similar answer: {opts[0][0]}, and similarity char number is {opts[0][1]}')
|
|
|
return opts[0][0]
|
|
return opts[0][0]
|
|
|
- else:
|
|
|
|
|
- return None
|
|
|
|
|
|
|
+ index = decide_by_question_foreach(question, ops)
|
|
|
|
|
+ return index if index else None
|
|
|
|
|
|
|
|
|
|
|
|
|
question = question.replace('「', '').replace('」', '').replace('?', '')
|
|
question = question.replace('「', '').replace('」', '').replace('?', '')
|
|
@@ -92,7 +109,22 @@ class Answer:
|
|
|
if len(match) - count_intersection(match, option) <= 1 :
|
|
if len(match) - count_intersection(match, option) <= 1 :
|
|
|
logger.warning('Option is not match: %s, %s', match, option)
|
|
logger.warning('Option is not match: %s, %s', match, option)
|
|
|
return index + 1
|
|
return index + 1
|
|
|
- # 选项一个都对不上可能是,识别的选项异常
|
|
|
|
|
|
|
+ # 最保守策略,答案匹配度最高
|
|
|
|
|
+ opts = {}
|
|
|
|
|
+ for index, option in enumerate(options):
|
|
|
|
|
+ item_match_ratio = 0
|
|
|
|
|
+ for match in question_matches:
|
|
|
|
|
+ match_ratio = difflib.SequenceMatcher(None, match, option).ratio()
|
|
|
|
|
+ if match_ratio >= 0.33 and match_ratio > item_match_ratio:
|
|
|
|
|
+ item_match_ratio = match_ratio
|
|
|
|
|
+ if item_match_ratio > 0:
|
|
|
|
|
+ opts[index + 1] = item_match_ratio
|
|
|
|
|
+ if opts:
|
|
|
|
|
+ opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
|
+ logger.warning(f'Use SequenceMatcher and get answers: {opts[0][0]}, score: {opts[0][1]}')
|
|
|
|
|
+ return opts[0][0]
|
|
|
|
|
+
|
|
|
|
|
+ # 选项一个都对不上可能是,正确答案检测为空
|
|
|
for index, option in enumerate(options):
|
|
for index, option in enumerate(options):
|
|
|
if option == '':
|
|
if option == '':
|
|
|
logger.error('Option is empty: %s', options)
|
|
logger.error('Option is empty: %s', options)
|