V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 4953 views
    This topic created in 1452 days ago, the information mentioned may be changed or developed.

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

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

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

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