Coder Social home page Coder Social logo

js-sensitivewords's Introduction

说明

提供敏感词检测,替换功能。使用DFA算法,性能优秀

如果是超大的敏感词库,例如 1W 以上,如果对初始化词库时长敏感,可以使用以下方法查看初始化所消耗的时间

console.time("spend time");
sw.addWord("管理员");
console.timeEnd("spend time");

egg 版本

egg 版本见 https://github.com/alex-my/egg-full-sensitivewords

安装

$ npm i js-sensitivewords --save

示例

可以通过sw来调用

测试示例见 test/test-dfa.js

所有接口的示例如下:

const sw = require("js-sensitivewords");

// 初始化敏感词库,建议在启动的时候执行
sw.addWords(["***", "毛主席", "周总理"]);
sw.addWord("管理员");

// 判断是否包含敏感词,返回值 true: 包含; false: 不包含
sw.containsDfa("我爱北京***"); // => true
sw.containsDfa("主席***同志"); // => false

// 获取含有的敏感词内容,返回值 字符串数组
sw.wordsDfa("我爱毛主席和周总理"); // => ['毛主席', '周总理']

// 替换敏感词,当once=true时,对于每一个出现的敏感词,只替换一次。默认 once = false
sw.replaceDfa("我是管理员", "*", true); // => 我是*
// 替换敏感词, 当once=false时,对于每一个出现的敏感词,按照其长度替换。默认 once = false
sw.replaceDfa("我是管理员", "*", false); // => 我是***

License

MIT

js-sensitivewords's People

Contributors

alex-my avatar

Stargazers

Dane avatar 嘿喵-财运旺旺 avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

daudxu

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.