answer.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, option: list[str]):
  41. # 瞎猫当死耗子
  42. opts = {}
  43. for index, option in enumerate(options):
  44. if option in self.data_options.keys():
  45. cnts = [count_intersection(question, ques) for ques in self.data_options[option] ]
  46. cnts.sort(reverse=True)
  47. opts[index + 1] = cnts[0]
  48. #
  49. if opts:
  50. opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
  51. if opts:
  52. return opts[0][0]
  53. else:
  54. return None
  55. question = question.replace('「', '').replace('」', '').replace('?', '')
  56. question = remove_symbols(question)
  57. options = [remove_symbols(option) for option in options]
  58. for index, option in enumerate(options):
  59. if option == '其余选项皆对':
  60. return index + 1
  61. question_matches: list = []
  62. try:
  63. question_matches = self.data[question]
  64. except Exception as e:
  65. # 没有出现题目,从选项反入手
  66. logger.error('Exception: %s', e)
  67. return decide_by_options(question=question, option=options)
  68. # 出现了题目,开始对答案
  69. for index, option in enumerate(options):
  70. for match in question_matches:
  71. if match == option:
  72. return index + 1
  73. # 可能选项识别某一个字错误
  74. if options[0] != '' and options[1] != '' and options[2] != '' and options[3] != '':
  75. for index, option in enumerate(options):
  76. for match in question_matches:
  77. if len(match) != len(option):
  78. continue
  79. if len(match) - count_intersection(match, option) <= 1 :
  80. logger.warning('Option is not match: %s, %s', match, option)
  81. return index + 1
  82. # 选项一个都对不上可能是,识别的选项异常
  83. for index, option in enumerate(options):
  84. if option == '':
  85. logger.error('Option is empty: %s', options)
  86. return index + 1
  87. return None
  88. if __name__ == "__main__":
  89. answer = Answer()
  90. question = '以下式神中,谁从小就是孤'
  91. options = ['生命上限', '小鹿男', '荒川之主']
  92. start_time = datetime.now()
  93. print(answer.answer_one(question, options))
  94. print(answer.answer_one(question, options))
  95. print(datetime.now() - start_time)