AC自动机算法-字符串搜索算法:敏感词检测

参考:

https://baike.baidu.com/item/AC自动机算法/22799985

敏感词库参考:https://github.com/konsheng/Sensitive-lexicon

可用于大模型输出内容检测

pip install ahocorasick

复制代码
import ahocorasick
import asyncio
from typing import List, Dict

class AsyncSensitiveChecker:
    """
    高性能异步敏感词检测器 (基于 Aho--Corasick)
    - 支持 async 调用
    - 支持流式文本检测
    """
    def __init__(self, word_file: str):
        self.automaton = ahocorasick.Automaton()
        self._load_words(word_file)
        print(f"✅ 已加载敏感词 {len(self.automaton)} 条")

    def _load_words(self, path: str):
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                word = line.strip()
                if word:
                    self.automaton.add_word(word.lower(), word)
        self.automaton.make_automaton()

    async def check_text_async(self, text: str) -> Dict[str, List[str]]:
        """异步检测单段文本"""
        # 使用 asyncio.to_thread() 避免阻塞主线程
        return await asyncio.to_thread(self._check_sync, text)

    def _check_sync(self, text: str) -> Dict[str, List[str]]:
        """同步检测核心(被线程池调用)"""
        hits = []
        lower = text.lower()
        for _, word in self.automaton.iter(lower):
            hits.append(word)
        return {
            "ok": len(hits) == 0,
            "count": len(hits),
            "hits": list(set(hits))
        }

    async def stream_check(self, text_stream):
        """
        异步流式检测器
        text_stream: 异步生成器( async for chunk in text_stream )
        """
        buffer = ""
        async for chunk in text_stream:
            buffer += chunk
            result = await self.check_text_async(chunk)
            if not result["ok"]:
                print(f"⚠️ 检测到敏感词: {result['hits']}")
                # 可选择立即中断输出
                return {"ok": False, "hits": result["hits"]}
        return {"ok": True, "hits": []}



# import asyncio
# from async_sensitive_checker import AsyncSensitiveChecker

async def main():
    checker = AsyncSensitiveChecker("sensitive_words.txt")

    text = "今天讨论的内容涉及非法交易与敏感主题,色情奔放。"
    result = await checker.check_text_async(text)
    print(f"结果: {result}")
    if not result["ok"]:
        print("⚠️ 发现敏感词:", result["hits"])
    else:
        print("✅ 内容安全")

asyncio.run(main())

"ok": True 表示正常,false表示有敏感词

相关推荐
xrkhy4 小时前
Java全栈面试题及答案汇总(2)
java·开发语言
Xの哲學4 小时前
Linux Netlink全面解析:从原理到实践
linux·网络·算法·架构·边缘计算
Tisfy4 小时前
LeetCode 3289.数字小镇中的捣蛋鬼:哈希表O(n)空间 / 位运算O(1)空间
算法·leetcode·散列表·题解·位运算·哈希表
@LetsTGBot搜索引擎机器人4 小时前
从零打造 Telegram 中文生态:界面汉化 + 中文Bot + @letstgbot 搜索引擎整合实战
开发语言·python·搜索引擎·github·全文检索
2501_938963965 小时前
基于音乐推荐数据的逻辑回归实验报告:曲风特征与用户收听意愿预测
算法·机器学习·逻辑回归
2501_938791225 小时前
逻辑回归正则化解释性实验报告:L2 正则对模型系数收缩的可视化分析
算法·机器学习·逻辑回归
2501_938790075 小时前
逻辑回归正则化参数选择实验报告:贝叶斯优化与网格搜索的效率对比
算法·机器学习·逻辑回归
洲覆5 小时前
缓存异常:缓存穿透、缓存击穿、缓存雪崩
开发语言·数据库·mysql·缓存
2501_938780285 小时前
逻辑回归特征重要性排序实验报告:不同特征选择方法的排序一致性验证
算法·机器学习·逻辑回归