描述
一款高性能敏感詞(非法詞/臟字)檢測過濾組件,附帶繁體簡體互換,支持全角半角互換,漢字轉(zhuǎn)拼音,模糊搜索等功能。
同時(shí)支持多種語言,C#、golang、java、javascript、python
參數(shù)
來源:GitHub(3100) https://github.com/toolgood/ToolGood.Words
示例
非法詞(敏感詞)檢測類:StringMatch、StringMatchEx、WordsMatch、WordsMatchEx。
支持部分正則表達(dá)式類型:.(點(diǎn))?(問號) [](方括號) (|)(括號與豎線)
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,默認(rèn)從0開始 Assert.AreEqual(“國人”, alls[1].Keyword); Assert.AreEqual(2, alls.Count); var t = wordsSearch.Replace(test, ‘*’); Assert.AreEqual(“我****”, t);
繁體簡體互換、全角半角互換、數(shù)字轉(zhuǎn)成中文大寫、拼音操作
// 轉(zhuǎn)成簡體 WordsHelper.ToSimplifiedChinese(“我愛中國”); WordsHelper.ToSimplifiedChinese(“我愛中國”,1);// 港澳繁體 轉(zhuǎn) 簡體 WordsHelper.ToSimplifiedChinese(“我愛中國”,2);// 臺灣正體 轉(zhuǎn) 簡體 // 轉(zhuǎn)成繁體 WordsHelper.ToTraditionalChinese(“我愛中國”); WordsHelper.ToTraditionalChinese(“我愛中國”,1);// 簡體 轉(zhuǎn) 港澳繁體 WordsHelper.ToTraditionalChinese(“我愛中國”,2);// 簡體 轉(zhuǎn) 臺灣正體 // 轉(zhuǎn)成全角 WordsHelper.ToSBC(“abcABC123”); // 轉(zhuǎn)成半角 WordsHelper.ToDBC(“abcABC123”); // 數(shù)字轉(zhuǎn)成中文大寫 WordsHelper.ToChineseRMB(12345678901.12); // 中文轉(zhuǎn)成數(shù)字 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ī
PinyinMatch:方法有SetKeywords、SetIndexs、Find、FindIndex。
PinyinMatch:方法有SetKeywordsFunc、SetPinyinFunc、SetPinyinSplitChar、Find。
string s = “北京|天津|河北|遼寧|吉林|黑龍江|山東|江蘇|上海|浙江|安徽|福建|江西|廣東|廣西|海南|河南|湖南|湖北|山西|內(nèi)蒙古|寧夏|青海|陜西|甘肅|新疆|四川|貴州|云南|重慶|西藏|香港|澳門|臺灣”; PinyinMatch match = new PinyinMatch(); match.SetKeywords(s.Split(‘|’).ToList()); var all = match.Find(“BJ”); Assert.AreEqual(“北京”, all[0]); Assert.AreEqual(1, all.Count); all = match.Find(“北J”); Assert.AreEqual(“北京”, all[0]); Assert.AreEqual(1, all.Count); all = match.Find(“北Ji”); Assert.AreEqual(“北京”, all[0]); Assert.AreEqual(1, all.Count); all = match.Find(“S”); Assert.AreEqual(“山東”, all[0]); Assert.AreEqual(“江蘇”, all[1]); var all2 = match.FindIndex(“BJ”); Assert.AreEqual(0, all2[0]); Assert.AreEqual(1, all.Count);
結(jié)尾
一款支持多語言的敏感詞檢測過濾組件,同時(shí)支持一些拼音、簡繁體常規(guī)操作,主要針對一些中文語言語法的相關(guān)內(nèi)容。