C # send message filtering keyword
TrieFilter class
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Web
Namespace SaaS.Web.Base
{
Public class TrieNode
{
Public bool m_end
Public Dictionary m_values
Public TrieNode ()
{
M_values = new Dictionary ()
}
}
Public class TrieFilter: TrieNode
{
/ / /
/ / add keyword
/ / /
/ / /
Public void AddKey (string key)
{
If (string.IsNullOrEmpty (key))
{
Return
}
TrieNode node = this
For (int I = 0; I < key.Length; iTunes +)
{
Char c = key [I]
TrieNode subnode
If (! node.m_values.TryGetValue (c, out subnode))
{
Subnode = new TrieNode ()
Node.m_values.Add (c, subnode)
}
Node = subnode
}
Node.m_end = true
}
/ / /
/ check whether it contains illegal characters
/ / /
/ / enter text
/ / the first illegal character found. If not, return string.Empty.
Public bool HasBadWord (string text)
{
For (int I = 0; I < text.Length; iTunes +)
{
TrieNode node
If (m_values.TryGetValue (text [I], out node))
{
For (int j = I + 1; j < text.Length; jacks +)
{
If (node.m_values.TryGetValue (text [j], out node))
{
If (node.m_end)
{
Return true
}
}
Else
{
Break
}
}
}
}
Return false
}
/ / /
/ check whether it contains illegal characters
/ / /
/ / enter text
/ / the first illegal character found. If not, return string.Empty.
Public string FindOne (string text)
{
For (int I = 0; I < text.Length; iTunes +)
{
Char c = text [I]
TrieNode node
If (m_values.TryGetValue (c, out node))
{
For (int j = I + 1; j < text.Length; jacks +)
{
If (node.m_values.TryGetValue (text [j], out node))
{
If (node.m_end)
{
Return text.Substring (I, j + 1-I)
}
}
Else
{
Break
}
}
}
}
Return string.Empty
}
/ / find all illegal characters
Public IEnumerable FindAll (string text)
{
For (int I = 0; I < text.Length; iTunes +)
{
TrieNode node
If (m_values.TryGetValue (text [I], out node))
{
For (int j = I + 1; j < text.Length; jacks +)
{
If (node.m_values.TryGetValue (text [j], out node))
{
If (node.m_end)
{
Yield return text.Substring (I, (j + 1-I))
}
}
Else
{
Break
}
}
}
}
}
/ / /
/ / replace illegal characters
/ / /
/ / /
/ / used to replace illegal characters
/ / replaced string
Public string Replace (string text, char c)
/ / public string Replace (string text, char c ='*')
{
Char [] chars = null
For (int I = 0; I < text.Length; iTunes +)
{
TrieNode subnode
If (m_values.TryGetValue (text [I], out subnode))
{
For (int j = I + 1; j < text.Length; jacks +)
{
If (subnode.m_values.TryGetValue (text [j], out subnode))
{
If (subnode.m_end)
{
If (chars = = null) chars = text.ToArray ()
For (int t = I; t