Open
fix(layout): 修正序号检测正则的字符集误用,避免时间/比例被判成流程型 #1
待遇安然风Add on 18 days ago
fix(layout): 修正序号检测正则的字符集误用,避免时间/比例被判成流程型 #1
Open
Total: 2 files changed+92-1
| @@ -74,7 +74,8 @@ def detect_sequence(text): | |||
| 74 | r'[1-9][\.、]\s', # 1. 或 1、 | 74 | r'[1-9][\.、]\s', # 1. 或 1、 |
| 75 | r'[一二三四五六七八九十][、\.]\s', # 一、或 一. | 75 | r'[一二三四五六七八九十][、\.]\s', # 一、或 一. |
| 76 | r'第[一二三四五六七八九十\d]+[步阶段环节]', # 第一步 | 76 | r'第[一二三四五六七八九十\d]+[步阶段环节]', # 第一步 |
| 77 | - r'[Step\s]*\d+[::]', # Step 1: | 77 | + r'(?i)\bstep\s*\d+[::]', # Step 1: |
| 78 | + r'(?m)^[ \t]*\d{1,2}[::](?!\d)', # 行首编号 1: / 10: | ||
| 78 | ] | 79 | ] |
| 79 | for pattern in seq_patterns: | 80 | for pattern in seq_patterns: |
| 80 | if re.search(pattern, text): | 81 | if re.search(pattern, text): |
| @@ -0,0 +1,90 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +"""layout_recommender.detect_sequence 回归测试。 | ||
| 4 | + | ||
| 5 | +用法: | ||
| 6 | + python tests/test_layout_recommender.py | ||
| 7 | + | ||
| 8 | +覆盖: | ||
| 9 | + [N] 非序号 时间、比例、比分、端口号不再被判成序号结构(原正则 [Step\\s]*\\d+[::] 会命中) | ||
| 10 | + [S] 序号 真正的 Step 1: / step 2: / 行首 1: 仍能识别 | ||
| 11 | + [C] CLI 端到端跑一遍文本文件,确认版式从 process 变回 data | ||
| 12 | +""" | ||
| 13 | +import json | ||
| 14 | +import os | ||
| 15 | +import subprocess | ||
| 16 | +import sys | ||
| 17 | +import tempfile | ||
| 18 | + | ||
| 19 | +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 20 | +sys.path.insert(0, os.path.join(ROOT, "scripts")) | ||
| 21 | + | ||
| 22 | +from layout_recommender import detect_sequence # noqa: E402 | ||
| 23 | + | ||
| 24 | +FAILS = [] | ||
| 25 | + | ||
| 26 | +NEGATIVE = [ | ||
| 27 | + "全体研发人员会议定于今天 14:30 在多功能厅举行,请各位准时入场。", | ||
| 28 | + "视频演示文稿建议采用标准的 16:9 比例进行播放展示。", | ||
| 29 | + "本轮测试双方比分为 2:1,系统运行平稳。", | ||
| 30 | + "后端调试服务监听端口 8080: 准备接收外部请求。", | ||
| 31 | +] | ||
| 32 | + | ||
| 33 | +POSITIVE = [ | ||
| 34 | + "Step 1: 打开设置", | ||
| 35 | + "step 2:填写账号", | ||
| 36 | + "步骤 Step 3:确认提交", | ||
| 37 | + "1: 首先准备材料", | ||
| 38 | + "10: 第二步执行", | ||
| 39 | +] | ||
| 40 | + | ||
| 41 | + | ||
| 42 | +def check(ok, msg): | ||
| 43 | + print((" [OK] " if ok else " [FAIL] ") + msg) | ||
| 44 | + if not ok: | ||
| 45 | + FAILS.append(msg) | ||
| 46 | + | ||
| 47 | + | ||
| 48 | +def run_unit(): | ||
| 49 | + print("[N] 非序号文本不再命中") | ||
| 50 | + for text in NEGATIVE: | ||
| 51 | + check(not detect_sequence(text), f"detect_sequence=False: {text[:28]}") | ||
| 52 | + print("[S] 真实序号仍能识别") | ||
| 53 | + for text in POSITIVE: | ||
| 54 | + check(detect_sequence(text), f"detect_sequence=True: {text[:28]}") | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +def run_cli(): | ||
| 58 | + print("[C] CLI 端到端") | ||
| 59 | + tmp = tempfile.mkdtemp(prefix="pptx_layout_") | ||
| 60 | + text_path = os.path.join(tmp, "test_notice.txt") | ||
| 61 | + with open(text_path, "w", encoding="utf-8") as f: | ||
| 62 | + f.write(NEGATIVE[0] + "\n") | ||
| 63 | + proc = subprocess.run( | ||
| 64 | + [sys.executable, os.path.join(ROOT, "scripts", "layout_recommender.py"), "--input", text_path], | ||
| 65 | + capture_output=True, text=True, encoding="utf-8", | ||
| 66 | + ) | ||
| 67 | + check(proc.returncode == 0, f"CLI 退出码 0(实际 {proc.returncode})") | ||
| 68 | + if proc.returncode != 0: | ||
| 69 | + print(proc.stderr[:300]) | ||
| 70 | + return | ||
| 71 | + data = json.loads(proc.stdout) | ||
| 72 | + para = data["paragraphs"][0] | ||
| 73 | + check(para["recommended_pattern"] == "data", f"推荐版式为 data(实际 {para['recommended_pattern']})") | ||
| 74 | + check(para["all_scores"]["process"] == 0, f"process 得分为 0(实际 {para['all_scores']['process']})") | ||
| 75 | + check("检测到序号结构" not in para["signals"], "signals 里没有「检测到序号结构」") | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +def main(): | ||
| 79 | + print("== layout_recommender 回归测试") | ||
| 80 | + run_unit() | ||
| 81 | + run_cli() | ||
| 82 | + print() | ||
| 83 | + if FAILS: | ||
| 84 | + print(f"FAILED: {len(FAILS)} 项未通过") | ||
| 85 | + sys.exit(1) | ||
| 86 | + print("全部通过") | ||
| 87 | + | ||
| 88 | + | ||
| 89 | +if __name__ == "__main__": | ||
| 90 | + main() | ||