answer.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. from datetime import datetime
  7. from pathlib import Path
  8. from module.logger import logger
  9. def count_intersection(str1, str2):
  10. set1 = set(str1)
  11. set2 = set(str2)
  12. intersection = set1.intersection(set2)
  13. return len(intersection)
  14. def remove_symbols(text):
  15. return re.sub(r'[^\w\s]', '', text)
  16. class Answer:
  17. def __init__(self):
  18. self.data: dict[str, list] = {}
  19. self.data_options: dict[str, list] = {}
  20. file = str(Path(__file__).parent / 'data.csv')
  21. with open(file, newline='', encoding='utf-8-sig') as csvfile:
  22. reader = csv.reader(csvfile)
  23. next(reader)
  24. for row in reader:
  25. key = remove_symbols(row[0])
  26. value = remove_symbols(row[1])
  27. if key not in self.data:
  28. self.data[key] = []
  29. self.data[key].append(value)
  30. if value not in self.data_options:
  31. self.data_options[value] = []
  32. self.data_options[value].append(key)
  33. def answer_one(self, question: str, options: list[str]) -> int|None:
  34. """
  35. 每一个问题有三个选项, 返回选项的序号(1、2、3)
  36. :param question:
  37. :param options:
  38. :return:
  39. """
  40. def decide_by_options(question: str, ops: list[str]):
  41. # 瞎猫当死耗子
  42. opts = {}
  43. for index, option in enumerate(ops):
  44. for char in option:
  45. pass
  46. if option in self.data_options.keys():
  47. cnts = [count_intersection(question, ques) for ques in self.data_options[option] ]
  48. cnts.sort(reverse=True)
  49. opts[index + 1] = cnts[0]
  50. #
  51. if opts:
  52. opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
  53. if opts:
  54. return opts[0][0]
  55. else:
  56. return None
  57. question = question.replace('「', '').replace('」', '').replace('?', '')
  58. question = remove_symbols(question)
  59. options = [remove_symbols(option) for option in options]
  60. for index, option in enumerate(options):
  61. if option == '其余选项皆对':
  62. return index + 1
  63. question_matches: list = []
  64. try:
  65. question_matches = self.data[question]
  66. except Exception as e:
  67. # 没有出现题目,从选项反入手
  68. logger.error('Exception: %s', e)
  69. return decide_by_options(question=question, ops=options)
  70. # 出现了题目,开始对答案
  71. for index, option in enumerate(options):
  72. for match in question_matches:
  73. if match == option:
  74. return index + 1
  75. # 可能选项识别某一个字错误
  76. if options[0] != '' and options[1] != '' and options[2] != '' and options[3] != '':
  77. for index, option in enumerate(options):
  78. for match in question_matches:
  79. if len(match) != len(option):
  80. continue
  81. if len(match) - count_intersection(match, option) <= 1 :
  82. logger.warning('Option is not match: %s, %s', match, option)
  83. return index + 1
  84. # 选项一个都对不上可能是,识别的选项异常
  85. for index, option in enumerate(options):
  86. if option == '':
  87. logger.error('Option is empty: %s', options)
  88. return index + 1
  89. return None
  90. if __name__ == "__main__":
  91. answer = Answer()
  92. question = '冥界中谁拥阁魔之目一双审善度恶'
  93. options = ['判官', '孟婆', '荒川之主', '阁魔']
  94. start_time = datetime.now()
  95. print(answer.answer_one(question, options))
  96. print(datetime.now() - start_time)