Trie tree-dirty word Filtering Application

Source: Internet
Author: User

Trie tree, also known as character search tree, Prefix Tree, mainly used for character matching (see http://en.wikipedia.org/wiki/Trie ). It is suitable for keyword search. For example, you can search for keywords in an article and add links to them. Of course, the application of dirty word filtering is also similar, but the replacement of the connection is replaced with a replacement character.

The current Code is just a simple replacement and does not process some characters. For example, "I saw your mom yesterday and forced me to buy a house" is not a dirty word, because there are commas (,), the range of characters must be added to the program.

The Skip in the program is a simple variant used to filter out dirty words. For example, if you are "looking for a ** sister", you can skip up to three characters by default. This can be adjusted as needed. In short, it is a trie exercise.

 

  1. Public class trietree
  2. {
  3. Private readonly dictionary <char, trietree> children;
  4. Public bool end {Get; set ;}
  5. Public trietree ()
  6. {
  7. Children = new dictionary <char, trietree> ();
  8. }
  9. Public void addkey (string keyword)
  10. {
  11. If (string. isnullorempty (keyword ))
  12. {
  13. Return;
  14. }
  15. VaR cNode = this;
  16. Foreach (var key in keyword)
  17. {
  18. If (cNode. Children. containskey (key ))
  19. {
  20. CNode = cNode. Children [Key];
  21. }
  22. Else
  23. {
  24. VaR node = new trietree ();
  25. CNode. Children. Add (Key, node );
  26. CNode = node;
  27. }
  28. }
  29. CNode. End = true;
  30. }
  31. Public void Replace (ref string text)
  32. {
  33. For (VAR I = 0; I <text. length; I ++)
  34. {
  35. VaR cNode = this;
  36. Var key = text [I];
  37. // The first dirty word
  38. If (cNode. Children. containskey (key ))
  39. {
  40. CNode = cNode. Children [Key];
  41. // Search for words that contain dirty words
  42. VaR skip = 0;
  43. For (var j = I + 1; j <text. length; j ++)
  44. {
  45. If (cNode. Children. containskey (Text [J])
  46. {
  47. CNode = cNode. Children [Text [J];
  48. Skip = 0;
  49. }
  50. Else
  51. {
  52. // Allow skipping a few characters
  53. Skip ++;
  54. If (skip> 3)
  55. {
  56. Break;
  57. }
  58. }
  59. If (cNode. End)
  60. {
  61. VaR Len = J + 1-I;
  62. TEXT = text. Replace (text. substring (I, Len), String. Empty. padleft (Len ,'*'));
  63. I + = Len;
  64. Break;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }

The usage is as follows:

 

  1. Class Program
  2. {
  3. Static void main (string [] ARGs)
  4. {
  5. VaR trie = new trietree ();
  6. VaR KEYWORDS = "fuck, prostitute, fuck". Split (',');
  7. Foreach (var key in keywords)
  8. {
  9. Trie. addkey (key );
  10. }
  11. VaR text = @ "I wiped it, Nima, fuck you, you prostitute, bitch. ";
  12. Trie. Replace (ref text );
  13. Console. writeline (text );
  14. Console. Read ();
  15. }
  16. }


Execution result:

 

From http://blog.csdn.net/maddemon/article/details/7011699

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.