trieTree.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Text;
  7. using System.Text.Json.Serialization;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using System.Collections;
  11. namespace trieTree.xiaoshi.sz.com
  12. {
  13. public class trieTree
  14. {
  15. treeNode root = new treeNode();
  16. Hashtable keyAddress = new Hashtable();
  17. public void AddWord(string word,string Key)
  18. {
  19. treeNode temNode = root;
  20. for (int i = 0; i < word.Length; i++)
  21. {
  22. if (temNode != null && temNode.children.ContainsKey(word[i]))
  23. {
  24. temNode = (treeNode)temNode.children[word[i]];
  25. }
  26. else
  27. {
  28. treeNode treeNode = new treeNode(word[i]);
  29. temNode.children.Add(word[i], treeNode);
  30. temNode = treeNode;
  31. }
  32. }
  33. temNode.isEnd = true;
  34. if (temNode.EndValues == null)
  35. {
  36. temNode.EndValues = new List<string>();
  37. }
  38. if (!temNode.EndValues.Contains(Key))
  39. {
  40. temNode.EndValues.Add(Key);
  41. }
  42. if (!keyAddress.ContainsKey(Key.Substring(0,12)))
  43. {
  44. keyAddress.Add(Key.Substring(0,12),word);
  45. }
  46. }
  47. public bool Search(string word,out List<string> key)
  48. {
  49. key = null;
  50. treeNode temNode = root;
  51. for (int i = 0; i < word.Length; i++)
  52. {
  53. if (temNode != null && temNode.children.ContainsKey(word[i]))
  54. {
  55. temNode = (treeNode)temNode.children[word[i]];
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61. }
  62. if (temNode != null)
  63. {
  64. if (temNode.isEnd)
  65. {
  66. key = temNode.EndValues;
  67. return true;
  68. }
  69. else
  70. {
  71. return false;
  72. }
  73. }
  74. else
  75. {
  76. return false ;
  77. }
  78. }
  79. /// <summary>
  80. /// 词库中最长匹配
  81. /// </summary>
  82. /// <param name="word"></param>
  83. /// <returns></returns>
  84. public treeNode maxPrif(string word,out int index)
  85. {
  86. treeNode retNode = null;
  87. index = -1;
  88. treeNode temNode = root;
  89. for (int i = 0; i < word.Length; i++)
  90. {
  91. if (temNode != null && temNode.children.ContainsKey(word[i]))
  92. {
  93. temNode = (treeNode)temNode.children[word[i]];
  94. if(temNode.isEnd)
  95. {
  96. retNode = temNode;
  97. index = i;
  98. }
  99. }
  100. else
  101. {
  102. break;
  103. }
  104. }
  105. return retNode;
  106. }
  107. public void Save(string filePath)
  108. {
  109. using (FileStream stream = new FileStream(filePath, FileMode.Create))
  110. {
  111. stream.Write(DataContractFormatSerializer.Serialize<treeNode>(root, true));
  112. }
  113. using (FileStream stream = new FileStream(filePath+".key", FileMode.Create))
  114. {
  115. stream.Write(DataContractFormatSerializer.Serialize<Hashtable>(keyAddress, true));
  116. }
  117. }
  118. public void Load(string filePath)
  119. {
  120. using (FileStream stream = new FileStream(filePath, FileMode.Open))
  121. {
  122. byte[] bytes = new byte[stream.Length];
  123. stream.Read(bytes, 0, bytes.Length);
  124. root = (treeNode)DataContractFormatSerializer.Deserialize<treeNode>(bytes,true);
  125. }
  126. using (FileStream stream = new FileStream(filePath + ".key", FileMode.Open))
  127. {
  128. byte[] bytes = new byte[stream.Length];
  129. stream.Read(bytes, 0, bytes.Length);
  130. keyAddress = (Hashtable)DataContractFormatSerializer.Deserialize<Hashtable>(bytes, true);
  131. }
  132. }
  133. public List<string> formatAddress(List<string> list)
  134. {
  135. List<string> retList = new List<string>();
  136. if (list != null && list.Count > 0)
  137. {
  138. string strAddress = list[list.Count - 1];
  139. String strKey = strAddress.Substring(0, 12);
  140. retList.Add((String)keyAddress[strKey.Substring(0, 2) + "0000000000"]);
  141. if (!strKey.EndsWith("0000000000"))
  142. {
  143. if (!retList.Contains((String)keyAddress[strKey.Substring(0, 4) + "00000000"]))
  144. retList.Add((String)keyAddress[strKey.Substring(0, 4) + "00000000"]);
  145. }
  146. if (!strKey.EndsWith("00000000"))
  147. {
  148. if (!retList.Contains((String)keyAddress[strKey.Substring(0, 6) + "000000"]))
  149. retList.Add((String)keyAddress[strKey.Substring(0, 6) + "000000"]);
  150. }
  151. if (!strKey.EndsWith("000000"))
  152. {
  153. if (!retList.Contains((String)keyAddress[strKey.Substring(0, 9) + "000"]))
  154. retList.Add((String)keyAddress[strKey.Substring(0, 9) + "000"]);
  155. }
  156. if (!strKey.EndsWith("000"))
  157. retList.Add((String)keyAddress[strKey]);
  158. }
  159. return retList;
  160. }
  161. }
  162. }