<h1 align="center">Welcome to mint-filter 👋</h1>
<p align="center">
<a href="https://github.com/ZhelinCheng/mint-filter" target="_blank">
<img alt="GitHub package.json version" src="https://img.shields.io/github/package-json/v/ZhelinCheng/mint-filter.svg">
</a>
<a href="https://www.npmjs.com/package/mint-filter" target="_blank">
<img alt="Version" src="https://img.shields.io/npm/v/mint-filter.svg">
</a>
<a href="https://www.npmjs.com/package/mint-filter" target="_blank">
<img alt="Download" src="https://img.shields.io/npm/dm/mint-filter.svg">
</a>
<a href="https://coveralls.io/github/ZhelinCheng/mint-filter?branch=master" target="_blank">
<img alt="Coverage" src="https://coveralls.io/repos/github/ZhelinCheng/mint-filter/badge.svg?branch=master">
</a>
<br/>
<a href="https://github.com/ZhelinCheng/mint-filter/actions" target="_blank">
<img alt="Coverage" src="https://github.com/ZhelinCheng/mint-filter/workflows/CI/badge.svg">
</a>
<a href="https://github.com/ZhelinCheng/mint-filter#readme" target="_blank">
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
</a>
<a href="https://github.com/ZhelinCheng/mint-filter/graphs/commit-activity" target="_blank">
<img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-green.svg" />
</a>
<a href="https://github.com/ZhelinCheng/mint-filter/blob/master/LICENSE" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/github/license/ZhelinCheng/mint-filter" />
</a>
</p>
> 基于Aho–Corasick算法实现的敏感词过滤方案,Aho–Corasick算法是由Alfred V. Aho和Margaret J.Corasick 发明的字符串搜索算法,用于在输入的一串字符串中匹配有限组“字典”中的子串。它与普通字符串匹配的不同点在于同时与所有字典串进行匹配。算法均摊情况下具有近似于线性的时间复杂度,约为字符串的长度加所有匹配的数量。
实现详细说明(搜索算法未更新,请查看代码):
个人博客:[《TypeScript:Aho–Corasick算法实现敏感词过滤》](https://zhelin.me/post/47627553bd09576fbdeafc11dc93bfbf/)
掘金社区:[《TypeScript:Aho–Corasick算法实现敏感词过滤》](https://juejin.im/post/5cfa6bb6f265da1b8a4f0ed8)
### 🏠 [Homepage](https://github.com/ZhelinCheng/mint-filter#readme)
## 1. 性能
### 运行性能
**需要注意的是,实际生产环境运行速度会比下面测试数据更快。**
使用20000个随机敏感词实例化的平均时间:< 96ms
测试字符串包含随机生成的汉字、字母、数字。
以下测试均在20000个随机敏感词构建的树下进行测试,每组测试6次取平均值:
| 编号 | 字符串长度 | 不替换敏感词[replace:false] | 替换敏感词 |
| :--------: | :-----: | :----: | :----: |
| 1 | 1000 | < 1.35ms | < 1.55ms |
| 2 | 5000 | < 3.60ms | < 3.60ms |
| 3 | 10000 | < 8.10ms | < 9.81ms |
| 4 | 20000 | < 15.03ms | < 16.03ms |
| 5 | 50000 | < 20.83ms | < 21.18ms |
| 6 | 100000 | < 29.02ms | < 34.45ms |
### 与DFA算法对比
| | Aho-Corasick算法 | DFA算法 |
|---------|----------------|--------|
| 算法效率 | 多模式串时表现良好 | 单模式串时表现良好 |
| 内存占用 | 需要更多内存空间 | 内存消耗小 |
| 匹配速度 | 非常快,具有线性时间复杂度 | 常量时间复杂度,匹配速度受限于自动机大小 |
| 实现难度 | 较为复杂,需要构建AC自动机并进行状态转移 | 实现简单,只需维护一个大的状态转移表 |
| 匹配多个字符串 | 高效,可同时搜索多个模式字符串 | 必须为每个字符串单独执行匹配操作 |
| 错误容错性 | 良好 | 不太好 |
## 2. 安装
```sh
yarn add mint-filter
```
## 3. 使用
### CommonJS导入
```javascript
const { Mint } = require('mint-filter')
```
### TypeScript / ES Module引用
```typescript
import Mint from 'mint-filter'
const mint = new Mint(['敏感词数组'])
// 基本使用
mint.filter('需要验证的文本')
```
## 4. 构造函数
### constructor
• **new Mint**(`keys`)
#### 参数
| Name | Type |
| :------ | :------ |
| `keys` | `string`[] |
#### 定义于
[index.ts:26](https://github.com/ZhelinCheng/mint-filter/blob/f25e001/src/index.ts#L26)
## 5. 方法
### add
▸ **add**(`key`, `build?`): `boolean`
新增敏感词
**`Example`**
```typescript
const status = mint.add('敏感词')
```
#### 参数
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `key` | `string` | `undefined` | 关键词 |
| `build` | `boolean` | `true` | 是否构建树,默认不用传递 |
#### 返回
`boolean`
状态
#### 定义于
[index.ts:233](https://github.com/ZhelinCheng/mint-filter/blob/f25e001/src/index.ts#L233)
___
### delete
▸ **delete**(`key`): ``"update"`` \| ``"delete"``
删除关键字
**`Example`**
```typescript
const status = mint.delete('敏感词')
```
#### 参数
| Name | Type | Description |
| :------ | :------ | :------ |
| `key` | `string` | 关键词 |
#### 返回
``"update"`` \| ``"delete"``
状态(update | delete),告知用户是删除了树上的节点还是单纯的更新了节点
#### 定义于
[index.ts:169](https://github.com/ZhelinCheng/mint-filter/blob/f25e001/src/index.ts#L169)
___
### filter
▸ **filter**(`text`, `options?`): `FilterData`
过滤文本
**`Example`**
```typescript
mint.add('无法通过')
let status = mint.filter('这是一句无法通过的文本')
console.log(status) // { words: ["无法通过"], text: "这是一句****的文本" }
status = mint.filter('这是一句无法通过的文本', { replace: false })
console.log(status) // { words: ["无法通过"], text: "这是一句无法通过的文本" }
```
#### 参数
| Name | Type | Description |
| :------ | :------ | :------ |
| `text` | `string` | 文本内容 |
| `options?` | `Pick`<`FilterOptions`, ``"replace"``\> | - |
#### 返回
`FilterData`
FilterData
#### 定义于
[index.ts:134](https://github.com/ZhelinCheng/mint-filter/blob/f25e001/src/index.ts#L134)
___
### verify
▸ **verify**(`text`): `boolean`
检测文本是否通过验证
**`Example`**
```typescript
mint.add('无法通过')
const status = mint.verify('这是一句无法通过的文本')
console.log(status) // false
```
#### 参数
| Name | Type | Description |
| :------ | :------ | :------ |
| `text` | `string` | 文本内容 |
#### 返回
`boolean`
Boolean
#### 定义于
[index.ts:152](https://github.com/ZhelinCheng/mint-filter/blob/f25e001/src/index.ts#L152)
## 6. 测试
```sh
yarn run test
```
## 7. 作者
👤 **ZhelinCheng**
* Website: https://zhelin.me
* Github: [@ZhelinCheng](https://github.com/ZhelinCheng)
### 🤝 Contributing
Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/ZhelinCheng/mint-filter/issues). You can also take a look at the [contributing guide](https://github.com/ZhelinCheng/mint-filter/blob/master/CONTRIBUTING.md).
### Show your support
Give a ⭐️ if this project helped you!
### 📝 License
Copyright © 2023 [ZhelinCheng](https://github.com/ZhelinCheng).<br />
This project is [MIT](https://github.com/ZhelinCheng/mint-filter/blob/master/LICENSE) licensed.
没有合适的资源?快使用搜索试试~ 我知道了~
基于Aho–Corasick算法,更轻巧的JavaScript敏感词过滤库,更好的容错性,适用Node及浏览器环境
共36个文件
txt:7个
md:5个
json:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 27 浏览量
2024-05-08
20:25:22
上传
评论
收藏 587KB ZIP 举报
温馨提示
性能 运行性能 需要注意的是,实际生产环境运行速度会比下面测试数据更快。 使用20000个随机敏感词实例化的平均时间:< 96ms 测试字符串包含随机生成的汉字、字母、数字。 以下测试均在20000个随机敏感词构建的树下进行测试,每组测试6次取平均值: 编号 字符串长度 不替换敏感词[replace:false] 替换敏感词 1 1000 < 1.35ms < 1.55ms 2 5000 < 3.60ms < 3.60ms 3 10000 < 8.10ms < 9.81ms 4 20000 < 15.03ms < 16.03ms 5 50000 < 20.83ms < 21.18ms 6 100000 < 29.02ms < 34.45ms 与DFA算法对比 Aho-Corasick算法 DFA算法 算法效率 多模式串时表现良好 单模式串时表现良好 内存占用 需要更多内存空间 内存消耗小 匹配速度 非常快,具有线性时间复杂度 常量时间复杂度,匹配速度受限于自动机大小 实现难度 较为复杂,需要构建AC自动机并进行状态转移 实现简单,只需维护一个大的状态转移表 匹配多个字符串 高效,
资源推荐
资源详情
资源评论
收起资源包目录
mint-filter-master.zip (36个子文件)
mint-filter-master
.editorconfig 244B
yarn.lock 195KB
nodemon.json 224B
time
100000.txt 285KB
50000.txt 137KB
keywords.txt 172KB
20000.txt 55KB
10000.txt 27KB
1000.txt 2KB
index.js 1KB
5000.txt 13KB
_README.md 6KB
.prettierrc 191B
.github
workflows
test.yml 515B
jest.config.js 477B
src
node.ts 716B
index.ts 7KB
index.test.ts 4KB
LICENSE 1KB
.husky
prepare-commit-msg 106B
.gitignore 2B
pre-commit 130B
commitlint.config.js 944B
.prettierignore 49B
docs
modules.md 118B
classes
default.md 4KB
.nojekyll 143B
README.md 3KB
package.json 2KB
.eslintrc.json 504B
.npmignore 258B
tsconfig.module.json 232B
.gitignore 1KB
.eslintignore 51B
tsconfig.json 2KB
README.md 7KB
共 36 条
- 1
资源评论
hakesashou
- 粉丝: 4312
- 资源: 1135
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功