Skip to content

feat: 新增ocr參考庫 #221

Open
Abino01 wants to merge 8 commits into
1bananachicken:devfrom
Abino01:dev
Open

feat: 新增ocr參考庫 #221
Abino01 wants to merge 8 commits into
1bananachicken:devfrom
Abino01:dev

Conversation

@Abino01
Copy link
Copy Markdown
Contributor

@Abino01 Abino01 commented May 27, 2026

新增ocr_i18n_lib.py做為未來的ocr文字判定對應庫 不再透過assets/resource/base/pipeline/內的.json內容作判定 .json之後只作為roi區域讀取跟判定結果的操作 不參與文字檢驗跟判定
舉例來說
更動.json前

"FishNewBaitLack": {
        "desc": "缺少鱼饵",
        "recognition": {
            "type": "OCR",
            "param": {
                "roi": [520,340,240,40],
                "expected": [
                    "需要装备鱼饵才可以钓鱼",
                    "需要裝備魚餌才可以釣魚",
                    .... (省略) ],
                "threshold": 0.5,
                "roi_offset": [0,0,220,0]
            }
        },

更動.json後

"FishNewBaitLack": {
        "desc": "缺少鱼饵",
        "recognition": {
            "type": "CustomOCRVerify", # 假設框架允許自定義識別器名稱
            "param": {
                "task_name": "FishNewBaitLack", # 傳給 python 庫對照的 Key
                "roi": [520, 340, 240, 40],
                "threshold": 0.5
            }
        },

可以大幅降低未來維護的時間

路徑 agent/utils/ocr_i18n_lib.py

更新 move 0526*0951

現在在思考怎麼改 在agent/custom/action/裡的.py

Summary by Sourcery

引入一个集中管理的多语言 OCR 匹配库和验证辅助工具,将文本识别规则与 JSON 流水线配置解耦。

新功能:

  • 新增可复用的 MULTILANG_OCR_LIBRARY,用于在多种语言中将任务名称映射到本地化的 OCR 匹配模式。
  • 提供 verify_single_task_ocr 辅助函数,通过精确匹配和模糊匹配来验证指定任务的 OCR 输出。
Original summary in English

Summary by Sourcery

Introduce a centralized multi-language OCR matching library and verification helper to decouple text recognition rules from JSON pipeline configurations.

New Features:

  • Add a reusable MULTILANG_OCR_LIBRARY mapping task names to localized OCR match patterns across multiple languages.
  • Provide a verify_single_task_ocr helper to validate OCR output for a specified task using exact and fuzzy matching.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 27, 2026

Reviewer's Guide

引入一个集中式多语言 OCR 校验库和辅助函数,通过将任务名称映射到预期的本地化字符串,并对 OCR 输出执行精确/正则匹配和模糊匹配,从而将文本匹配规则与 JSON 流水线配置解耦。

使用 verify_single_task_ocr 的集中式多语言 OCR 校验流程图

flowchart TD
    A["Frontend OCR node<br/>(task_name, roi_text)"] --> B[Call verify_single_task_ocr]
    B --> C{"task_name in<br/>MULTILANG_OCR_LIBRARY?"}
    C -->|No| Z["Return False<br/>(skip action)"]
    C -->|Yes| D["Load task_data<br/>from MULTILANG_OCR_LIBRARY"]
    D --> E["cleaned_text =<br/>frontend_ocr_text.strip()"]
    E --> F{"Exact/regex match<br/>re.search over expected"}
    F -->|Matched| X["Return True<br/>(hit on exact/regex)"]
    F -->|No match| G{"Fuzzy match<br/>SequenceMatcher<br/>similarity >= threshold"}
    G -->|Matched| Y["Return True<br/>(hit on fuzzy)"]
    G -->|No match| Z
Loading

File-Level Changes

Change Details Files
Add centralized multi-language OCR task library for OCR text verification keyed by task name.
  • Define MULTILANG_OCR_LIBRARY mapping task names to descriptions and lists of expected phrases/regex patterns across multiple languages.
  • Consolidate multiple previously separate OCR tasks into unified task keys (e.g., various claim, fountain, fishing, event, and mini-game flows).
  • Document the expected configuration pattern for tasks, including support for regex (e.g., English with r"" and \s*), and provide comments on task grouping and renaming for maintainability.
agent/utils/ocr_i18n_lib.py
Implement reusable OCR verification helper with exact and fuzzy matching support.
  • Implement verify_single_task_ocr(task_name, frontend_ocr_text, threshold=0.5) that validates OCR output against MULTILANG_OCR_LIBRARY.
  • Use regex search for primary matching, allowing patterns to handle partial text, spacing, and case-insensitive English rules.
  • Fallback to difflib.SequenceMatcher-based similarity scoring for non-regex patterns when exact matching fails, controlled by a configurable threshold.
  • Short-circuit and return False when the requested task_name is not in the library.
agent/utils/ocr_i18n_lib.py

提示与命令

与 Sourcery 交互

  • 触发新的代码审查: 在 Pull Request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub Issue: 通过回复某条审查评论,要求 Sourcery 从该评论创建一个 Issue。你也可以在审查评论下回复 @sourcery-ai issue 来从该评论创建 Issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题中的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 Pull Request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文中的任意位置写上 @sourcery-ai summary,即可在指定位置生成 PR 摘要。你也可以在 Pull Request 中评论 @sourcery-ai summary 来在任意时间(重新)生成摘要。
  • 生成 Reviewer’s Guide: 在 Pull Request 中评论 @sourcery-ai guide,即可在任意时间(重新)生成 Reviewer’s Guide。
  • 一键解决所有 Sourcery 评论: 在 Pull Request 中评论 @sourcery-ai resolve 来标记已解决所有 Sourcery 评论。适用于你已经处理完所有评论且不希望再看到它们的时候。
  • 一键忽略所有 Sourcery Review: 在 Pull Request 中评论 @sourcery-ai dismiss 来忽略所有现有的 Sourcery Review。特别适合你想从头开始新的审查时——别忘了再评论一次 @sourcery-ai review 来触发新的审查!

自定义使用体验

打开你的 dashboard

  • 启用或关闭诸如 Sourcery 自动生成的 Pull Request 摘要、Reviewer’s Guide 等审查功能。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获取帮助

Original review guide in English

Reviewer's Guide

Introduce a centralized multi-language OCR verification library and helper function that decouples text-matching rules from JSON pipeline configs by mapping task names to expected localized strings and performing exact/regex and fuzzy matching on OCR outputs.

Flow diagram for centralized multi-language OCR verification using verify_single_task_ocr

flowchart TD
    A["Frontend OCR node<br/>(task_name, roi_text)"] --> B[Call verify_single_task_ocr]
    B --> C{"task_name in<br/>MULTILANG_OCR_LIBRARY?"}
    C -->|No| Z["Return False<br/>(skip action)"]
    C -->|Yes| D["Load task_data<br/>from MULTILANG_OCR_LIBRARY"]
    D --> E["cleaned_text =<br/>frontend_ocr_text.strip()"]
    E --> F{"Exact/regex match<br/>re.search over expected"}
    F -->|Matched| X["Return True<br/>(hit on exact/regex)"]
    F -->|No match| G{"Fuzzy match<br/>SequenceMatcher<br/>similarity >= threshold"}
    G -->|Matched| Y["Return True<br/>(hit on fuzzy)"]
    G -->|No match| Z
Loading

File-Level Changes

Change Details Files
Add centralized multi-language OCR task library for OCR text verification keyed by task name.
  • Define MULTILANG_OCR_LIBRARY mapping task names to descriptions and lists of expected phrases/regex patterns across multiple languages.
  • Consolidate multiple previously separate OCR tasks into unified task keys (e.g., various claim, fountain, fishing, event, and mini-game flows).
  • Document the expected configuration pattern for tasks, including support for regex (e.g., English with r"" and \s*), and provide comments on task grouping and renaming for maintainability.
agent/utils/ocr_i18n_lib.py
Implement reusable OCR verification helper with exact and fuzzy matching support.
  • Implement verify_single_task_ocr(task_name, frontend_ocr_text, threshold=0.5) that validates OCR output against MULTILANG_OCR_LIBRARY.
  • Use regex search for primary matching, allowing patterns to handle partial text, spacing, and case-insensitive English rules.
  • Fallback to difflib.SequenceMatcher-based similarity scoring for non-regex patterns when exact matching fails, controlled by a configurable threshold.
  • Short-circuit and return False when the requested task_name is not in the library.
agent/utils/ocr_i18n_lib.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了两个问题,并给出了一些整体性的反馈:

  • 在若干个 expected 列表中,因为缺少逗号,你不小心把相邻的字符串字面量拼接在一起了(例如:InCharactersMenu 中有 r"(?i)Profile" r"(?i)INFO",以及 Ptory"入る" 后面紧跟着 r"(?i)ENTER"),这会改变原本预期的匹配模式;请在这些字面量之间补上逗号。
  • 建议在 verify_single_task_ocr 中对 frontend_ocr_text 为非字符串或 None 的情况做防御性处理(例如:如果它为假值或不是 str,就尽早返回 False),以避免在调用 .strip() 或正则时出现意外异常。
  • 由于 MULTILANG_OCR_LIBRARY 体积较大且大量依赖正则,你可能会希望预先编译一次这些正则模式(例如将编译后的 pattern 存起来而不是原始字符串),从而避免在每次调用 verify_single_task_ocr 时重复解析,降低单次调用开销。
给 AI 代理的提示词
Please address the comments from this code review:

## Overall Comments
- In several `expected` lists you are accidentally concatenating adjacent string literals due to missing commas (e.g. `InCharactersMenu` has `r"(?i)Profile" r"(?i)INFO"`, and `Ptory` has `"入る"` followed directly by `r"(?i)ENTER"`), which will change the intended patterns; please add commas between these literals.
- Consider defensively handling non-string or `None` values for `frontend_ocr_text` in `verify_single_task_ocr` (e.g. early return `False` if it's falsy or not a `str`) to avoid unexpected exceptions from `.strip()` or the regex calls.
- Since `MULTILANG_OCR_LIBRARY` is large and heavily regex-based, you may want to precompile regex patterns once (e.g. store compiled patterns instead of raw strings) to avoid re-parsing them on every `verify_single_task_ocr` call and reduce per-call overhead.

## Individual Comments

### Comment 1
<location path="agent/utils/ocr_i18n_lib.py" line_range="334-335" />
<code_context>
+    "InCharactersMenu": {
+        "desc": "在角色菜单内",
+        "expected": [
+            "个人信息", "個人資訊", "信息", "資訊",
+            r"(?i)PROFILE", r"(?i)Profile" r"(?i)INFO", r"(?i)Info",
+            "メッセージ", "情報", "정보"
+        ]
</code_context>
<issue_to_address>
**issue (bug_risk):** Missing comma between the `Profile` and `INFO` patterns causes an unintended concatenated regex.

Python concatenates adjacent string literals, so `r"(?i)Profile" r"(?i)INFO"` becomes `r"(?i)Profile(?i)INFO"`, which won’t behave as two separate patterns. Add a comma after `r"(?i)Profile"` to make them distinct entries in the list.
</issue_to_address>

### Comment 2
<location path="agent/utils/ocr_i18n_lib.py" line_range="561-123" />
<code_context>
+    "Ptory": {
+        "desc": "在钮",
+        "expected": [
+            "进入", "進入", "入る"
+            r"(?i)ENTER", r"(?i)Enter",
+            "들"
+        ]
</code_context>
<issue_to_address>
**issue (bug_risk):** Missing comma after the last CJK string leads to invalid syntax or unintended concatenation.

Add a trailing comma after "入る" so this remains a separate list element and doesn’t get concatenated with the following `r"(?i)ENTER"` string literal.
</issue_to_address>

Sourcery 对开源项目免费 —— 如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've found 2 issues, and left some high level feedback:

  • In several expected lists you are accidentally concatenating adjacent string literals due to missing commas (e.g. InCharactersMenu has r"(?i)Profile" r"(?i)INFO", and Ptory has "入る" followed directly by r"(?i)ENTER"), which will change the intended patterns; please add commas between these literals.
  • Consider defensively handling non-string or None values for frontend_ocr_text in verify_single_task_ocr (e.g. early return False if it's falsy or not a str) to avoid unexpected exceptions from .strip() or the regex calls.
  • Since MULTILANG_OCR_LIBRARY is large and heavily regex-based, you may want to precompile regex patterns once (e.g. store compiled patterns instead of raw strings) to avoid re-parsing them on every verify_single_task_ocr call and reduce per-call overhead.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In several `expected` lists you are accidentally concatenating adjacent string literals due to missing commas (e.g. `InCharactersMenu` has `r"(?i)Profile" r"(?i)INFO"`, and `Ptory` has `"入る"` followed directly by `r"(?i)ENTER"`), which will change the intended patterns; please add commas between these literals.
- Consider defensively handling non-string or `None` values for `frontend_ocr_text` in `verify_single_task_ocr` (e.g. early return `False` if it's falsy or not a `str`) to avoid unexpected exceptions from `.strip()` or the regex calls.
- Since `MULTILANG_OCR_LIBRARY` is large and heavily regex-based, you may want to precompile regex patterns once (e.g. store compiled patterns instead of raw strings) to avoid re-parsing them on every `verify_single_task_ocr` call and reduce per-call overhead.

## Individual Comments

### Comment 1
<location path="agent/utils/ocr_i18n_lib.py" line_range="334-335" />
<code_context>
+    "InCharactersMenu": {
+        "desc": "在角色菜单内",
+        "expected": [
+            "个人信息", "個人資訊", "信息", "資訊",
+            r"(?i)PROFILE", r"(?i)Profile" r"(?i)INFO", r"(?i)Info",
+            "メッセージ", "情報", "정보"
+        ]
</code_context>
<issue_to_address>
**issue (bug_risk):** Missing comma between the `Profile` and `INFO` patterns causes an unintended concatenated regex.

Python concatenates adjacent string literals, so `r"(?i)Profile" r"(?i)INFO"` becomes `r"(?i)Profile(?i)INFO"`, which won’t behave as two separate patterns. Add a comma after `r"(?i)Profile"` to make them distinct entries in the list.
</issue_to_address>

### Comment 2
<location path="agent/utils/ocr_i18n_lib.py" line_range="561-123" />
<code_context>
+    "Ptory": {
+        "desc": "在钮",
+        "expected": [
+            "进入", "進入", "入る"
+            r"(?i)ENTER", r"(?i)Enter",
+            "들"
+        ]
</code_context>
<issue_to_address>
**issue (bug_risk):** Missing comma after the last CJK string leads to invalid syntax or unintended concatenation.

Add a trailing comma after "入る" so this remains a separate list element and doesn’t get concatenated with the following `r"(?i)ENTER"` string literal.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread agent/utils/ocr_i18n_lib.py Outdated
"desc": "在详情页点进入按钮",
"expected": [
"进入", "進入", "入る",
r"(?i)ENTER", r"(?i)Enter",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): 最后一个 CJK 字符串后缺少逗号,会导致语法错误或意外的字符串拼接。

请在 "入る" 之后补一个逗号,这样它会保持为单独的列表元素,而不会与后面的 r"(?i)ENTER" 字符串字面量拼接在一起。

Original comment in English

issue (bug_risk): Missing comma after the last CJK string leads to invalid syntax or unintended concatenation.

Add a trailing comma after "入る" so this remains a separate list element and doesn’t get concatenated with the following r"(?i)ENTER" string literal.

@Abino01
Copy link
Copy Markdown
Contributor Author

Abino01 commented May 27, 2026

@sourcery-ai guide

@Abino01 Abino01 changed the title 新增ocr參考庫 feat: 新增ocr參考庫 May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant