Ejemplo n.º 1
0
        private static void AddInMap(string prefix, string tail, string code, SortedDictionary <string, AreaInfo> map)
        {
            AreaInfo ai;

            if (map.TryGetValue(prefix, out ai))
            {
                ai.AddTailCode(tail, code);
            }
            else
            {
                ai = new AreaInfo();
                ai.AddTailCode(tail, code);
                map[prefix] = ai;
            }
        }
Ejemplo n.º 2
0
        public static bool Load(string path)
        {
            try
            {
                _trie = new DoubleArrayTrie <AreaInfo>();
                var valueArr = LoadDat(path + ".value.dat");
                if (valueArr != null)
                {
                    if (_trie.Load(path + ".trie.dat", valueArr))
                    {
                        return(true);
                    }
                }
                // 读取txt文件
                var map = new SortedDictionary <string, AreaInfo>(StrComparer.Default);
                foreach (var line in File.ReadLines(path))
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }
                    var segs = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    var code = segs[0];
                    for (int i = 1; i < segs.Length; i++)
                    {
                        var name = segs[i];
                        if (Invalids.Contains(name))
                        {
                            continue;                            // 跳过无效地区名
                        }
                        if (name.Length == 2)
                        {
                            AddInMap(name, "", code, map);
                        }
                        else
                        {
                            var lastChar = name[name.Length - 1];
                            if ("市省县区州旗盟".Contains(lastChar))
                            {
                                AddInMap(name.Substring(0, name.Length - 1), lastChar.ToString(), code, map);
                            }
                            else if (name.Length < 9)
                            {
                                AddInMap(name, "", code, map);
                            }
                            var lastTwo = name.Substring(2);
                            var prevs   = name.Substring(0, name.Length - 2);
                            if (Invalids.Contains(lastTwo))
                            {
                                AddInMap(prevs, lastTwo, code, map);
                                if (prevs.Length == 3 && "市省".Contains(prevs[2]))
                                {
                                    AddInMap(name.Substring(0, 2), lastTwo, code, map);
                                }
                            }
                            if (lastChar == '旗')
                            {
                                var sublast = name[2];
                                if ("前后左中右特".Contains(sublast))
                                {
                                    AddInMap(prevs, "旗", code, map);
                                }
                            }
                            var subLastTwo = name.Substring(name.Length - 3, 2);
                            if (subLastTwo == "自治")
                            {
                                prevs = name.Substring(0, name.Length - 3);
                                var ends = name.Substring(name.Length - 3);
                                AddInMap(prevs, ends, code, map);
                                if (prevs.Length >= 4)
                                {
                                    for (int k = 2; k < prevs.Length - 1; k++)
                                    {
                                        if (k < prevs.Length - 3)
                                        {
                                            if (Nationalities.Contains(prevs.Substring(k, 4)))
                                            {
                                                AddInMap(prevs.Substring(0, k), ends, code, map);
                                                AddInMap(prevs.Substring(0, k) + "自治", lastChar.ToString(), code, map);
                                                break;
                                            }
                                        }
                                        if (k < prevs.Length - 2)
                                        {
                                            if (Nationalities.Contains(prevs.Substring(k, 3)))
                                            {
                                                AddInMap(prevs.Substring(0, k), ends, code, map);
                                                AddInMap(prevs.Substring(0, k) + "自治", lastChar.ToString(), code, map);
                                                break;
                                            }
                                        }
                                        if (Nationalities.Contains(prevs.Substring(k, 2)))
                                        {
                                            AddInMap(prevs.Substring(0, k), ends, code, map);
                                            AddInMap(prevs.Substring(0, k) + "自治", lastChar.ToString(), code, map);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                _trie = new DoubleArrayTrie <AreaInfo>();
                _trie.Build(map);
                valueArr = new AreaInfo[map.Count];
                int m = 0;
                foreach (var v in map.Values)
                {
                    valueArr[m++] = v;
                }

                var fs = new FileStream(path + ".trie.dat", FileMode.Create, FileAccess.Write);
                _trie.Save(fs);
                fs.Close();
                SaveDat(path + ".value.dat", valueArr);
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }