推荐学习书目
› 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 · May 10, 2022 · 5521 views
    This topic created in 1637 days ago, the information mentioned may be changed or developed.

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

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

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

    Go: https://github.com/yihleego/trie
    Java: https://github.com/yihleego/trie4j
    demonps
    demonps
    OP
    May 10, 2022
    11
    @LeegoYih 感谢感谢,我去看看
    acehowxx
    acehowxx
    May 10, 2022 via Android
    12
    在意性能可以用布隆过滤器
    demonps
    demonps
    OP
    May 10, 2022
    13
    @fengjianxinghun 这个感觉有点子臃肿,好多依赖🤣
    demonps
    demonps
    OP
    May 10, 2022
    14
    @documentzhangx66 分词这个方向目前还没考虑到,居家办公比较闲,研究研究
    demonps
    demonps
    OP
    May 10, 2022
    15
    @ipwx 测试过 ahocorasick 这个库,感觉差不是太多
    documentzhangx66
    documentzhangx66
    May 10, 2022
    16
    @demonps 不用研究,Python 有现成的,叫 结巴分词。
    raycool
    raycool
    May 10, 2022
    17
    这应该就是 AC 状态自动机干的事吧
    t2jk4000
    t2jk4000
    May 10, 2022
    18
    TimePPT
    TimePPT
    May 10, 2022 via Android
    19
    flashtext
    paopjian
    paopjian
    May 11, 2022
    20
    https://github.com/intel/hyperscan
    https://www.colm.net/open-source/ragel/
    好像是 v2 以前有个帖子,当时特别震撼
    paopjian
    paopjian
    May 11, 2022❤️1
    21
    fengjianxinghun
    fengjianxinghun
    May 11, 2022
    22
    @demonps 这个就是性能最好的,比什么 AC 字典树都快,安全防火墙都用这个代替以前 AC 了
    demonps
    demonps
    OP
    May 11, 2022
    23
    @fengjianxinghun 🙏我去研究下这个
    demonps
    demonps
    OP
    May 11, 2022
    24
    demonps
    demonps
    OP
    May 11, 2022
    25
    @paopjian 都推 hyperscan ,我去测试下😂
    gitgabige
    gitgabige
    May 11, 2022
    26
    哎, 作孽啊
    demonps
    demonps
    OP
    May 11, 2022
    27
    @gitgabige [叹气]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1508 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 23:01 · PVG 07:01 · LAX 15:01 · JFK 18:01
    ♥ Do have faith in what you're doing.