answer.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. file = str(Path(__file__).parent / 'data.csv')
  22. with open(file, newline='', encoding='utf-8-sig') as csvfile:
  23. reader = csv.reader(csvfile)
  24. next(reader)
  25. for row in reader:
  26. key = remove_symbols(row[0])
  27. value = remove_symbols(row[1])
  28. if key not in self.data:
  29. self.data[key] = []
  30. self.data[key].append(value)
  31. if value not in self.data_options:
  32. self.data_options[value] = []
  33. self.data_options[value].append(key)
  34. def answer_one(self, question: str, options: list[str]) -> int|None:
  35. """
  36. 每一个问题有三个选项, 返回选项的序号(1、2、3)
  37. :param question:
  38. :param options:
  39. :return:
  40. """
  41. def decide_by_question_foreach(ques: str, ops: list[str]):
  42. # 这个是最耗时的操作,遍历数据库中的所有题目,找到最相识的题目和选项
  43. for key, values in self.data.items():
  44. question_match_ratio = difflib.SequenceMatcher(None, ques, key).ratio()
  45. if question_match_ratio < 0.7:
  46. continue
  47. for value in values:
  48. for index, option in enumerate(ops):
  49. option_match_ratio = difflib.SequenceMatcher(None, option, value).ratio()
  50. if option_match_ratio >= 0.5:
  51. logger.warning('The worst case: no match found for question and option')
  52. logger.warning('Now traversing the entire database to find the most similar question and option')
  53. logger.warning(f'Most similar question: {key} and most similar option: {value}')
  54. return index + 1
  55. return None
  56. def decide_by_options(question: str, ops: list[str]):
  57. # 瞎猫当死耗子
  58. matches = {}
  59. for index, option in enumerate(ops):
  60. if option not in self.data_options.keys():
  61. continue
  62. for ques in self.data_options[option]:
  63. ques_match_ratio = difflib.SequenceMatcher(None, ques, question).ratio()
  64. if ques_match_ratio > 0.8:
  65. matches[index + 1] = ques_match_ratio
  66. if matches:
  67. opts = sorted(matches.items(), key=lambda x: x[1], reverse=True)
  68. logger.warning('No match found for question,\n Now traversing the entire database to find the most similar question')
  69. logger.warning(f'Most similar answer: {opts[0][0]}, and similarity char number is {opts[0][1]}')
  70. return opts[0][0]
  71. index = decide_by_question_foreach(question, ops)
  72. return index if index else None
  73. question = question.replace('「', '').replace('」', '').replace('?', '')
  74. question = remove_symbols(question)
  75. options = [remove_symbols(option) for option in options]
  76. for index, option in enumerate(options):
  77. if option == '其余选项皆对':
  78. return index + 1
  79. question_matches: list = []
  80. try:
  81. question_matches = self.data[question]
  82. except Exception as e:
  83. # 没有出现题目,从选项反入手
  84. logger.error('Exception: %s', e)
  85. return decide_by_options(question=question, ops=options)
  86. # 出现了题目,开始对答案
  87. for index, option in enumerate(options):
  88. for match in question_matches:
  89. if match == option:
  90. return index + 1
  91. # 可能选项识别某一个字错误
  92. if options[0] != '' and options[1] != '' and options[2] != '' and options[3] != '':
  93. for index, option in enumerate(options):
  94. for match in question_matches:
  95. if len(match) != len(option):
  96. continue
  97. if len(match) - count_intersection(match, option) <= 1 :
  98. logger.warning('Option is not match: %s, %s', match, option)
  99. return index + 1
  100. # 最保守策略,答案匹配度最高
  101. opts = {}
  102. for index, option in enumerate(options):
  103. item_match_ratio = 0
  104. for match in question_matches:
  105. match_ratio = difflib.SequenceMatcher(None, match, option).ratio()
  106. if match_ratio >= 0.33 and match_ratio > item_match_ratio:
  107. item_match_ratio = match_ratio
  108. if item_match_ratio > 0:
  109. opts[index + 1] = item_match_ratio
  110. if opts:
  111. opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
  112. logger.warning(f'Use SequenceMatcher and get answers: {opts[0][0]}, score: {opts[0][1]}')
  113. return opts[0][0]
  114. # 选项一个都对不上可能是,正确答案检测为空
  115. for index, option in enumerate(options):
  116. if option == '':
  117. logger.error('Option is empty: %s', options)
  118. return index + 1
  119. return None
  120. if __name__ == "__main__":
  121. answer = Answer()
  122. question = '冥界中谁拥阁魔之目一双审善度恶'
  123. options = ['判官', '孟婆', '荒川之主', '阁魔']
  124. start_time = datetime.now()
  125. print(answer.answer_one(question, options))
  126. print(datetime.now() - start_time)