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

Merge pull request #391 from runhey/dev

Dev
huangrunheng 2 лет назад
Родитель
Сommit
ef18b89af7

+ 51 - 18
tasks/DemonEncounter/data/answer.py

@@ -3,6 +3,7 @@
 # github https://github.com/runhey
 import csv
 import re
+import difflib
 
 from datetime import datetime
 from pathlib import Path
@@ -44,21 +45,39 @@ class Answer:
         :param options:
         :return:
         """
-        def decide_by_options(question: str, option: list[str]):
+        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}')
+                            return index + 1
+            return None
+
+        def decide_by_options(question: str, ops: list[str]):
             # 瞎猫当死耗子
-            opts = {}
-            for index, option in enumerate(options):
-                if option in self.data_options.keys():
-                    cnts = [count_intersection(question, ques) for ques in self.data_options[option] ]
-                    cnts.sort(reverse=True)
-                    opts[index + 1] = cnts[0]
-            #
-            if opts:
-                opts = sorted(opts.items(), key=lambda x: x[1], reverse=True)
-            if opts:
+            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]}')
                 return opts[0][0]
-            else:
-                return None
+            index = decide_by_question_foreach(question, ops)
+            return index if index else None
 
 
         question = question.replace('「', '').replace('」', '').replace('?', '')
@@ -75,7 +94,7 @@ class Answer:
         except Exception as e:
             # 没有出现题目,从选项反入手
             logger.error('Exception: %s', e)
-            return decide_by_options(question=question, option=options)
+            return decide_by_options(question=question, ops=options)
         # 出现了题目,开始对答案
         for index, option in enumerate(options):
             for match in question_matches:
@@ -90,7 +109,22 @@ class Answer:
                     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]}')
+            return opts[0][0]
+
+        # 选项一个都对不上可能是,正确答案检测为空
         for index, option in enumerate(options):
             if option == '':
                 logger.error('Option is empty: %s', options)
@@ -100,9 +134,8 @@ class Answer:
 
 if __name__ == "__main__":
     answer = Answer()
-    question = '以下式神中,谁从小就是孤'
-    options = ['生命上限', '小鹿男', '荒川之主']
+    question = '冥界中谁拥阁魔之目一双审善度恶'
+    options = ['判官', '孟婆', '荒川之主', '阁魔']
     start_time = datetime.now()
     print(answer.answer_one(question, options))
-    print(answer.answer_one(question, options))
     print(datetime.now() - start_time)

+ 9 - 1
tasks/DemonEncounter/data/data.csv

@@ -1568,4 +1568,12 @@ SSR式神可以通过多少个对应的式神碎片召唤获得,50
 灶门祢豆子通过哪种方式恢复体力,睡觉
 每个式神最多可以装备几个御魂,6
 剧情中阁魔要求晴明镇压的妖怪是谁,傀儡师
-百鬼夜行中非鬼尺级式神每次可以提供多少碎片,3片
+百鬼夜行中非鬼尺级式神每次可以提供多少碎片,3片
+以下哪个是晓浪荒川之主的特殊效果,吃立不倒
+式神的觉醒需要什么材料,觉醒材料
+不携带任何御魂的狸猫能附加哪种控制效果,挑鲜
+俗称四不像的动物是,康鹿
+头像框一百鬼之主的获得方式是,为崽而战捌段前十
+以下式神中哪一位是可爱的女孩子,纸舞
+想要回顾已经解锁的绘卷剧情入口是,点击庭院大树一追忆绘卷
+御魂珍珠的四件套可能触发的效果是,使被治疗目标获得护

+ 2 - 0
tasks/Quiz/assets.py

@@ -38,5 +38,7 @@ class QuizAssets:
 	O_COUNTDOWN = RuleOcr(roi=(1045,6,27,30), area=(1045,6,27,30), mode="Digit", method="Default", keyword="4", name="countdown")
 	# 倒计时 
 	O_COUNTDOWN3 = RuleOcr(roi=(1045,6,27,30), area=(1045,6,27,30), mode="Digit", method="Default", keyword="3", name="countdown3")
+	# Ocr-description 
+	O_QUESTION_AND_ANSWER = RuleOcr(roi=(902,34,325,534), area=(902,34,325,534), mode="Full", method="Default", keyword="", name="question_and_answer")
 
 

+ 9 - 0
tasks/Quiz/quiz/ocr.json

@@ -61,5 +61,14 @@
     "method": "Default",
     "keyword": "3",
     "description": "倒计时"
+  },
+  {
+    "itemName": "question_and_answer",
+    "roiFront": "902,34,325,534",
+    "roiBack": "902,34,325,534",
+    "mode": "Full",
+    "method": "Default",
+    "keyword": "",
+    "description": "Ocr-description"
   }
 ]

+ 49 - 22
tasks/Quiz/script_task.py

@@ -13,6 +13,7 @@ from tasks.Quiz.debug import Debugger, remove_symbols
 
 from module.logger import logger
 from module.exception import TaskEnd
+from module.base.timer import Timer
 from module.atom.image_grid import ImageGrid
 from module.atom.image import RuleImage
 from module.atom.click import RuleClick
@@ -88,6 +89,8 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
                 continue
         self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = '', '', '', ''
 
+        quiz_timer = Timer(1.4)
+        quiz_timer.start()
         while 1:
             self.screenshot()
 
@@ -103,40 +106,40 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
                 logger.info('Quiz Victory and exit')
                 self.ui_click(self.I_UI_BACK_RED, self.I_START)
                 break
-            self._deal_quiz()
+            if quiz_timer.reached():
+                quiz_timer.reset()
+                self._deal_quiz()
+                continue
         self.close_fn()
 
     def detect_new(self, select_1, select_2, select_3, select_4) -> bool:
-        if self.last_select_1 != select_1 or \
+        new = True if self.last_select_1 != select_1 or \
                 self.last_select_2 != select_2 or \
                 self.last_select_3 != select_3 or \
-                self.last_select_4 != select_4:
-            return True
-        return False
+                self.last_select_4 != select_4 else False
+        self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = \
+            select_1, select_2, select_3, select_4
+        return new
 
     def _deal_quiz(self):
-        answer_1 = self.O_ANSWER1.detect_text(self.device.image)
-        answer_2 = self.O_ANSWER2.detect_text(self.device.image)
-        answer_3 = self.O_ANSWER3.detect_text(self.device.image)
-        answer_4 = self.O_ANSWER4.detect_text(self.device.image)
+        countdown = self.O_COUNTDOWN.ocr(self.device.image)
+        if countdown < 2 or countdown > 5:
+            # 最后两秒钟的时候 进行选择
+            return False
+        question, answer_1, answer_2, answer_3, answer_4 = self.detect_question_and_answers()
         if answer_1 == '' and answer_2 == '' and answer_3 == '' and answer_4 == '':
             return False
+        question = remove_symbols(question)
+
         new_question = self.detect_new(answer_1, answer_2, answer_3, answer_4)
-        if not new_question and\
-                not self.ocr_appear(self.O_COUNTDOWN) and\
-                not self.ocr_appear(self.O_COUNTDOWN3):
-            # 第一次换题目的时候 和 最后两秒钟的时候 进行选择
-            return False
         if new_question:
             self.answer_cnt += 1
             logger.info(f'Question count: {self.answer_cnt}')
 
-        self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = \
-            answer_1, answer_2, answer_3, answer_4
-        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)
+        # 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:
@@ -151,11 +154,35 @@ class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
             index_options.remove(index)
             index = random.choice(list(index_options))
         logger.info(f'Question: {question}, Answer: {index}')
-        self.click(self.click_options[index-1])
+        self.click(self.click_options[index-1], interval=1)
         self.device.click_record_clear()
         return True
 
-
+    def detect_question_and_answers(self) -> tuple:
+        results = self.O_QUESTION_AND_ANSWER.detect_and_ocr(self.device.image)
+        question = ''
+        answer_1 = ''
+        answer_2 = ''
+        answer_3 = ''
+        answer_4 = ''
+        for result in results:
+            # box 是四个点坐标 左上, 右上, 右下, 左下
+            # x1, y1, x2, y2 = result.box[0][0], result.box[0][1], result.box[2][0], result.box[2][1]
+            # w, h = x2 - x1, y2 - y1
+            y_start = result.box[0][1]
+            y_end = result.box[2][1]
+            text = result.ocr_text
+            if y_start >= 0 and y_end <= 150:
+                question += text
+            elif y_start > 150 and y_end <= 240:
+                answer_1 += text
+            elif y_start > 240 and y_end <= 370:
+                answer_2 += text
+            elif y_start > 370 and y_end <= 470:
+                answer_3 += text
+            elif y_start > 470 and y_end <= 600:
+                answer_4 += text
+        return question, answer_1, answer_2, answer_3, answer_4