項(xiàng)目介紹
ToolGood.Words是一款高性能的非法詞(敏感詞)檢測(cè)組件,由C#語(yǔ)言開(kāi)發(fā)。它不僅具備敏感詞檢測(cè)功能,還提供了繁體簡(jiǎn)體互換、全角半角互換、獲取拼音首字母、獲取拼音字母、拼音模糊搜索等額外功能。
主要功能
文件夾和代碼結(jié)構(gòu)
- ToolGood.Pinyin.Build: 生成詞的拼音
- ToolGood.Pinyin.Pretreatment: 生成拼音預(yù)處理,核對(duì)拼音,詞組最小化
- ToolGood.Transformation.Build:生成簡(jiǎn)體繁體轉(zhuǎn)換文檔,更新時(shí)文檔放在同一目錄下,詞庫(kù)參考 https://github.com/BYVoid/OpenCC
- ToolGood.Words.Contrast: 字符串搜索對(duì)比
- ToolGood.Words.Test: 單元測(cè)試
- ToolGood.Words: 本項(xiàng)目源代碼
非法詞(敏感詞)檢測(cè)(字符串搜索)(支持通配符)
- 非法詞(敏感詞)檢測(cè)類:StringMatch、StringMatchEx、WordsMatch、WordsMatchEx。
- 支持部分正則表達(dá)式類型:.(點(diǎn))?(問(wèn)號(hào)) (|)(括號(hào)與豎線)
string s = ".[中美]國(guó)|國(guó)人|zg人";
string test = "我是中國(guó)人";
WordsMatch wordsSearch = new WordsMatch();
wordsSearch.SetKeywords(s.Split('|'));
var b = wordsSearch.ContainsAny(test);
Assert.AreEqual(true, b);
var f = wordsSearch.FindFirst(test);
Assert.AreEqual("是中國(guó)", f.Keyword);
var alls = wordsSearch.FindAll(test);
Assert.AreEqual("是中國(guó)", alls[0].Keyword);
Assert.AreEqual(".[中美]國(guó)", alls[0].MatchKeyword);
Assert.AreEqual(1, alls[0].Start);
Assert.AreEqual(3, alls[0].End);
Assert.AreEqual(0, alls[0].Index);//返回索引Index,默認(rèn)從0開(kāi)始
Assert.AreEqual("國(guó)人", alls[1].Keyword);
Assert.AreEqual(2, alls.Count);
var t = wordsSearch.Replace(test, '*');
Assert.AreEqual("我****", t);
繁體簡(jiǎn)體互換
// 轉(zhuǎn)成簡(jiǎn)體
WordsHelper.ToSimplifiedChinese("我愛(ài)中國(guó)");
WordsHelper.ToSimplifiedChinese("我愛(ài)中國(guó)",1);// 港澳繁體 轉(zhuǎn) 簡(jiǎn)體
WordsHelper.ToSimplifiedChinese("我愛(ài)中國(guó)",2);// 臺(tái)灣正體 轉(zhuǎn) 簡(jiǎn)體
// 轉(zhuǎn)成繁體
WordsHelper.ToTraditionalChinese("我愛(ài)中國(guó)");
WordsHelper.ToTraditionalChinese("我愛(ài)中國(guó)",1);// 簡(jiǎn)體 轉(zhuǎn) 港澳繁體
WordsHelper.ToTraditionalChinese("我愛(ài)中國(guó)",2);// 簡(jiǎn)體 轉(zhuǎn) 臺(tái)灣正體
全角半角互換
// 轉(zhuǎn)成全角
WordsHelper.ToSBC("abcABC123");
// 轉(zhuǎn)成半角
WordsHelper.ToDBC("abcABC123");
數(shù)字轉(zhuǎn)成中文大寫
// 數(shù)字轉(zhuǎn)成中文大寫
WordsHelper.ToChineseRMB(12345678901.12);
// 中文轉(zhuǎn)成數(shù)字
WordsHelper.ToNumber("壹佰貳拾叁億肆仟伍佰陸拾柒萬(wàn)捌仟玖佰零壹元壹角貳分");
拼音操作
// 獲取全拼
WordsHelper.GetPinyin("我愛(ài)中國(guó)");//WoAiZhongGuo
WordsHelper.GetPinyin("我愛(ài)中國(guó)",",");//Wo,Ai,Zhong,Guo
WordsHelper.GetPinyin("我愛(ài)中國(guó)",true);//WǒÀiZhōngGuó
// 獲取首字母
WordsHelper.GetFirstPinyin("我愛(ài)中國(guó)");//WAZG
// 獲取全部拼音
WordsHelper.GetAllPinyin('傳');//Chuan,Zhuan
// 獲取姓名
WordsHelper.GetPinyinForName("單一一")//ShanYiYi
WordsHelper.GetPinyinForName("單一一",",")//Shan,Yi,Yi
WordsHelper.GetPinyinForName("單一一",true)//ShànYīYī
開(kāi)源地址
https://github.com/toolgood/ToolGood.Words
該文章在 2024/7/24 9:08:17 編輯過(guò)