123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading.Tasks;
- using trieTree.xiaoshi.sz.com;
- namespace trieTree.xiaoshi.sz.com
- {
- public class AddressUtility
- {
- trieTree tree = new trieTree();
- private Hashtable keyAddress = new Hashtable();
- public AddressUtility() {
- string filePath = "data/tree.bin";
- if (File.Exists(filePath))
- {
- tree.Load(filePath);
- }
- else
- {
- string csvFile = "data/area_code_2024.csv";
- using (StreamReader sr = new StreamReader(csvFile))
- {
- while (!sr.EndOfStream)
- {
- string line = sr.ReadLine();
- string[] values = line.Split(',');
- //if (!keyAddress.ContainsKey(values[0]))
- //{
- // keyAddress.Add(values[0], values[1]);
- //}
- switch (values[2])
- {
- case "1":
- tree.AddWord(values[1], values[0] + values[1]);
- if (values[1].EndsWith("市") || values[1].EndsWith("省"))
- tree.AddWord(values[1].Substring(0, values[1].Length - 1), values[0] + values[1]);
- if (values[1].EndsWith("自治区"))
- tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
- break;
- case "2":
- if (values[1] != "市辖区"
- && values[1] != "省直辖县级行政区划"
- && values[1] != "县"
- && values[1] != "自治区直辖县级行政区划")
- {
- tree.AddWord(values[1], values[0] + values[1]);
- if (values[1].EndsWith("市") || values[1].EndsWith("盟"))
- {
- tree.AddWord(values[1].Substring(0, values[1].Length - 1), values[0] + values[1]);
- }
- if (values[1].EndsWith("地区"))
- {
- tree.AddWord(values[1].Substring(0, values[1].Length - 2), values[0] + values[1]);
- }
- #region 特殊市级名称
- //朝鲜族自治州
- //阿坝藏族羌族自治州
- //甘孜藏族自治州
- //凉山彝族自治州
- //黔西南布依族苗族自治州
- //黔东南苗族侗族自治州
- //黔南布依族苗族自治州
- //楚雄彝族自治州
- //红河哈尼族彝族自治州
- //文山壮族苗族自治州
- //西双版纳傣族自治州
- //大理白族自治州
- //德宏傣族景颇族自治州
- //怒江傈僳族自治州
- //临夏回族自治州
- //甘南藏族自治州
- //海北藏族自治州
- //黄南藏族自治州
- //海南藏族自治州
- //果洛藏族自治州
- //玉树藏族自治州
- //海西蒙古族藏族自治州
- //昌吉回族自治州
- //博尔塔拉蒙古自治州
- //巴音郭楞蒙古自治州
- //克孜勒苏柯尔克孜自治州
- //伊犁哈萨克自治州
- #endregion
- }
- break;
- case "3":
- tree.AddWord(values[1], values[0] + values[1]);
- if (values[1].EndsWith("旗") || values[1].EndsWith("市") || (values[1].EndsWith("区") && !values[1].EndsWith("地区")) || (values[1].EndsWith("县") && !values[1].EndsWith("自治县")))
- {
- if (values[1].Length > 2)
- {
- tree.AddWord(values[1].Substring(0, values[1].Length - 1), values[0] + values[1]);
- }
- }
- if (values[1].EndsWith("地区"))
- {
- if (values[1].Length > 3)
- tree.AddWord(values[1].Substring(0, values[1].Length - 2), values[0] + values[1]);
- }
- if (values[1].EndsWith("自治县"))
- {
- if (values[1].Length > 4)
- tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
- }
- #region 特殊区县级名称
- //和布克赛尔蒙古自治县
- //察布查尔锡伯自治县
- //塔什库尔干塔吉克自治县
- //焉耆回族自治县
- //木垒哈萨克自治县
- #endregion
- break;
- case "4":
- if (!values[1].Contains("直辖"))
- {
- tree.AddWord(values[1], values[0] + values[1]);
- }
- break;
- case "5":
- //break;
- tree.AddWord(values[1], values[0] + values[1]);
- if (values[1].EndsWith("居委会") || values[1].EndsWith("生活区"))
- {
- if (values[1].Length > 4)
- {
- tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
- }
- }
- if (values[1].EndsWith("村委会"))
- {
- //if (values[1].Length > 4)
- //{
- // tree.AddWord(values[1].Substring(0, values[1].Length - 3), values[0] + values[1]);
- //}
- tree.AddWord(values[1].Substring(0, values[1].Length - 2), values[0] + values[1]);
- }
- if (values[1].EndsWith("居民委员会"))
- {
- //if (values[1].Length > 6)
- //{
- // tree.AddWord(values[1].Substring(0, values[1].Length - 5), values[0] + values[1]);
- //}
- }
- break;
- default:
- break;
- }
- //tree.AddWord(values[1],values[0]);
- }
- }
- tree.AddWord("工业园区", "320576000000苏州工业园区");
- tree.AddWord("高新区", "320505000000虎丘区");
- tree.AddWord("科技城", "320505000000虎丘区");
- tree.AddWord("芙蓉村", "320412106003芙蓉社区居委会(含芙蓉村村委)");
- tree.AddWord("芙蓉社区", "320412106003芙蓉社区居委会(含芙蓉村村委)");
- tree.AddWord("襄樊", "420600000000襄阳市");
- tree.AddWord("襄樊市", "420600000000襄阳市");
- tree.AddWord("四方区", "370203000000市北区");
- Console.WriteLine(DateTime.Now);
- tree.Save(filePath);
- }
-
- }
- public List<string>? Paser(string address)
- {
- List<List<string>> list = new List<List<string>>();
- int i = 0;
- bool shenAdded = false;
- bool shiAdded = false;
- bool quxianAdded = false;
-
- while (i < address.Length)
- {
- int temIndex = 0;
- treeNode treeNode = tree.maxPrif(address.Substring(i), out temIndex);
- if (treeNode != null)
- {
- //list.Add(treeNode.EndValues);
- //Console.WriteLine($"{address.Substring(i,temIndex)},{i},{temIndex + i}");
- foreach (string key in treeNode.EndValues)
- {
- string strId = key.Substring(0,12);
- if (strId.EndsWith("0000000000"))
- {
- if (!shenAdded)
- {
- addkeyToResult(key, list);
- shenAdded = true;
- }
- continue;
- }
- if (strId.EndsWith("00000000") )
- {
- if (!shiAdded)
- {
- addkeyToResult(key, list);
- shiAdded = true;
- }
- continue;
- }
- //if (strId.EndsWith("000000"))
- //{
- // if (!quxianAdded)
- // {
- // addkeyToResult(key, list);
- // quxianAdded = true;
- // }
- // continue;
- //}
- addkeyToResult(key, list);
- //Console.WriteLine($"\t{key}");
- }
- i = i+ temIndex;
- }
- i++;
- }
-
- List<string>? retList = null;
- //int maxLength =0;
- //foreach (List<string> strings in list)
- //{
- // if(strings.Count > maxLength && IncludeShenShi(strings))
- // {
- // maxLength = strings.Count;
- // retList = strings;
- // }
- //}
- double maxResult =0;
- foreach (List<string> strings in list)
- {
- double temResult = CalResult(strings);
- if(temResult > maxResult)
- {
- maxResult = temResult;
- retList = strings;
- }
- }
- if (retList != null)
- {
- retList.Sort();
- }
- return tree.formatAddress(retList);
- }
- private double CalResult(List<string> list)
- {
- double result = 0;
- foreach (string str in list)
- {
- string value = str.Substring(0, 12);
- if (value.EndsWith("0000000000"))
- {
- result += 1.2;
- }
- else
- {
- if (value.EndsWith("00000000"))
- {
- result += 1.1;
- }
- else
- {
- if (value.EndsWith("000000"))
- {
- result += 0.9;
- }
- else
- {
- if (value.EndsWith("000"))
- {
- result += 0.6;
- }
- else
- {
- result += 0.4;
- }
- }
- }
- }
- }
- return result;
- }
- private bool IncludeShenShi(List<string> strings)
- {
- foreach (string key in strings)
- {
- string value = key.Substring(0, 12);
- if(value.EndsWith("0000000000") || value.EndsWith("00000000"))
- {
- return true;
- }
- }
- return false;
- }
-
- public bool Search(string str)
- {
- return tree.Search(str, out _);
- }
- private void addkeyToResult(string key, List<List<string>> list)
- {
- if(list != null)
- {
- if (list.Count > 0) {
- bool isAdded = false;
- List<List<string>> strings1 = new List<List<string>>();
-
- foreach (List<string> item in list)
- {
- List<string> tempList = new List<string>();
- foreach (string key2 in item)
- {
- if(isSub(key2, key))
- {
- if (!tempList.Contains(key2))
- {
- tempList.Add(key2);
- }
- }
- }
- if (tempList.Count > 0)
- {
- if (tempList.Count == item.Count)
- {
- if (!item.Contains(key))
- {
- item.Add(key);
- }
- isAdded = true;
- break;
- }
- else
- {
- tempList.Add(key);
- strings1.Add(tempList);
- //isAdded = true;
- }
-
- }
-
- }
- if (!isAdded)
- {
- if (strings1.Count > 0)
- {
- List<string> strings2 = new List<string>();
- int lstLength = 0;
- foreach(List<string> lst in strings1)
- {
- if (lstLength < lst.Count)
- {
- lstLength = lst.Count;
- strings2 = lst;
- }
- }
- list.Add(strings2);
- }
- else
- {
- List<string> strings = new List<string>() { key };
- list.Add(strings);
- }
-
- }
- }
- else
- {
- List<string> strings = new List<string>() { key};
- list.Add(strings);
- }
- }
- else
- {
- throw new Exception("list不能未空!");
- }
- }
- private bool isSub(string key2, string key)
- {
- if (key2 != null && key != null)
- {
- string s1 = TrimRightZero(key2.Substring(0, 12));
- string s2 = TrimRightZero(key.Substring(0, 12));
- return (s1.StartsWith(s2) || s2.StartsWith(s1));
- }
- return false;
- }
- private string TrimRightZero(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- return str;
- }
- else
- {
- string str1= str.Substring(0, 2);
- string str2 = str.Substring(2, 2);
- string str3 = str.Substring(4, 2);
- string str4 = str.Substring(6, 3);
- string str5 = str.Substring(9, 3);
- if (str5 != "000")
- {
- return str1 + str2 +str3 +str4 + str5;
- }
- else
- {
- if(str4 != "000")
- {
- return str1+str2 +str3 +str4 ;
- }
- else
- {
- if(str3 != "00")
- {
- return str1 + str2 + str3 ;
- }
- else
- {
- if(str2 != "00")
- {
- return str1 + str2 ;
- }
- else
- {
- return str1 ;
- }
- }
- }
- }
- //for (int i = str.Length - 1; i >= 0; i--)
- //{
- // if (str[i] != '0')
- // {
- // return str.Substring(0, i+1);
- // }
- //}
- //return "";
- }
- }
- }
- }
|