项目介绍
ToolGood.Words是一款高性能的非法词(敏感词)检测组件,由C#语言开发。它不仅具备敏感词检测功能,还提供了繁体简体互换、全角半角互换、获取拼音首字母、获取拼音字母、拼音模糊搜索等额外功能。
主要功能
文件夹和代码结构
- ToolGood.Pinyin.Build:          生成词的拼音
- ToolGood.Pinyin.Pretreatment:   生成拼音预处理,核对拼音,词组最小化
- ToolGood.Transformation.Build:生成简体繁体转换文档,更新时文档放在同一目录下,词库参考 https://github.com/BYVoid/OpenCC
- ToolGood.Words.Contrast:        字符串搜索对比
- ToolGood.Words.Test:            单元测试
非法词(敏感词)检测(字符串搜索)(支持通配符)
- 非法词(敏感词)检测类:StringMatch、StringMatchEx、WordsMatch、WordsMatchEx。
- 支持部分正则表达式类型:.(点)?(问号)  (|)(括号与竖线)
    string s = ".[中美]国|国人|zg人";
    string test = "我是中国人";
    WordsMatch wordsSearch = new WordsMatch();
    wordsSearch.SetKeywords(s.Split('|'));
    var b = wordsSearch.ContainsAny(test);
    Assert.AreEqual(true, b);
    var f = wordsSearch.FindFirst(test);
    Assert.AreEqual("是中国", f.Keyword);
    var alls = wordsSearch.FindAll(test);
    Assert.AreEqual("是中国", alls[0].Keyword);
    Assert.AreEqual(".[中美]国", alls[0].MatchKeyword);
    Assert.AreEqual(1, alls[0].Start);
    Assert.AreEqual(3, alls[0].End);
    Assert.AreEqual(0, alls[0].Index);//返回索引Index,默认从0开始
    Assert.AreEqual("国人", alls[1].Keyword);
    Assert.AreEqual(2, alls.Count);
    var t = wordsSearch.Replace(test, '*');
    Assert.AreEqual("我****", t);
繁体简体互换
// 转成简体
    WordsHelper.ToSimplifiedChinese("我愛中國");
    WordsHelper.ToSimplifiedChinese("我愛中國",1);// 港澳繁体 转 简体
    WordsHelper.ToSimplifiedChinese("我愛中國",2);// 台湾正体 转 简体
    // 转成繁体
    WordsHelper.ToTraditionalChinese("我爱中国");
    WordsHelper.ToTraditionalChinese("我爱中国",1);// 简体 转 港澳繁体
    WordsHelper.ToTraditionalChinese("我爱中国",2);// 简体 转 台湾正体
全角半角互换
// 转成全角
    WordsHelper.ToSBC("abcABC123");
    // 转成半角
    WordsHelper.ToDBC("abcABC123");
数字转成中文大写
    // 数字转成中文大写
    WordsHelper.ToChineseRMB(12345678901.12);
    // 中文转成数字
    WordsHelper.ToNumber("壹佰贰拾叁亿肆仟伍佰陆拾柒万捌仟玖佰零壹元壹角贰分");
拼音操作
// 获取全拼
    WordsHelper.GetPinyin("我爱中国");//WoAiZhongGuo   
    WordsHelper.GetPinyin("我爱中国",",");//Wo,Ai,Zhong,Guo   
    WordsHelper.GetPinyin("我爱中国",true);//WǒÀiZhōngGuó
    // 获取首字母
    WordsHelper.GetFirstPinyin("我爱中国");//WAZG
    // 获取全部拼音
    WordsHelper.GetAllPinyin('传');//Chuan,Zhuan
    // 获取姓名
    WordsHelper.GetPinyinForName("单一一")//ShanYiYi
    WordsHelper.GetPinyinForName("单一一",",")//Shan,Yi,Yi
    WordsHelper.GetPinyinForName("单一一",true)//ShànYīYī
开源地址
https://github.com/toolgood/ToolGood.Words
该文章在 2024/7/24 9:08:17 编辑过