Просмотр исходного кода

Merge pull request #1476 from AzurTian/dev_answer

Dev answer
huangrunheng 7 месяцев назад
Родитель
Сommit
145474e236
3 измененных файлов с 206 добавлено и 172 удалено
  1. 191 150
      tasks/DemonEncounter/data/answer.py
  2. 12 9
      tasks/Quiz/debug.py
  3. 3 13
      tasks/Quiz/script_task.py

+ 191 - 150
tasks/DemonEncounter/data/answer.py

@@ -10,179 +10,220 @@ from pathlib import Path
 
 from module.logger import logger
 
-def count_intersection(str1, str2):
-    set1 = set(str1)
-    set2 = set(str2)
-    intersection = set1.intersection(set2)
-    return len(intersection)
 
 def remove_symbols(text):
     return re.sub(r'[^\w\s]', '', text)
 
+
+def _tokenize_question(text: str) -> list[str]:
+    # 2-gram 题目切词,长度不足时退化为 1-gram
+    text = text.replace(' ', '')
+    if len(text) < 2:
+        return list(text)
+    return [text[i:i + 2] for i in range(len(text) - 1)]
+
+
+def _normalize(q: str, opts: list[str]) -> tuple[str, list[str]]:
+    # 统一清洗 OCR 字符
+    q = q.replace('「', '').replace('」', '').replace('?', '')
+    q = remove_symbols(q)
+    opts = [remove_symbols(opt) for opt in opts]
+    return q, opts
+
+
 class Answer:
-    def __init__(self):
-        self.data: dict[str, list] = {}
-        self.data_options: dict[str, list] = {}
-        # 添加缓存以确保相同问题的答案一致性
-        # 修改:缓存存储答案字符串而不是索引,避免选项顺序变化的影响
-        self.question_cache: dict[str, str] = {}
-        file = str(Path(__file__).parent / 'data.csv')
-        with open(file, newline='', encoding='utf-8-sig') as csvfile:
+    question_sim_threshold = 0.8
+    answer_sim_threshold = 0.5
+    data_file = Path(__file__).parent / 'data.csv'
+    question_answer: dict[str, set[str]] = {}  # 题目: {标准答案
+    answer_question: dict[str, set[str]] = {}  # 标准答案: {题目}
+    question_index: dict[str, set[str]] = {}  # 倒排索引
+    question_cache: dict[str, set[str]] = {}  # 缓存:识别到的问题 -> {标准答案, 选项答案}
+
+    def __init__(self, top_n: int = 30):
+        self.top_n = top_n
+        with open(self.data_file, newline='', encoding='utf-8') as csvfile:
             reader = csv.reader(csvfile)
             next(reader)
             for row in reader:
                 key = remove_symbols(row[0])
                 value = remove_symbols(row[1])
-                if key not in self.data:
-                    self.data[key] = []
-                self.data[key].append(value)
-                if value not in self.data_options:
-                    self.data_options[value] = []
-                self.data_options[value].append(key)
-
-    def answer_one(self, question: str, options: list[str]) -> int|None:
+                self.question_answer.setdefault(key, set()).add(value)
+                self.answer_question.setdefault(value, set()).add(key)
+                for token in _tokenize_question(key):
+                    self.question_index.setdefault(token, set()).add(key)
+
+    def answer_one(self, question: str, options: list[str]) -> int | None:
         """
         每一个问题有三个选项, 返回选项的序号(1、2、3)
         :param question:
         :param options:
         :return:
         """
-        
-        # 检查缓存,如果已经处理过类似问题,直接返回结果
-        # 修改:现在缓存的是答案字符串,需要在选项中查找对应的索引
-        for cached_q, cached_answer_str in self.question_cache.items():
-            similarity = difflib.SequenceMatcher(None, question, cached_q).ratio()
-            if similarity >= 0.9:  # 如果问题相似度超过90%,直接返回缓存的答案
-                # 在当前选项中查找缓存的答案字符串
-                for idx, option in enumerate(options):
-                    if option == cached_answer_str:
-                        return idx + 1  # 返回选项索引(从1开始)
-                # 如果缓存的答案在当前选项中不存在,则尝试模糊匹配
-                for idx, option in enumerate(options):
-                    if difflib.SequenceMatcher(None, option, cached_answer_str).ratio() >= 0.9:
-                        logger.info(f"Fuzzy match cache: '{cached_answer_str}' ~ '{option}'")
-                        return idx + 1
-        def decide_by_question_foreach(ques: str, ops: list[str]):
-            # 这个是最耗时的操作,遍历数据库中的所有题目,找到最相识的题目和选项
-            for key, values in self.data.items():
-                question_match_ratio = difflib.SequenceMatcher(None, ques, key).ratio()
-                if question_match_ratio < 0.7:
-                    continue
-                for value in values:
-                    for index, option in enumerate(ops):
-                        option_match_ratio = difflib.SequenceMatcher(None, option, value).ratio()
-                        if option_match_ratio >= 0.5:
-                            logger.warning('The worst case: no match found for question and option')
-                            logger.warning('Now traversing the entire database to find the most similar question and option')
-                            logger.warning(f'Most similar question: {key} and most similar option: {value}')
-                            result_index = index + 1
-                            # 修改:缓存问题和答案字符串
-                            self.question_cache[ques] = ops[index]
-                            return result_index
-            return None
-
-        def decide_by_options(question: str, ops: list[str]):
-            # 瞎猫当死耗子
-            matches = {}
-            for index, option in enumerate(ops):
-                if option not in self.data_options.keys():
-                    continue
-                for ques in self.data_options[option]:
-                    ques_match_ratio = difflib.SequenceMatcher(None, ques, question).ratio()
-                    if ques_match_ratio > 0.8:
-                        matches[index + 1] = ques_match_ratio
-            if matches:
-                opts = sorted(matches.items(), key=lambda x: x[1], reverse=True)
-                logger.warning('No match found for question,\n Now traversing the entire database to find the most similar question')
-                logger.warning(f'Most similar answer: {opts[0][0]}, and similarity char number is {opts[0][1]}')
-                result_index = opts[0][0]
-                # 修改:缓存问题和答案字符串
-                self.question_cache[question] = ops[result_index - 1]
-                return result_index
-            index = decide_by_question_foreach(question, ops)
-            return index if index else None
-
-
-        question = question.replace('「', '').replace('」', '').replace('?', '')
-        question = remove_symbols(question)
-        options = [remove_symbols(option) for option in options]
-
+        question, options = _normalize(question, options)
         for index, option in enumerate(options):
             if option == '其余选项皆对':
                 result_index = index + 1
-                # 特殊选项才缓存
-                # 修改:缓存问题和答案字符串
-                self.question_cache[question] = option
+                # 缓存问题和答案字符串
+                self._cache_store(question, option, option)
+                logger.debug('Answer strategy: special option hit.')
                 return result_index
-
-        question_matches: list = []
-        try:
-            question_matches = self.data[question]
-        except Exception as e:
-            # 没有出现题目,从选项反入手
-            logger.error('Exception: %s', e)
-            result = decide_by_options(question=question, ops=options)
-            if result is not None:
-                # 只有在使用模糊匹配的情况下才缓存结果
-                # 修改:缓存问题和答案字符串
-                self.question_cache[question] = options[result - 1]
+        # 1) 优先命中缓存
+        cached_index = self._cache_hit(question, options)
+        if cached_index is not None:
+            return cached_index
+        # 2) 题目命中:精确或相似选项匹配
+        if question in self.question_answer:
+            return self._handle_known_question(question, options)
+        # 3) 相似题:先精确,再相似匹配
+        result = self._handle_similar_questions(question, options)
+        if result is not None:
             return result
-        # 出现了题目,开始对答案
-        for index, option in enumerate(options):
-            for match in question_matches:
-                if match == option:
-                    return index + 1  # 精确匹配,不缓存,直接返回
-        # 可能选项识别某一个字错误
-        if all(options):
-            for index, option in enumerate(options):
-                for match in question_matches:
-                    if len(match) != len(option):
-                        continue
-                    if len(match) - count_intersection(match, option) <= 1 :
-                        logger.warning('Option is not match: %s, %s', match, option)
-                        return index + 1  # 这也是精确匹配的一种,不缓存,直接返回
-        # 最保守策略,答案匹配度最高
-        opts = {}
-        for index, option in enumerate(options):
-            item_match_ratio = 0
-            for match in question_matches:
-                match_ratio = difflib.SequenceMatcher(None, match, option).ratio()
-                if match_ratio >= 0.33 and match_ratio > item_match_ratio:
-                    item_match_ratio = match_ratio
-            if item_match_ratio > 0:
-                opts[index + 1] = item_match_ratio
-        if opts:
-            opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
-            logger.warning(f'Use SequenceMatcher and get answers: {opts[0][0]}, score: {opts[0][1]}')
-            result_index = opts[0][0]
-            # 只有在这种情况下才缓存
-            # 修改:缓存问题和答案字符串
-            self.question_cache[question] = options[result_index - 1]
-            return result_index
-
-        # 选项一个都对不上可能是,正确答案检测为空
-        for index, option in enumerate(options):
-            if option == '':
-                logger.error('Option is empty: %s', options)
-                result_index = index + 1
-                # 只有在这种情况下才缓存
-                # 修改:缓存问题和答案字符串
-                self.question_cache[question] = option
-                return result_index
-                
-        # 如果所有方法都失败,尝试使用decide_by_options
-        result = decide_by_options(question=question, ops=options)
+        # 4) 选项是否存在于答案集合
+        result = self._handle_answer_option_match(question, options)
         if result is not None:
-            # 只有在使用模糊匹配的情况下才缓存结果
-            # 修改:缓存问题和答案字符串
-            self.question_cache[question] = options[result - 1]
-        return result
+            return result
+        # 5) 完全未知:返回 None
+        logger.error('Unknown question: %s', question)
+        return None
+
+    def _cache_store(self, question: str, std_answer: str, chosen: str) -> None:
+        # 更新缓存,便于下一次直接命中
+        cache = self.question_cache.setdefault(question, set())
+        if std_answer:
+            cache.add(std_answer)
+        if chosen:
+            cache.add(chosen)
+
+    def _find_option_index(self, opts: list[str], target: str) -> int | None:
+        # 精确查找选项索引
+        for idx, option in enumerate(opts):
+            if option == target:
+                return idx + 1
+        return None
+
+    def _cache_hit(self, question: str, opts: list[str]) -> int | None:
+        # 优先命中缓存答案
+        cached = self.question_cache.get(question)
+        if not cached:
+            return None
+        for ans in cached:
+            idx = self._find_option_index(opts, ans)
+            if idx is not None:
+                logger.info('Hit cache.')
+                return idx
+        return None
+
+    def _best_similarity_option(self, options: list[str], answer: str) -> tuple[int | None, float]:
+        # 从选项中找与答案最相似的项
+        best_idx = None
+        best_score = 0.0
+        for idx, option in enumerate(options):
+            score = difflib.SequenceMatcher(None, option, answer).ratio()
+            if score > best_score:
+                best_score = score
+                best_idx = idx + 1
+        return best_idx, best_score
+
+    def _get_similar_questions(self, question: str) -> list[tuple[str, float]]:
+        # 倒排索引过滤候选问题,再按相似度排序
+        tokens = _tokenize_question(question)
+        if not tokens:
+            return []
+        candidate_scores: dict[str, int] = {}
+        for token in tokens:
+            for key in self.question_index.get(token, []):
+                candidate_scores[key] = candidate_scores.get(key, 0) + 1
+        if not candidate_scores:
+            return []
+        candidates = sorted(candidate_scores.items(), key=lambda x: x[1], reverse=True)
+        if self.top_n and len(candidates) > self.top_n:
+            candidates = candidates[:self.top_n]
+        scored = []
+        for key, _ in candidates:
+            score = difflib.SequenceMatcher(None, question, key).ratio()
+            if score >= self.question_sim_threshold:
+                scored.append((key, score))
+        scored.sort(key=lambda x: x[1], reverse=True)
+        return scored
+
+    def _handle_known_question(self, question: str, options: list[str]) -> int | None:
+        # 题目命中时,优先精确匹配答案
+        for answer in self.question_answer[question]:
+            idx = self._find_option_index(options, answer)
+            if idx is not None:
+                logger.info('Hit question, correct answer.')
+                self._cache_store(question, answer, options[idx - 1])
+                return idx
+        # 题目命中但选项不一致:按相似度选最接近答案
+        best_idx, best_score, best_answer = None, 0.0, ""
+        for answer in self.question_answer[question]:
+            idx, score = self._best_similarity_option(options, answer)
+            if idx is not None and score > best_score:
+                best_idx, best_score, best_answer = idx, score, answer
+        if best_idx is not None:
+            logger.info('Hit question, similar answer.')
+            self._cache_store(question, best_answer, options[best_idx - 1])
+        return best_idx
+
+    def _handle_similar_questions(self, question: str, options: list[str]) -> int | None:
+        # 相似题优先:逐题匹配答案是否在选项中
+        similar_questions = self._get_similar_questions(question)
+        for key, _ in similar_questions:
+            for answer in self.question_answer.get(key, []):
+                idx = self._find_option_index(options, answer)
+                if idx is not None:
+                    logger.info('Hit similar question, correct answer')
+                    self._cache_store(question, answer, options[idx - 1])
+                    return idx
+        # 相似题答案不在选项中:做相似度匹配
+        best_idx, best_score, best_answer = None, 0.0, ""
+        for key, _ in similar_questions:
+            for answer in self.question_answer.get(key, []):
+                idx, score = self._best_similarity_option(options, answer)
+                if idx is not None and score > best_score:
+                    best_idx, best_score, best_answer = idx, score, answer
+        if best_idx is not None and best_score >= self.answer_sim_threshold:
+            logger.info('Hit similar question, similar answer')
+            self._cache_store(question, best_answer, options[best_idx - 1])
+            return best_idx
+        return None
+
+    def _handle_answer_option_match(self, question: str, options: list[str]) -> int | None:
+        # 选项命中答案集合时,根据题目相似度校验
+        for idx, option in enumerate(options):
+            if option in self.answer_question:
+                questions = self.answer_question.get(option, set())
+                best_question, best_score = None, 0.0
+                for candidate in questions:
+                    score = difflib.SequenceMatcher(None, question, candidate).ratio()
+                    if score > best_score:
+                        best_score = score
+                        best_question = candidate
+                # 选项在答案中, 则问题相似度降级处理
+                if best_question is not None and best_score >= self.answer_sim_threshold:
+                    self._cache_store(question, option, option)
+                    logger.info('Hit correct answer, lower similar question')
+                    return idx + 1
+        return None
 
 
 if __name__ == "__main__":
     answer = Answer()
-    question = '冥界中谁拥阁魔之目一双审善度恶'
-    options = ['判官', '孟婆', '荒川之主', '阁魔']
-    start_time = datetime.now()
-    print(answer.answer_one(question, options))
-    print(datetime.now() - start_time)
+    test_cases = [
+        # 精确命中
+        ('冥界中谁拥阁魔之目一双审善度恶', ['判官', '孟婆', '荒川之主', '阁魔']),
+        # 相似题命中(缺尾)
+        ('冥界中谁拥阁魔之目一双', ['判官', '孟婆', '荒川之主', '阁魔']),
+        # 相似题命中(字形误差)
+        ('冥界中谁用阎魔之日一双审善渡恶', ['判官', '孟婆', '荒川之主', '阁魔']),
+        # 选项存在于答案集合
+        ('冥界中谁拥阁魔', ['六百六十六', '七百七十七', '八百八十八', '阁魔']),
+        # 完全未知
+        ('这是一条完全未知的问题', ['甲', '乙', '丙', '丁']),
+    ]
+    for question, options in test_cases:
+        start_time = datetime.now()
+        result = answer.answer_one(question, options)
+        cost_ms = (datetime.now() - start_time).microseconds / 1000
+        print(f'Q: {question} -> {result}, cost: {cost_ms:.2f}ms')

