feat: 新增ocr參考庫 #221
Conversation
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
File-Level Changes
提示与命令与 Sourcery 交互
自定义使用体验打开你的 dashboard:
获取帮助Original review guide in EnglishReviewer's GuideIntroduce 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_ocrflowchart 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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English
Hey - I've found 2 issues, and left some high level feedback:
- In several
expectedlists you are accidentally concatenating adjacent string literals due to missing commas (e.g.InCharactersMenuhasr"(?i)Profile" r"(?i)INFO", andPtoryhas"入る"followed directly byr"(?i)ENTER"), which will change the intended patterns; please add commas between these literals. - Consider defensively handling non-string or
Nonevalues forfrontend_ocr_textinverify_single_task_ocr(e.g. early returnFalseif it's falsy or not astr) to avoid unexpected exceptions from.strip()or the regex calls. - Since
MULTILANG_OCR_LIBRARYis 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 everyverify_single_task_ocrcall 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| "desc": "在详情页点进入按钮", | ||
| "expected": [ | ||
| "进入", "進入", "入る", | ||
| r"(?i)ENTER", r"(?i)Enter", |
There was a problem hiding this comment.
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.
|
@sourcery-ai guide |
新增ocr_i18n_lib.py做為未來的ocr文字判定對應庫 不再透過assets/resource/base/pipeline/內的.json內容作判定 .json之後只作為roi區域讀取跟判定結果的操作 不參與文字檢驗跟判定
舉例來說
更動.json前
更動.json後
可以大幅降低未來維護的時間
路徑 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: