answer.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 remove_symbols(text):
  11. return re.sub(r'[^\w\s]', '', text)
  12. def _tokenize_question(text: str) -> list[str]:
  13. # 2-gram 题目切词,长度不足时退化为 1-gram
  14. text = text.replace(' ', '')
  15. if len(text) < 2:
  16. return list(text)
  17. return [text[i:i + 2] for i in range(len(text) - 1)]
  18. def _normalize(q: str, opts: list[str]) -> tuple[str, list[str]]:
  19. # 统一清洗 OCR 字符
  20. q = q.replace('「', '').replace('」', '').replace('?', '')
  21. q = remove_symbols(q)
  22. opts = [remove_symbols(opt) for opt in opts]
  23. return q, opts
  24. class Answer:
  25. question_sim_threshold = 0.8
  26. answer_sim_threshold = 0.5
  27. data_file = Path(__file__).parent / 'data.csv'
  28. question_answer: dict[str, set[str]] = {} # 题目: {标准答案
  29. answer_question: dict[str, set[str]] = {} # 标准答案: {题目}
  30. question_index: dict[str, set[str]] = {} # 倒排索引
  31. question_cache: dict[str, set[str]] = {} # 缓存:识别到的问题 -> {标准答案, 选项答案}
  32. def __init__(self, top_n: int = 30):
  33. self.top_n = top_n
  34. with open(self.data_file, newline='', encoding='utf-8') as csvfile:
  35. reader = csv.reader(csvfile)
  36. next(reader)
  37. for row in reader:
  38. key = remove_symbols(row[0])
  39. value = remove_symbols(row[1])
  40. self.question_answer.setdefault(key, set()).add(value)
  41. self.answer_question.setdefault(value, set()).add(key)
  42. for token in _tokenize_question(key):
  43. self.question_index.setdefault(token, set()).add(key)
  44. def answer_one(self, question: str, options: list[str]) -> int | None:
  45. """
  46. 每一个问题有三个选项, 返回选项的序号(1、2、3)
  47. :param question:
  48. :param options:
  49. :return:
  50. """
  51. question, options = _normalize(question, options)
  52. for index, option in enumerate(options):
  53. if option == '其余选项皆对':
  54. result_index = index + 1
  55. # 缓存问题和答案字符串
  56. self._cache_store(question, option, option)
  57. logger.debug('Answer strategy: special option hit.')
  58. return result_index
  59. # 1) 优先命中缓存
  60. cached_index = self._cache_hit(question, options)
  61. if cached_index is not None:
  62. return cached_index
  63. # 2) 题目命中:精确或相似选项匹配
  64. if question in self.question_answer:
  65. return self._handle_known_question(question, options)
  66. # 3) 相似题:先精确,再相似匹配
  67. result = self._handle_similar_questions(question, options)
  68. if result is not None:
  69. return result
  70. # 4) 选项是否存在于答案集合
  71. result = self._handle_answer_option_match(question, options)
  72. if result is not None:
  73. return result
  74. # 5) 完全未知:返回 None
  75. logger.error('Unknown question: %s', question)
  76. return None
  77. def _cache_store(self, question: str, std_answer: str, chosen: str) -> None:
  78. # 更新缓存,便于下一次直接命中
  79. cache = self.question_cache.setdefault(question, set())
  80. if std_answer:
  81. cache.add(std_answer)
  82. if chosen:
  83. cache.add(chosen)
  84. def _find_option_index(self, opts: list[str], target: str) -> int | None:
  85. # 精确查找选项索引
  86. for idx, option in enumerate(opts):
  87. if option == target:
  88. return idx + 1
  89. return None
  90. def _cache_hit(self, question: str, opts: list[str]) -> int | None:
  91. # 优先命中缓存答案
  92. cached = self.question_cache.get(question)
  93. if not cached:
  94. return None
  95. for ans in cached:
  96. idx = self._find_option_index(opts, ans)
  97. if idx is not None:
  98. logger.info('Hit cache.')
  99. return idx
  100. return None
  101. def _best_similarity_option(self, options: list[str], answer: str) -> tuple[int | None, float]:
  102. # 从选项中找与答案最相似的项
  103. best_idx = None
  104. best_score = 0.0
  105. for idx, option in enumerate(options):
  106. score = difflib.SequenceMatcher(None, option, answer).ratio()
  107. if score > best_score:
  108. best_score = score
  109. best_idx = idx + 1
  110. return best_idx, best_score
  111. def _get_similar_questions(self, question: str) -> list[tuple[str, float]]:
  112. # 倒排索引过滤候选问题,再按相似度排序
  113. tokens = _tokenize_question(question)
  114. if not tokens:
  115. return []
  116. candidate_scores: dict[str, int] = {}
  117. for token in tokens:
  118. for key in self.question_index.get(token, []):
  119. candidate_scores[key] = candidate_scores.get(key, 0) + 1
  120. if not candidate_scores:
  121. return []
  122. candidates = sorted(candidate_scores.items(), key=lambda x: x[1], reverse=True)
  123. if self.top_n and len(candidates) > self.top_n:
  124. candidates = candidates[:self.top_n]
  125. scored = []
  126. for key, _ in candidates:
  127. score = difflib.SequenceMatcher(None, question, key).ratio()
  128. if score >= self.question_sim_threshold:
  129. scored.append((key, score))
  130. scored.sort(key=lambda x: x[1], reverse=True)
  131. return scored
  132. def _handle_known_question(self, question: str, options: list[str]) -> int | None:
  133. # 题目命中时,优先精确匹配答案
  134. for answer in self.question_answer[question]:
  135. idx = self._find_option_index(options, answer)
  136. if idx is not None:
  137. logger.info('Hit question, correct answer.')
  138. self._cache_store(question, answer, options[idx - 1])
  139. return idx
  140. # 题目命中但选项不一致:按相似度选最接近答案
  141. best_idx, best_score, best_answer = None, 0.0, ""
  142. for answer in self.question_answer[question]:
  143. idx, score = self._best_similarity_option(options, answer)
  144. if idx is not None and score > best_score:
  145. best_idx, best_score, best_answer = idx, score, answer
  146. if best_idx is not None:
  147. logger.info('Hit question, similar answer.')
  148. self._cache_store(question, best_answer, options[best_idx - 1])
  149. return best_idx
  150. def _handle_similar_questions(self, question: str, options: list[str]) -> int | None:
  151. # 相似题优先:逐题匹配答案是否在选项中
  152. similar_questions = self._get_similar_questions(question)
  153. for key, _ in similar_questions:
  154. for answer in self.question_answer.get(key, []):
  155. idx = self._find_option_index(options, answer)
  156. if idx is not None:
  157. logger.info('Hit similar question, correct answer')
  158. self._cache_store(question, answer, options[idx - 1])
  159. return idx
  160. # 相似题答案不在选项中:做相似度匹配
  161. best_idx, best_score, best_answer = None, 0.0, ""
  162. for key, _ in similar_questions:
  163. for answer in self.question_answer.get(key, []):
  164. idx, score = self._best_similarity_option(options, answer)
  165. if idx is not None and score > best_score:
  166. best_idx, best_score, best_answer = idx, score, answer
  167. if best_idx is not None and best_score >= self.answer_sim_threshold:
  168. logger.info('Hit similar question, similar answer')
  169. self._cache_store(question, best_answer, options[best_idx - 1])
  170. return best_idx
  171. return None
  172. def _handle_answer_option_match(self, question: str, options: list[str]) -> int | None:
  173. # 选项命中答案集合时,根据题目相似度校验
  174. for idx, option in enumerate(options):
  175. if option in self.answer_question:
  176. questions = self.answer_question.get(option, set())
  177. best_question, best_score = None, 0.0
  178. for candidate in questions:
  179. score = difflib.SequenceMatcher(None, question, candidate).ratio()
  180. if score > best_score:
  181. best_score = score
  182. best_question = candidate
  183. # 选项在答案中, 则问题相似度降级处理
  184. if best_question is not None and best_score >= self.answer_sim_threshold:
  185. self._cache_store(question, option, option)
  186. logger.info('Hit correct answer, lower similar question')
  187. return idx + 1
  188. return None
  189. if __name__ == "__main__":
  190. answer = Answer()
  191. test_cases = [
  192. # 精确命中
  193. ('冥界中谁拥阁魔之目一双审善度恶', ['判官', '孟婆', '荒川之主', '阁魔']),
  194. # 相似题命中(缺尾)
  195. ('冥界中谁拥阁魔之目一双', ['判官', '孟婆', '荒川之主', '阁魔']),
  196. # 相似题命中(字形误差)
  197. ('冥界中谁用阎魔之日一双审善渡恶', ['判官', '孟婆', '荒川之主', '阁魔']),
  198. # 选项存在于答案集合
  199. ('冥界中谁拥阁魔', ['六百六十六', '七百七十七', '八百八十八', '阁魔']),
  200. # 完全未知
  201. ('这是一条完全未知的问题', ['甲', '乙', '丙', '丁']),
  202. ]
  203. for question, options in test_cases:
  204. start_time = datetime.now()
  205. result = answer.answer_one(question, options)
  206. cost_ms = (datetime.now() - start_time).microseconds / 1000
  207. print(f'Q: {question} -> {result}, cost: {cost_ms:.2f}ms')