You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

"""文本消息解析/清洗"""
from __future__ import annotations
import re
class TextParser:
"""对文本消息内容进行清洗与结构化提取"""
# 常见的 @ 消息模式
_AT_PATTERN = re.compile(r"@(\S+)\s")
# URL 模式
_URL_PATTERN = re.compile(r"https?://\S+")
@staticmethod
def clean(content: str) -> str:
"""去除多余空白与不可见字符"""
content = content.strip()
content = re.sub(r"[\u200b\u200c\u200d\ufeff]", "", content)
return content
@staticmethod
def extract_urls(content: str) -> list[str]:
return TextParser._URL_PATTERN.findall(content)
@staticmethod
def extract_mentions(content: str) -> list[str]:
return TextParser._AT_PATTERN.findall(content)
@staticmethod
def is_system_message(content: str) -> bool:
"""判断是否为系统消息(撤回、入群等)"""
keywords = ["撤回了一条消息", "加入了群聊", "你已添加了", "以上是打招呼的内容"]
return any(kw in content for kw in keywords)