answer.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import csv
  5. import re
  6. import difflib
  7. from datetime import datetime
  8. from pathlib import Path
  9. from module.logger import logger
  10. def count_intersection(str1, str2):
  11. set1 = set(str1)
  12. set2 = set(str2)
  13. intersection = set1.intersection(set2)
  14. return len(intersection)
  15. def remove_symbols(text):
  16. return re.sub(r'[^\w\s]', '', text)
  17. class Answer:
  18. def __init__(self):
  19. self.data: dict[str, list] = {}
  20. self.data_options: dict[str, list] = {}
  21. # 添加缓存以确保相同问题的答案一致性
  22. # 修改:缓存存储答案字符串而不是索引,避免选项顺序变化的影响
  23. self.question_cache: dict[str, str] = {}
  24. file = str(Path(__file__).parent / 'data.csv')
  25. with open(file, newline='', encoding='utf-8-sig') as csvfile:
  26. reader = csv.reader(csvfile)
  27. next(reader)
  28. for row in reader:
  29. key = remove_symbols(row[0])
  30. value = remove_symbols(row[1])
  31. if key not in self.data:
  32. self.data[key] = []
  33. self.data[key].append(value)
  34. if value not in self.data_options:
  35. self.data_options[value] = []
  36. self.data_options[value].append(key)
  37. def answer_one(self, question: str, options: list[str]) -> int|None:
  38. """
  39. 每一个问题有三个选项, 返回选项的序号(1、2、3)
  40. :param question:
  41. :param options:
  42. :return:
  43. """
  44. # 检查缓存,如果已经处理过类似问题,直接返回结果
  45. # 修改:现在缓存的是答案字符串,需要在选项中查找对应的索引
  46. for cached_q, cached_answer_str in self.question_cache.items():
  47. similarity = difflib.SequenceMatcher(None, question, cached_q).ratio()
  48. if similarity >= 0.9: # 如果问题相似度超过90%,直接返回缓存的答案
  49. # 在当前选项中查找缓存的答案字符串
  50. for idx, option in enumerate(options):
  51. if option == cached_answer_str:
  52. return idx + 1 # 返回选项索引(从1开始)
  53. # 如果缓存的答案在当前选项中不存在,则尝试模糊匹配
  54. for idx, option in enumerate(options):
  55. if difflib.SequenceMatcher(None, option, cached_answer_str).ratio() >= 0.9:
  56. logger.info(f"Fuzzy match cache: '{cached_answer_str}' ~ '{option}'")
  57. return idx + 1
  58. def decide_by_question_foreach(ques: str, ops: list[str]):
  59. # 这个是最耗时的操作,遍历数据库中的所有题目,找到最相识的题目和选项
  60. for key, values in self.data.items():
  61. question_match_ratio = difflib.SequenceMatcher(None, ques, key).ratio()
  62. if question_match_ratio < 0.7:
  63. continue
  64. for value in values:
  65. for index, option in enumerate(ops):
  66. option_match_ratio = difflib.SequenceMatcher(None, option, value).ratio()
  67. if option_match_ratio >= 0.5:
  68. logger.warning('The worst case: no match found for question and option')
  69. logger.warning('Now traversing the entire database to find the most similar question and option')
  70. logger.warning(f'Most similar question: {key} and most similar option: {value}')
  71. result_index = index + 1
  72. # 修改:缓存问题和答案字符串
  73. self.question_cache[ques] = ops[index]
  74. return result_index
  75. return None
  76. def decide_by_options(question: str, ops: list[str]):
  77. # 瞎猫当死耗子
  78. matches = {}
  79. for index, option in enumerate(ops):
  80. if option not in self.data_options.keys():
  81. continue
  82. for ques in self.data_options[option]:
  83. ques_match_ratio = difflib.SequenceMatcher(None, ques, question).ratio()
  84. if ques_match_ratio > 0.8:
  85. matches[index + 1] = ques_match_ratio
  86. if matches:
  87. opts = sorted(matches.items(), key=lambda x: x[1], reverse=True)
  88. logger.warning('No match found for question,\n Now traversing the entire database to find the most similar question')
  89. logger.warning(f'Most similar answer: {opts[0][0]}, and similarity char number is {opts[0][1]}')
  90. result_index = opts[0][0]
  91. # 修改:缓存问题和答案字符串
  92. self.question_cache[question] = ops[result_index - 1]
  93. return result_index
  94. index = decide_by_question_foreach(question, ops)
  95. return index if index else None
  96. question = question.replace('「', '').replace('」', '').replace('?', '')
  97. question = remove_symbols(question)
  98. options = [remove_symbols(option) for option in options]
  99. for index, option in enumerate(options):
  100. if option == '其余选项皆对':
  101. result_index = index + 1
  102. # 特殊选项才缓存
  103. # 修改:缓存问题和答案字符串
  104. self.question_cache[question] = option
  105. return result_index
  106. question_matches: list = []
  107. try:
  108. question_matches = self.data[question]
  109. except Exception as e:
  110. # 没有出现题目,从选项反入手
  111. logger.error('Exception: %s', e)
  112. result = decide_by_options(question=question, ops=options)
  113. if result is not None:
  114. # 只有在使用模糊匹配的情况下才缓存结果
  115. # 修改:缓存问题和答案字符串
  116. self.question_cache[question] = options[result - 1]
  117. return result
  118. # 出现了题目,开始对答案
  119. for index, option in enumerate(options):
  120. for match in question_matches:
  121. if match == option:
  122. return index + 1 # 精确匹配,不缓存,直接返回
  123. # 可能选项识别某一个字错误
  124. if all(options):
  125. for index, option in enumerate(options):
  126. for match in question_matches:
  127. if len(match) != len(option):
  128. continue
  129. if len(match) - count_intersection(match, option) <= 1 :
  130. logger.warning('Option is not match: %s, %s', match, option)
  131. return index + 1 # 这也是精确匹配的一种,不缓存,直接返回
  132. # 最保守策略,答案匹配度最高
  133. opts = {}
  134. for index, option in enumerate(options):
  135. item_match_ratio = 0
  136. for match in question_matches:
  137. match_ratio = difflib.SequenceMatcher(None, match, option).ratio()
  138. if match_ratio >= 0.33 and match_ratio > item_match_ratio:
  139. item_match_ratio = match_ratio
  140. if item_match_ratio > 0:
  141. opts[index + 1] = item_match_ratio
  142. if opts:
  143. opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
  144. logger.warning(f'Use SequenceMatcher and get answers: {opts[0][0]}, score: {opts[0][1]}')
  145. result_index = opts[0][0]
  146. # 只有在这种情况下才缓存
  147. # 修改:缓存问题和答案字符串
  148. self.question_cache[question] = options[result_index - 1]
  149. return result_index
  150. # 选项一个都对不上可能是,正确答案检测为空
  151. for index, option in enumerate(options):
  152. if option == '':
  153. logger.error('Option is empty: %s', options)
  154. result_index = index + 1
  155. # 只有在这种情况下才缓存
  156. # 修改:缓存问题和答案字符串
  157. self.question_cache[question] = option
  158. return result_index
  159. # 如果所有方法都失败,尝试使用decide_by_options
  160. result = decide_by_options(question=question, ops=options)
  161. if result is not None:
  162. # 只有在使用模糊匹配的情况下才缓存结果
  163. # 修改:缓存问题和答案字符串
  164. self.question_cache[question] = options[result - 1]
  165. return result
  166. if __name__ == "__main__":
  167. answer = Answer()
  168. question = '冥界中谁拥阁魔之目一双审善度恶'
  169. options = ['判官', '孟婆', '荒川之主', '阁魔']
  170. start_time = datetime.now()
  171. print(answer.answer_one(question, options))
  172. print(datetime.now() - start_time)