C # Send Message Filter keywords

Source: Internet
Author: User


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<char, trienode> m_values;

Public Trienode ()

{

M_values = new Dictionary<char, trienode> ();

}

}

public class Triefilter:trienode

{

<summary>

Add keywords

</summary>

<param name= "Key" ></param>

public void Addkey (string key)

{

if (string. IsNullOrEmpty (Key))

{

Return

}

Trienode node = this;

for (int i = 0; i < key. Length; i++)

{

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;

}


<summary>

Check if illegal characters are included

</summary>

<param name= "text" > Input text </param>

<returns> found the 1th illegal character. No then return to string.empty</returns>

public bool Hasbadword (string text)

{

for (int i = 0; i < text. Length; i++)

{

Trienode node;

if (m_values. TryGetValue (Text[i], out node))

{

for (int j = i + 1; j < text. Length; J + +)

{

if (node.m_values. TryGetValue (Text[j], out node))

{

if (node.m_end)

{

return true;

}

}

Else

{

Break

}

}

}

}

return false;

}


<summary>

Check if illegal characters are included

</summary>

<param name= "text" > Input text </param>

<returns> found the 1th illegal character. No then return to string.empty</returns>

public string FindOne (string text)

{

for (int i = 0; i < text. Length; i++)

{

char C = text[i];

Trienode node;

if (m_values. TryGetValue (c, out node))

{

for (int j = i + 1; j < text. Length; J + +)

{

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<string> FindAll (string text)

{

for (int i = 0; i < text. Length; i++)

{

Trienode node;

if (m_values. TryGetValue (Text[i], out node))

{

for (int j = i + 1; j < text. Length; J + +)

{

if (node.m_values. TryGetValue (Text[j], out node))

{

if (node.m_end)

{

Yield return text. Substring (I, (j + 1-i));

}

}

Else

{

Break

}

}

}

}

}


<summary>

Replace illegal characters

</summary>

<param name= "Text" ></param>

<param name= "C" > used in place of illegal characters </param>

<returns> replaced String </returns>

public string Replace (string text, char c)

public string Replace (string text, char c = ' * ')

{

char[] chars = null;

for (int i = 0; i < text. Length; i++)

{

Trienode subnode;

if (m_values. TryGetValue (Text[i], out subnode))

{

for (int j = i + 1; j < text. Length; J + +)

{

if (subnode.m_values. TryGetValue (Text[j], out subnode))

{

if (subnode.m_end)

{

if (chars = = null) chars = text. ToArray ();

for (int t = i; t <= J; t++)

{

Chars[t] = c;

}

i = j;

}

}

Else

{

Break

}

}

}

}

return chars = = null? Text:new string (chars);

}

}

}



Call the execution method class:

#region Filter Keywords

Stopwatch SW2 = new Stopwatch ();

SW2. Start ();

int time_cap = 2000;

String urladdress = HttpContext.Server.MapPath ("~/app_data/keyword.txt");

Triefilter tf = new Triefilter ();

using (StreamReader SW = new StreamReader (System.IO.File.OpenRead (urladdress)))

{

string key = sw. ReadLine ();

while (key = null)

{

if (Key! = string. Empty)

{

Tf. Addkey (key);

}

Key = SW. ReadLine ();

}

}

if (!string. IsNullOrEmpty (content))

Content = tf. Replace (Content, ' * ');


#region Test Run time

System.Diagnostics.Stopwatch SW1 = new System.Diagnostics.Stopwatch ();

SW1. Start ();

System.Threading.Thread.Sleep (TIME_CAP);

SW1. Stop ();


TimeSpan ts2 = SW1. Elapsed;

Double T = ts2. totalmilliseconds;//Run time


SW2. Stop ();

TimeSpan Ts3 = sw2. Elapsed;

Double times = Ts3. TotalMilliseconds;

Console.WriteLine ("Stopwatch total cost {0}ms.", Ts3. TotalMilliseconds);

#endregion


#endregion


C # Send Message Filter keywords

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.