File size: 885 Bytes
bdcdaf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
文本清理核心逻辑 - 供 mcp.py 和 api.py 共用
"""

import re


async def do_clean(text: str, operations: list) -> dict:
    """执行文本清理操作。

    Args:
        text: 待清理的文本
        operations: 清理操作列表(trim、remove_empty_lines、normalize_spaces等)

    Returns:
        包含清理结果的字典
    """
    result = text

    for op in operations:
        if op == "trim":
            result = result.strip()
        elif op == "remove_empty_lines":
            result = re.sub(r'\n\s*\n', '\n', result)
        elif op == "normalize_spaces":
            result = re.sub(r'[ \t]+', ' ', result)
        elif op == "remove_extra_newlines":
            result = re.sub(r'\n+', '\n', result)

    return {
        "success": True,
        "output": result,
        "message": f"已执行 {len(operations)} 个清理操作"
    }