Code Monkey home page Code Monkey logo

trie-tree's Introduction

字典树

简单的字典树模型,用于查找关键词、敏感词等

测试

编写测试类以测试:

public class TrieTreeTest {
    public static void main(String[] args) {
        TrieTreeUtil trieTreeUtil = new TrieTreeUtil();
        trieTreeUtil.add("和平");
        trieTreeUtil.add("未来");
        trieTreeUtil.add("守护");
        trieTreeUtil.add("那咋办嘛");
        
        System.out.println(trieTreeUtil.contains("我的愿望是世界和平~~"));
        System.out.println(trieTreeUtil.contains("技术日新月异,未来会出现什么技术谁也不知道"));
        System.out.println(trieTreeUtil.contains("只见他又又又又又说到:“那咋办嘛”"));
        System.out.println(trieTreeUtil.contains("可口可乐好喝还是百事可乐好喝"));
    }
}

打印如下:

和平
未来
那咋办嘛
null

效率对比

测试设备:2014年macbook pro,i5 4258U,8G

测试参数1:2500关键词,10万次查询:

样本类型 for循环 字典树
前匹配-长度20 1308毫秒 96毫秒
不匹配-长度20 14186毫秒 149毫秒
不匹配-长度100 32715毫秒 556毫秒

测试参数2:56000关键词,10万次查询:

样本类型 for循环 字典树
前匹配-长度20 73088毫秒 132毫秒
不匹配-长度20 225387毫秒 210毫秒
不匹配-长度100 452902毫秒 549毫秒

可以看到在关键词较多时,循环的效率下降非常快,字典树的效率几乎不受关键词数量影响,但他们都受到文本长度影响。

trie-tree's People

Contributors

ligb888 avatar

Stargazers

 avatar  avatar

Watchers

 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.