utility.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.Serialization;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using trieTree.xiaoshi.sz.com;
  10. namespace testtrieTree
  11. {
  12. public class AddressUtility
  13. {
  14. trieTree.xiaoshi.sz.com.trieTree tree = new trieTree.xiaoshi.sz.com.trieTree();
  15. private Hashtable keyAddress = new Hashtable();
  16. public AddressUtility() {
  17. string filePath = "data/tree.bin";
  18. if (File.Exists(filePath))
  19. {
  20. tree.Load(filePath);
  21. }
  22. else
  23. {
  24. string csvFile = "data/area_code_2024.csv";
  25. using (StreamReader sr = new StreamReader(csvFile))
  26. {
  27. while (!sr.EndOfStream)
  28. {
  29. string line = sr.ReadLine();
  30. string[] values = line.Split(',');
  31. //if (!keyAddress.ContainsKey(values[0]))
  32. //{
  33. // keyAddress.Add(values[0], values[1]);
  34. //}
  35. switch (values[2])
  36. {
  37. case "1":
  38. tree.AddWord(values[1], values[0] + values[1]);
  39. if (values[1].EndsWith("市") || values[1].EndsWith("省"))
  40. tree.AddWord(values[1].Substring(0, values[1].Length - 1), values[0] + values[1]);
  41. if (values[1].EndsWith("自治区"))
  42. tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
  43. break;
  44. case "2":
  45. if (values[1] != "市辖区"
  46. && values[1] != "省直辖县级行政区划"
  47. && values[1] != "县"
  48. && values[1] != "自治区直辖县级行政区划")
  49. {
  50. tree.AddWord(values[1], values[0] + values[1]);
  51. if (values[1].EndsWith("市") || values[1].EndsWith("盟"))
  52. {
  53. tree.AddWord(values[1].Substring(0, values[1].Length - 1), values[0] + values[1]);
  54. }
  55. if (values[1].EndsWith("地区"))
  56. {
  57. tree.AddWord(values[1].Substring(0, values[1].Length - 2), values[0] + values[1]);
  58. }
  59. #region 特殊市级名称
  60. //朝鲜族自治州
  61. //阿坝藏族羌族自治州
  62. //甘孜藏族自治州
  63. //凉山彝族自治州
  64. //黔西南布依族苗族自治州
  65. //黔东南苗族侗族自治州
  66. //黔南布依族苗族自治州
  67. //楚雄彝族自治州
  68. //红河哈尼族彝族自治州
  69. //文山壮族苗族自治州
  70. //西双版纳傣族自治州
  71. //大理白族自治州
  72. //德宏傣族景颇族自治州
  73. //怒江傈僳族自治州
  74. //临夏回族自治州
  75. //甘南藏族自治州
  76. //海北藏族自治州
  77. //黄南藏族自治州
  78. //海南藏族自治州
  79. //果洛藏族自治州
  80. //玉树藏族自治州
  81. //海西蒙古族藏族自治州
  82. //昌吉回族自治州
  83. //博尔塔拉蒙古自治州
  84. //巴音郭楞蒙古自治州
  85. //克孜勒苏柯尔克孜自治州
  86. //伊犁哈萨克自治州
  87. #endregion
  88. }
  89. break;
  90. case "3":
  91. tree.AddWord(values[1], values[0] + values[1]);
  92. if (values[1].EndsWith("旗") || values[1].EndsWith("市") || (values[1].EndsWith("区") && !values[1].EndsWith("地区")) || (values[1].EndsWith("县") && !values[1].EndsWith("自治县")))
  93. {
  94. if (values[1].Length > 2)
  95. {
  96. tree.AddWord(values[1].Substring(0, values[1].Length - 1), values[0] + values[1]);
  97. }
  98. }
  99. if (values[1].EndsWith("地区"))
  100. {
  101. if (values[1].Length > 3)
  102. tree.AddWord(values[1].Substring(0, values[1].Length - 2), values[0] + values[1]);
  103. }
  104. if (values[1].EndsWith("自治县"))
  105. {
  106. if (values[1].Length > 4)
  107. tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
  108. }
  109. #region 特殊区县级名称
  110. //和布克赛尔蒙古自治县
  111. //察布查尔锡伯自治县
  112. //塔什库尔干塔吉克自治县
  113. //焉耆回族自治县
  114. //木垒哈萨克自治县
  115. #endregion
  116. break;
  117. case "4":
  118. if (!values[1].Contains("直辖"))
  119. {
  120. tree.AddWord(values[1], values[0] + values[1]);
  121. }
  122. break;
  123. case "5":
  124. //break;
  125. tree.AddWord(values[1], values[0] + values[1]);
  126. if (values[1].EndsWith("居委会") || values[1].EndsWith("生活区"))
  127. {
  128. if (values[1].Length > 4)
  129. {
  130. tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
  131. }
  132. }
  133. if (values[1].EndsWith("村委会"))
  134. {
  135. //if (values[1].Length > 4)
  136. //{
  137. // tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
  138. //}
  139. tree.AddWord(values[1].Substring(0, values[1].Length - 2), values[0] + values[1]);
  140. }
  141. if (values[1].EndsWith("居民委员会"))
  142. {
  143. //if (values[1].Length > 6)
  144. //{
  145. // tree.AddWord(values[1].Substring(0, values[1].Length - 5), values[0] + values[1]);
  146. //}
  147. }
  148. break;
  149. default:
  150. break;
  151. }
  152. //tree.AddWord(values[1],values[0]);
  153. }
  154. }
  155. tree.AddWord("工业园区", "320576000000苏州工业园区");
  156. tree.AddWord("高新区", "320505000000虎丘区");
  157. tree.AddWord("科技城", "320505000000虎丘区");
  158. tree.AddWord("芙蓉村", "320412106003芙蓉社区居委会(含芙蓉村村委)");
  159. tree.AddWord("芙蓉社区", "320412106003芙蓉社区居委会(含芙蓉村村委)");
  160. tree.AddWord("襄樊", "420600000000襄阳市");
  161. tree.AddWord("襄樊市", "420600000000襄阳市");
  162. tree.AddWord("四方区", "370203000000市北区");
  163. Console.WriteLine(DateTime.Now);
  164. tree.Save(filePath);
  165. }
  166. }
  167. public List<string>? Paser(string address)
  168. {
  169. List<List<string>> list = new List<List<string>>();
  170. int i = 0;
  171. bool shenAdded = false;
  172. bool shiAdded = false;
  173. bool quxianAdded = false;
  174. while (i < address.Length)
  175. {
  176. int temIndex = 0;
  177. treeNode treeNode = tree.maxPrif(address.Substring(i), out temIndex);
  178. if (treeNode != null)
  179. {
  180. //list.Add(treeNode.EndValues);
  181. //Console.WriteLine($"{address.Substring(i,temIndex)},{i},{temIndex + i}");
  182. foreach (string key in treeNode.EndValues)
  183. {
  184. string strId = key.Substring(0,12);
  185. if (strId.EndsWith("0000000000"))
  186. {
  187. if (!shenAdded)
  188. {
  189. addkeyToResult(key, list);
  190. shenAdded = true;
  191. }
  192. continue;
  193. }
  194. if (strId.EndsWith("00000000") )
  195. {
  196. if (!shiAdded)
  197. {
  198. addkeyToResult(key, list);
  199. shiAdded = true;
  200. }
  201. continue;
  202. }
  203. //if (strId.EndsWith("000000"))
  204. //{
  205. // if (!quxianAdded)
  206. // {
  207. // addkeyToResult(key, list);
  208. // quxianAdded = true;
  209. // }
  210. // continue;
  211. //}
  212. addkeyToResult(key, list);
  213. //Console.WriteLine($"\t{key}");
  214. }
  215. i = i+ temIndex;
  216. }
  217. i++;
  218. }
  219. List<string>? retList = null;
  220. //int maxLength =0;
  221. //foreach (List<string> strings in list)
  222. //{
  223. // if(strings.Count > maxLength && IncludeShenShi(strings))
  224. // {
  225. // maxLength = strings.Count;
  226. // retList = strings;
  227. // }
  228. //}
  229. double maxResult =0;
  230. foreach (List<string> strings in list)
  231. {
  232. double temResult = CalResult(strings);
  233. if(temResult > maxResult)
  234. {
  235. maxResult = temResult;
  236. retList = strings;
  237. }
  238. }
  239. if (retList != null)
  240. {
  241. retList.Sort();
  242. }
  243. return tree.formatAddress(retList);
  244. }
  245. private double CalResult(List<string> list)
  246. {
  247. double result = 0;
  248. foreach (string str in list)
  249. {
  250. string value = str.Substring(0, 12);
  251. if (value.EndsWith("0000000000"))
  252. {
  253. result += 1.2;
  254. }
  255. else
  256. {
  257. if (value.EndsWith("00000000"))
  258. {
  259. result += 1.1;
  260. }
  261. else
  262. {
  263. if (value.EndsWith("000000"))
  264. {
  265. result += 0.9;
  266. }
  267. else
  268. {
  269. if (value.EndsWith("000"))
  270. {
  271. result += 0.6;
  272. }
  273. else
  274. {
  275. result += 0.4;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. return result;
  282. }
  283. private bool IncludeShenShi(List<string> strings)
  284. {
  285. foreach (string key in strings)
  286. {
  287. string value = key.Substring(0, 12);
  288. if(value.EndsWith("0000000000") || value.EndsWith("00000000"))
  289. {
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. public bool Search(string str)
  296. {
  297. return tree.Search(str, out _);
  298. }
  299. private void addkeyToResult(string key, List<List<string>> list)
  300. {
  301. if(list != null)
  302. {
  303. if (list.Count > 0) {
  304. bool isAdded = false;
  305. List<List<string>> strings1 = new List<List<string>>();
  306. foreach (List<string> item in list)
  307. {
  308. List<string> tempList = new List<string>();
  309. foreach (string key2 in item)
  310. {
  311. if(isSub(key2, key))
  312. {
  313. if (!tempList.Contains(key2))
  314. {
  315. tempList.Add(key2);
  316. }
  317. }
  318. }
  319. if (tempList.Count > 0)
  320. {
  321. if (tempList.Count == item.Count)
  322. {
  323. if (!item.Contains(key))
  324. {
  325. item.Add(key);
  326. }
  327. isAdded = true;
  328. break;
  329. }
  330. else
  331. {
  332. tempList.Add(key);
  333. strings1.Add(tempList);
  334. //isAdded = true;
  335. }
  336. }
  337. }
  338. if (!isAdded)
  339. {
  340. if (strings1.Count > 0)
  341. {
  342. List<string> strings2 = new List<string>();
  343. int lstLength = 0;
  344. foreach(List<string> lst in strings1)
  345. {
  346. if (lstLength < lst.Count)
  347. {
  348. lstLength = lst.Count;
  349. strings2 = lst;
  350. }
  351. }
  352. list.Add(strings2);
  353. }
  354. else
  355. {
  356. List<string> strings = new List<string>() { key };
  357. list.Add(strings);
  358. }
  359. }
  360. }
  361. else
  362. {
  363. List<string> strings = new List<string>() { key};
  364. list.Add(strings);
  365. }
  366. }
  367. else
  368. {
  369. throw new Exception("list不能未空!");
  370. }
  371. }
  372. private bool isSub(string key2, string key)
  373. {
  374. if (key2 != null && key != null)
  375. {
  376. string s1 = TrimRightZero(key2.Substring(0, 12));
  377. string s2 = TrimRightZero(key.Substring(0, 12));
  378. return (s1.StartsWith(s2) || s2.StartsWith(s1));
  379. }
  380. return false;
  381. }
  382. private string TrimRightZero(string str)
  383. {
  384. if (string.IsNullOrEmpty(str))
  385. {
  386. return str;
  387. }
  388. else
  389. {
  390. string str1= str.Substring(0, 2);
  391. string str2 = str.Substring(2, 2);
  392. string str3 = str.Substring(4, 2);
  393. string str4 = str.Substring(6, 3);
  394. string str5 = str.Substring(9, 3);
  395. if (str5 != "000")
  396. {
  397. return str1 + str2 +str3 +str4 + str5;
  398. }
  399. else
  400. {
  401. if(str4 != "000")
  402. {
  403. return str1+str2 +str3 +str4 ;
  404. }
  405. else
  406. {
  407. if(str3 != "00")
  408. {
  409. return str1 + str2 + str3 ;
  410. }
  411. else
  412. {
  413. if(str2 != "00")
  414. {
  415. return str1 + str2 ;
  416. }
  417. else
  418. {
  419. return str1 ;
  420. }
  421. }
  422. }
  423. }
  424. //for (int i = str.Length - 1; i >= 0; i--)
  425. //{
  426. // if (str[i] != '0')
  427. // {
  428. // return str.Substring(0, i+1);
  429. // }
  430. //}
  431. //return "";
  432. }
  433. }
  434. }
  435. }