日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【转】关键字过滤算法

發(fā)布時間:2025/3/8 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】关键字过滤算法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Collections;namespace BLL.Common {#region 操作類public class KeywordsFilter{#region 關(guān)鍵字過濾/// <summary>/// 關(guān)鍵字過濾/// /// </summary>/// <param name="keywords"></param>/// <returns></returns>public static string Filter(string keywords){//需過濾關(guān)鍵字集合List<string> badwords = new List<string>();KeywordsFilterClass kf = new KeywordsFilterClass();keywords = kf.BadwordInKeywords(keywords, badwords);return keywords;}#endregion}#endregion#region 關(guān)鍵字過濾類/// <summary>/// 關(guān)鍵字過濾類/// </summary>public class KeywordsFilterClass{private Dictionary<string, object> hash = new Dictionary<string, object>();//臟字字典 開頭臟字存儲private BitArray firstCharCheck = new BitArray(char.MaxValue);//臟字字典 單個char存儲private BitArray allCharCheck = new BitArray(char.MaxValue);private int maxLength = 0;/// <summary>/// 初始化 已存儲的 過濾字符串/// </summary>/// <param name="words"></param>private void InitHash(List<string> badwords){foreach (string word in badwords){//保存字典內(nèi)不存在的臟字if (!hash.ContainsKey(word)){hash.Add(word, null);//設(shè)置臟字計算長度this.maxLength = Math.Max(this.maxLength, word.Length);firstCharCheck[word[0]] = true;foreach (char c in word){allCharCheck[c] = true;}}}}/// <summary>/// 替換字符串中的臟字為指定的字符/// </summary>/// <param name="text"></param>/// <returns></returns>public string BadwordInKeywords(string text, List<string> badwords){//初始化 臟字字典this.InitHash(badwords);int index = 0;while (index < text.Length){//判斷開頭臟字if (!firstCharCheck[text[index]]){//未找到開頭臟字 則索引累加while (index < text.Length - 1 && !firstCharCheck[text[++index]]) ;}for (int j = 1; j <= Math.Min(maxLength, text.Length - index); j++){if (!allCharCheck[text[index + j - 1]]){break;}string sub = text.Substring(index, j);if (hash.ContainsKey(sub)){text = text.Replace(sub, "**");//this.InitHash(badwords);index += j;break;}}index++;}return text;}}#endregion }

?

轉(zhuǎn)載于:https://www.cnblogs.com/TiestoRay/articles/2686115.html

總結(jié)

以上是生活随笔為你收集整理的【转】关键字过滤算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。