/// <summary> /// 添加关键字 /// </summary> /// <param name="key"></param> public void AddKey(string key) { if (string.IsNullOrEmpty(key)) { return; } TrieFilter node = this; foreach (var c in key) { TrieFilter subnode; if (!node.m_values.TryGetValue(c, out subnode)) { subnode = new TrieFilter(c); node.m_values.Add(c, subnode); } node = subnode; } //最后的结点,表示单词结束,用‘\0’表示,指向一个空对象 node.m_values['\0'] = null; }
static TrieFilter getIllegalWords() { object obj = myCache.Get("IllegalWords"); if (obj == null) { obj = CacheHelper.Instance.Get(CacheCatalog.GetOther("IllegalWords")); TrieFilter tf = new TrieFilter(); if (obj != null) { foreach (string temp in (obj as List<string>)) { tf.AddKey(temp); } myCache.Add("IllegalWords", tf, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), System.Web.Caching.CacheItemPriority.Normal, null); return tf; } else { List<string> result = new List<string>(); using (System.IO.StreamReader sr = new System.IO.StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/File/") + "tools/IllegalWords.txt", Encoding.UTF8)) { string temp = string.Empty; while (sr.Peek() > 0) { temp = sr.ReadLine().Trim(); if (!string.IsNullOrEmpty(temp)) { result.Add(temp); tf.AddKey(temp); } } sr.Close(); } CacheHelper.Instance.Set(CacheCatalog.GetOther("IllegalWords"), result); myCache.Add("IllegalWords", tf, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), System.Web.Caching.CacheItemPriority.Normal, null); return tf; } } else return (TrieFilter)obj; }