+ 12 - 9
tasks/Quiz/debug.py

@@ -3,35 +3,38 @@
 # github https://github.com/runhey
 import re
 
-from cached_property import cached_property
 from pathlib import Path
 
-from module.logger import logger
+from filelock import FileLock
 
 
 def remove_symbols(text):
     return re.sub(r'[^\w\s]', '', text)
 
+
 class Debugger:
 
-    @cached_property
-    def fn(self):
-        # 以添加方式打开一个文件
+    def _log_file(self) -> Path:
         file: Path = Path(f'./log/quiz/supplement.txt')
         if not file.parent.exists():
             file.parent.mkdir(parents=True)
         if not file.exists():
             file.touch()
-        f = open(str(file), 'a', encoding='utf-8')
-        return f
+        return file
 
     def append_one(self, question: str, options: list[str]):
         question = remove_symbols(question)
         options = [remove_symbols(option) for option in options]
-        self.fn.write(f'{question},{options[0]},{options[1]},{options[2]},{options[3]}\n')
+        file = self._log_file()
+        lock = FileLock(f"{file}.lock")
+        line = f'{question},{options[0]},{options[1]},{options[2]},{options[3]}\n'
+        with lock:
+            with open(file, 'a', encoding='utf-8') as f:
+                f.write(line)
+                f.flush()
 
     def close_fn(self):
-        self.fn.close()
+        return
 
 
 if __name__ == '__main__':

+ 3 - 13
tasks/Quiz/script_task.py

@@ -20,9 +20,11 @@ from module.atom.image import RuleImage
 from module.atom.click import RuleClick
 from module.device.screenshot import Screenshot
 
+
 class NoTicket(Exception):
     pass
 
+
 class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
 
     answer_cnt = 0
@@ -169,11 +171,6 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
         self.answer_cnt += 1
         logger.info(f'Question count: {self.answer_cnt}')
 
-        # questions = self.O_QUESTION.detect_and_ocr(self.device.image)
-        # question = ''.join([q.ocr_text for q in questions])
-        # question = question.replace('?', '').replace('?', '').replace(' ', '').replace(',', ',')
-        # question = remove_symbols(question)
-
         index = self.anwser.answer_one(question=question,  options=[answer_1, answer_2, answer_3, answer_4])
         if index is None:
             logger.error('Now question has no answer, please check')
@@ -186,7 +183,7 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
             index_options = {1, 2, 3, 4}
             index_options.remove(index)
             index = random.choice(list(index_options))
-        logger.info(f'Question: {question}, Answer: {index}{[answer_1, answer_2, answer_3, answer_4]}')
+        logger.attr(index, 'Answer')
         self.click(self.click_options[index-1], interval=1)
         time.sleep(0.5)
         if index == 1:
@@ -203,10 +200,6 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
     def detect_question_and_answers(self) -> tuple:
         results = self.O_QUESTION.detect_and_ocr(self.device.image)
         question = ''
-        answer_1 = ''
-        answer_2 = ''
-        answer_3 = ''
-        answer_4 = ''
         answer_1 = remove_symbols(self.O_ANSWER1.ocr(self.device.image))
         answer_2 = remove_symbols(self.O_ANSWER2.ocr(self.device.image))
         answer_3 = remove_symbols(self.O_ANSWER3.ocr(self.device.image))
@@ -224,9 +217,6 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
         return question, answer_1, answer_2, answer_3, answer_4
 
 
-
-
-
 if __name__ == '__main__':
     from module.config.config import Config
     from module.device.device import Device