敏感词过滤器
More...
#include <tools_filterword.h>
|
| static void | init () |
| | 初始化过滤器
|
| static void | clear () |
| | 释放所有Trie和辅助状态
|
| static void | load (const std::string &apattern) |
| | 向Trie中插入一个UTF-8模式
|
| static void | build () |
| | 构建失败链接和输出链接
|
| static bool | match (char c, int &cur, int i, std::vector< std::pair< int, int > > &res) |
| | 单字符匹配推进
|
| static std::vector< std::pair< int, int > > | match (const std::string &text) |
| | 返回每个匹配的起始位置和长度
|
| static std::string | filter (const std::string &text) |
| | 将每个匹配的字节范围替换为 '*'
|
| static bool | utf8to32 (const std::string &text1, std::u32string &text2) |
| | 将UTF-8文本转换为UTF-32码点
|
| static bool | utf32to8 (const std::u32string &text1, std::string &text2) |
| | 将UTF-32码点转换回UTF-8文本
|
| static bool | is_filter (const std::string &text) |
| | 检测文本是否包含敏感词
|
| static int32_t | charcount (const std::u32string &astr) |
| | 计算显示字符数
|
| static const char * | enfilter_message (enfilter astat) |
| | 获取过滤结果描述信息
|
| static enfilter | check_name (const std::string &astr, int32_t amincount, int32_t amaxcount) |
| | 验证面向用户的名称
|
| static bool | is_language_char (uint32_t codepoint) |
| | 检查码点是否为语言字符
|
| static bool | is_emojispecial (char32_t codepoint) |
| | 检查码点是否为特殊emoji
|
| static bool | is_emojispecial (const std::u32string &astr) |
| | 检查UTF-32字符串是否包含特殊emoji
|
敏感词过滤器
基于 Aho-Corasick 自动机实现的高效敏感词检测和过滤系统。 支持 UTF-8 文本处理、emoji 特殊字符检测、名称验证等功能。
- Note
- 所有方法均为静态方法,禁止实例化。
◆ enfilter
过滤结果枚举
| Enumerator |
|---|
| enfilter_success | 验证通过
|
| enfilter_invalid_utf8 | 输入不是有效的UTF-8
|
| enfilter_emojispecial | 输入包含不允许的表情或符号码点
|
| enfilter_charcount | 输入长度超出配置的字符范围
|
| enfilter_filter | 输入匹配到已配置的敏感词
|
◆ build()
| void ngl::tools::filterword::build |
( |
| ) |
|
|
static |
构建失败链接和输出链接
构建Aho-Corasick自动机
所有模式加载后,必须调用此方法构建自动机。
计算失败链接和输出链接,完成自动机构建
- Note
- 必须在所有load()调用之后调用
◆ charcount()
| int32_t ngl::tools::filterword::charcount |
( |
const std::u32string & | astr | ) |
|
|
static |
计算显示字符数
计算UTF-32字符串的显示字符数
- Parameters
-
- Returns
- 显示字符数
使用项目的混合宽度命名规则计算显示字符数。
ASCII字符计1,非ASCII字符计2(宽字符)
- Parameters
-
- Returns
- 显示字符数
◆ check_name()
| filterword::enfilter ngl::tools::filterword::check_name |
( |
const std::string & | astr, |
|
|
int32_t | amincount, |
|
|
int32_t | amaxcount ) |
|
static |
验证面向用户的名称
检查角色名合法性
- Parameters
-
| astr | 待验证的名称 |
| amincount | 最小字符数 |
| amaxcount | 最大字符数 |
- Returns
- 验证结果
根据UTF-8、特殊字符、长度和过滤规则验证名称。
依次验证UTF-8编码、特殊字符、字符数、敏感词
- Parameters
-
| astr | 角色名字符串(UTF-8编码) |
| amincount | 最小字符数 |
| amaxcount | 最大字符数 |
- Returns
- 检查结果状态码
◆ clear()
| void ngl::tools::filterword::clear |
( |
| ) |
|
|
static |
释放所有Trie和辅助状态
清空自动机
释放所有节点并重置状态
◆ enfilter_message()
获取过滤结果描述信息
获取过滤状态的描述消息
- Parameters
-
- Returns
- 描述信息字符串
- Parameters
-
- Returns
- 状态描述字符串
◆ filter()
| std::string ngl::tools::filterword::filter |
( |
const std::string & | text | ) |
|
|
static |
将每个匹配的字节范围替换为 '*'
过滤文本中的敏感词
- Parameters
-
- Returns
- 过滤后的文本
将匹配到的敏感词替换为'*'字符
- Parameters
-
- Returns
- 过滤后的文本
◆ init()
| void ngl::tools::filterword::init |
( |
| ) |
|
|
static |
初始化过滤器
初始化Aho-Corasick自动机
构建/重置自动机,加载模式并查询/过滤文本。 必须在调用其他方法前调用。
创建根节点并初始化快速跳转表
◆ is_emojispecial() [1/2]
| bool ngl::tools::filterword::is_emojispecial |
( |
char32_t | codepoint | ) |
|
|
static |
检查码点是否为特殊emoji
判断UTF-32码点是否为Emoji或特殊符号
- Parameters
-
- Returns
- 当一个码点属于不允许的表情/符号区块时返回true
检测控制字符、标点、Emoji、数学符号、箭头等不应出现在名称中的字符
- Parameters
-
- Return values
-
| true | 是Emoji或特殊符号 |
| false | 不是(允许的字符) |
◆ is_emojispecial() [2/2]
| bool ngl::tools::filterword::is_emojispecial |
( |
const std::u32string & | astr | ) |
|
|
static |
检查UTF-32字符串是否包含特殊emoji
判断UTF-32字符串是否包含Emoji或特殊符号
- Parameters
-
- Returns
- 包含不允许的字符返回true
- Parameters
-
- Return values
-
| true | 包含Emoji或特殊符号 |
| false | 不包含 |
◆ is_filter()
| bool ngl::tools::filterword::is_filter |
( |
const std::string & | text | ) |
|
|
static |
检测文本是否包含敏感词
- Parameters
-
- Returns
- 包含敏感词返回true
逐字节推进自动机状态,跳过特殊字符
- Parameters
-
- Return values
-
◆ is_language_char()
| bool ngl::tools::filterword::is_language_char |
( |
uint32_t | codepoint | ) |
|
|
static |
检查码点是否为语言字符
判断Unicode码点是否为合法语言字符
- Parameters
-
- Returns
- 对于名称验证视为普通语言字母的码点返回true
支持拉丁字母、CJK汉字、日文假名、韩文、西里尔、希腊、阿拉伯字母
- Parameters
-
- Return values
-
◆ load()
| void ngl::tools::filterword::load |
( |
const std::string & | apattern | ) |
|
|
static |
向Trie中插入一个UTF-8模式
加载敏感词到Trie树
- Parameters
-
将单个敏感词插入Trie树中
- Parameters
-
◆ match() [1/2]
| bool ngl::tools::filterword::match |
( |
char | c, |
|
|
int & | cur, |
|
|
int | i, |
|
|
std::vector< std::pair< int, int > > & | res ) |
|
static |
单字符匹配推进
单字符匹配推进并收集结果
- Parameters
-
| c | 当前字符 |
| cur | 当前状态(会被更新) |
| i | 当前字符位置 |
| res | 匹配结果存储 |
- Returns
- 存在匹配返回true
- Parameters
-
| c | 输入字符 |
| cur | [in/out] 当前状态 |
| i | 字符位置 |
| res | [out] 匹配结果 |
- Return values
-
◆ match() [2/2]
| std::vector< std::pair< int, int > > ngl::tools::filterword::match |
( |
const std::string & | text | ) |
|
|
static |
返回每个匹配的起始位置和长度
文本敏感词匹配
- Parameters
-
- Returns
- 匹配结果数组,每个元素为 <起始位置, 长度>
对整个文本进行敏感词扫描,返回所有匹配位置
- Parameters
-
- Returns
- 匹配结果列表(起始位置, 长度)
◆ utf32to8()
| bool ngl::tools::filterword::utf32to8 |
( |
const std::u32string & | text1, |
|
|
std::string & | text2 ) |
|
static |
将UTF-32码点转换回UTF-8文本
UTF-32转UTF-8.
- Parameters
-
| text1 | 输入UTF-32文本 |
| text2 | 输出UTF-8文本 |
- Returns
- 转换成功返回true
- Parameters
-
| text1 | UTF-32编码字符串 |
| text2 | [out] UTF-8编码字符串 |
- Return values
-
◆ utf8to32()
| bool ngl::tools::filterword::utf8to32 |
( |
const std::string & | text1, |
|
|
std::u32string & | text2 ) |
|
static |
将UTF-8文本转换为UTF-32码点
UTF-8转UTF-32.
- Parameters
-
| text1 | 输入UTF-8文本 |
| text2 | 输出UTF-32文本 |
- Returns
- 转换成功返回true
- Parameters
-
| text1 | UTF-8编码字符串 |
| text2 | [out] UTF-32编码字符串 |
- Return values
-
| true | 转换成功 |
| false | 转换失败(非法UTF-8序列) |
The documentation for this class was generated from the following files: