V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
demonps
V2EX  ›  Python

求推荐中小型项目敏感词检测 Python 案例

  •  
  •   demonps · 2022-05-10 17:02:45 +08:00 · 3875 次点击
    这是一个创建于 709 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前使用 re 去做,样本越来越大,效率逐渐降低很多

    27 条回复    2022-05-11 12:16:16 +08:00
    Mohanson
        1
    Mohanson  
       2022-05-10 17:25:14 +08:00
    一般用字典树
    ila
        2
    ila  
       2022-05-10 17:37:58 +08:00 via Android
    如果是你负责,建议用 bat 的 API 。
    shuax
        3
    shuax  
       2022-05-10 17:55:35 +08:00
    Trie Tree
    demonps
        4
    demonps  
    OP
       2022-05-10 18:06:06 +08:00
    re + trie tree 因为要记录命中的敏感词,需要分组捕获。目前一个场景 build 出来的 pattern 大概有 300w 长,匹配一次感觉快要 1s ,用 bat 的 api 加上请求时间都比本地快好几倍。。。
    demonps
        5
    demonps  
    OP
       2022-05-10 18:08:42 +08:00
    @Mohanson 是的,我是正则字典树去搞的,但是词库太大,有点担忧
    demonps
        6
    demonps  
    OP
       2022-05-10 18:09:43 +08:00
    @ila 也有在用,本地做第一层拦截
    fengjianxinghun
        7
    fengjianxinghun  
       2022-05-10 18:18:33 +08:00
    hyperscan
    documentzhangx66
        8
    documentzhangx66  
       2022-05-10 19:30:14 +08:00
    1.目前主流的做法是,先分词,然后再去匹配敏感词列表。优点是性能高,速度快。缺点是匹配覆盖率低。

    2.但如果要保证匹配覆盖率,运算量必然超大,因为不能分词后再去匹配,而是反过来要用词库来匹配现有内容。可优化的就只有在匹配过程中对字典树进行剪枝了。
    ipwx
        9
    ipwx  
       2022-05-10 19:50:57 +08:00
    LeegoYih
        10
    LeegoYih  
       2022-05-10 19:52:31 +08:00
    我之前写了个 AC 自动机用来匹配和过滤关键词,现在生产环境大概有 1 亿个关键词,性能非常好。
    不过只有 Go 和 Java 版本,看看能不能帮到你,代码实现很简单,照着直接“翻译”成 Python 应该没啥问题。

    Go: https://github.com/yihleego/trie
    Java: https://github.com/yihleego/trie4j
    demonps
        11
    demonps  
    OP
       2022-05-10 20:06:40 +08:00
    @LeegoYih 感谢感谢,我去看看
    acehowxx
        12
    acehowxx  
       2022-05-10 20:34:35 +08:00 via Android
    在意性能可以用布隆过滤器
    demonps
        13
    demonps  
    OP
       2022-05-10 20:42:33 +08:00
    @fengjianxinghun 这个感觉有点子臃肿,好多依赖🤣
    demonps
        14
    demonps  
    OP
       2022-05-10 20:44:14 +08:00
    @documentzhangx66 分词这个方向目前还没考虑到,居家办公比较闲,研究研究
    demonps
        15
    demonps  
    OP
       2022-05-10 20:44:56 +08:00
    @ipwx 测试过 ahocorasick 这个库,感觉差不是太多
    documentzhangx66
        16
    documentzhangx66  
       2022-05-10 21:53:39 +08:00
    @demonps 不用研究,Python 有现成的,叫 结巴分词。
    raycool
        17
    raycool  
       2022-05-10 22:22:11 +08:00
    这应该就是 AC 状态自动机干的事吧
    t2jk4000
        18
    t2jk4000  
       2022-05-10 22:30:46 +08:00
    TimePPT
        19
    TimePPT  
       2022-05-10 23:23:28 +08:00 via Android
    flashtext
    paopjian
        20
    paopjian  
       2022-05-11 01:12:57 +08:00
    https://github.com/intel/hyperscan
    https://www.colm.net/open-source/ragel/
    好像是 v2 以前有个帖子,当时特别震撼
    paopjian
        21
    paopjian  
       2022-05-11 01:14:16 +08:00   ❤️ 1
    fengjianxinghun
        22
    fengjianxinghun  
       2022-05-11 10:05:47 +08:00
    @demonps 这个就是性能最好的,比什么 AC 字典树都快,安全防火墙都用这个代替以前 AC 了
    demonps
        23
    demonps  
    OP
       2022-05-11 10:51:28 +08:00
    @fengjianxinghun 🙏我去研究下这个
    demonps
        24
    demonps  
    OP
       2022-05-11 10:53:32 +08:00
    demonps
        25
    demonps  
    OP
       2022-05-11 10:55:09 +08:00
    @paopjian 都推 hyperscan ,我去测试下😂
    gitgabige
        26
    gitgabige  
       2022-05-11 11:32:08 +08:00
    哎, 作孽啊
    demonps
        27
    demonps  
    OP
       2022-05-11 12:16:16 +08:00
    @gitgabige [叹气]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1568 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 16:57 · PVG 00:57 · LAX 09:57 · JFK 12:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.