Code Monkey home page Code Monkey logo

gohs-ladon's Introduction

gohs-Ladon

Build Status
基于Intel开源的hyperscan实现的GO版本的一个服务, 取名ladon(拉冬),希腊神话中表示百头巨龙。给定一行文本,能够从海量的正则表达式中快速查询出命中了哪些正则,还可以返回该正则附加的一些数据。下方有简单的性能压测,5W个正则,匹配上只需要1ms.
使用这个服务,如果你能维护足够完善的正则数据,就能实现一个 伪智能AI,可以理解你的各种语义,然后去做对应的处理。当然,具体能玩成什么样子,就看大家发挥了.

Links

使用示例

例如,给出一个正则文本: 第一列是唯一id, 第二列是正则表达式, 第三列是附加数据

1	^[你|叫|什么|的|是]*名字[你|叫|什么|的|是]*$	{"type:"name", "user":"you"}
2	^[唱|一首|来]*歌[曲|吧|啊]*$	{"type":"song", "name":"random"}
3	^[唱|一首|来]*东风破[的|歌|歌曲|吧|啊]*$	{"type":"song", "name":"东风破"}

启动服务

./gohs-ladon --filepath=patterns/pattern2.txt
[2017-12-20T06:50:50Z] Hs-service 0.0.1 Running on 0.0.0.0:8080

通过服务查询

curl "http://127.0.0.1:8080/?q=你叫什么名字"

返回json,表明命中了第一条正则,并返回了正则文件中的附加数据
{
    "Errno": 0,
        "Msg": "",
        "Data": [
        {
            "Id": 1,
            "From": 0,
            "To": 18,
            "Flags": 0,
            "Context": "你叫什么名字",
            "RegexLinev": {
                "Expr": "^[你|叫|什么|的|是]*名字[你|叫|什么|的|是]*$",
                "Data": "{\"type:\"name\", \"user\":\"you\"}"
            }
        }
    ]
}

Benchmark

比如pttern3.txt中是随机生成了50000个邮箱地址,作为正则表达式,邮箱中的点就表示任意一个字符,所以也算是比较简单的正则了。

49991   [email protected] teststring
49992   [email protected] teststring
49993   [email protected]    teststring
49994   [email protected] teststring
49995   [email protected] teststring
49996   [email protected] teststring
49997   [email protected]    teststring
49998   [email protected] teststring
49999   [email protected] teststring
50000   [email protected]  teststring

比如这条结果,就是一个正则匹配的结果.

以10并发压10w次,得到的结果如下:

可以看到,平均响应时间只有1ms,这还基本都是网络开销,正则查找本身其实只有几十µs。

使用

该库使用需要安装hyperscan的类库,安装步骤比较繁琐,可以参考我hyperscan的一个Docker镜像的Dockerfile进行安装。 如果有Docker环境的话,git clone代码后,可以在代码根目录下直接执行make build,就会将编译完的二进制放在代码根目录下了。

使用./gohs-ladon -h可以查看帮助文档.

$ ./gohs-ladon -h
Gohs-ladon Service 0.0.1

Usage:
gohs-ladon [flags]

Flags:
    --debug             Enable debug mode (default false)
    --filepath string   Dict file path
    --flag string       Regex Flag (default "iou")
-h, --help              help for gohs-ladon
    --port int          Listen port (default 8080)

TODO

  • 增加动态加载字典逻辑。自动检测,当字典文件发生变化时,进行自动build.
  • 完善英文Readme

gohs-ladon's People

Watchers

danhen avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.