answer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. for index, option in enumerate(options):
  75. if option == '':
  76. logger.error('Option is empty: %s', options)
  77. return index + 1
  78. return None
  79. if __name__ == "__main__":
  80. answer = Answer()
  81. question = '以下式神中,谁从小就是孤'
  82. options = ['生命上限', '小鹿男', '荒川之主']
  83. start_time = datetime.now()
  84. print(answer.answer_one(question, options))
  85. print(answer.answer_one(question, options))
  86. print(datetime.now() - start_time)