/// <summary> /// 加载姓氏词典 /// </summary> private void LoadSurnameDict() { //建立一个姓氏词典实例 _SurnameDict = new DictionarySegment((char)0); //读取姓氏词典文件 FillDictSegment(_SurnameDict, Dictionary.PATH_DIC_SURNAME); }
/// <summary> /// 加载主词典及扩展词典 /// </summary> private void LoadMainDict() { //建立一个主词典实例 _MainDict = new DictionarySegment((char)0); //读取主词典文件 FillDictSegment(_MainDict, Dictionary.PATH_DIC_MAIN); //加载扩展词典配置 foreach (var extDictName in Configuration.ExtDictionarys) { //读取扩展词典文件 if (!File.Exists(extDictName)) continue; FillDictSegment(_MainDict, extDictName); } }
/// <summary> /// 将数组中的segment迁移到Map中 /// </summary> /// <param name="segmentArray"></param> /// <param name="segmentMap"></param> private void Migrate(DictionarySegment[] segmentArray, Map<char, DictionarySegment> segmentMap) { foreach (DictionarySegment segment in segmentArray) { if (segment != null) { segmentMap.Add(segment.nodeChar, segment); } } }
/// <summary> /// 查找本节点下对应的keyChar的segment /// 如果没有找到,则创建新的segment /// </summary> /// <param name="keyChar"></param> /// <returns></returns> private DictionarySegment LookforSegment(char keyChar) { DictionarySegment ds = null; if (this.storeSize <= ARRAY_LENGTH_LIMIT) { //获取数组容器,如果数组未创建则创建数组 DictionarySegment[] segmentArray = this.ChildrenArray; //搜寻数组 foreach (DictionarySegment segment in segmentArray) { if (segment != null && segment.nodeChar.Equals(keyChar)) { //在数组中找到与keyChar对应的segment ds = segment; break; } } //遍历数组后没有找到对应的segment if (ds == null) { //构造新的segment ds = new DictionarySegment(keyChar); if (this.storeSize < ARRAY_LENGTH_LIMIT) { //数组容量未满,使用数组存储 segmentArray[this.storeSize] = ds; //segment数目+1 this.storeSize++; } else { //数组容量已满,切换Map存储 //获取Map容器,如果Map未创建,则创建Map Map<char, DictionarySegment> segmentMap = this.ChildrenMap; //将数组中的segment迁移到Map中 Migrate(segmentArray, segmentMap); //存储新的segment segmentMap.Add(keyChar, ds); //segment数目+1 , 必须在释放数组前执行storeSize++ , 确保极端情况下,不会取到空的数组 this.storeSize++; //释放当前的数组引用 this.childrenArray = null; } } } else { //获取Map容器,如果Map未创建,则创建Map Map<char, DictionarySegment> segmentMap = this.ChildrenMap; //搜索Map if (!segmentMap.TryGetValue(keyChar, out ds)) { //构造新的segment ds = new DictionarySegment(keyChar); segmentMap.Add(keyChar, ds); //当前节点存储segment数目+1 this.storeSize++; } } return ds; }
/// <summary> /// 加载量词词典 /// </summary> private void LoadQuantifierDict() { //建立一个量词典实例 _QuantifierDict = new DictionarySegment((char)0); //读取量词词典文件 FillDictSegment(_QuantifierDict, Dictionary.PATH_DIC_QUANTIFIER); }
/// <summary> /// 填充字典 /// </summary> /// <param name="dictSeg"></param> /// <param name="dicPath"></param> private void FillDictSegment(DictionarySegment dictSeg, string dicPath) { dicPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dicPath); if (!File.Exists(dicPath)) { throw new FileNotFoundException(dicPath + " Dictionary not found!!!"); } try { var theWordLines = File.ReadAllLines(dicPath); foreach (var theWord in theWordLines) { if (!string.IsNullOrEmpty(theWord.Trim())) { dictSeg.FillSegment(theWord.Trim().ToCharArray()); } } } catch (IOException ioe) { System.Console.WriteLine(dicPath + " Dictionary loading exception."); System.Console.WriteLine(ioe.StackTrace); } }
/// <summary> /// 加载停止词词典 /// </summary> private void LoadStopWordDict() { //建立一个停止词典实例 _StopWords = new DictionarySegment((char)0); //读取停止词典文件 FillDictSegment(_StopWords, Dictionary.PATH_DIC_STOP); //加载扩展停止词典 foreach (String extStopWordDictName in Configuration.ExtStopWordDictionarys) { //如果找不到扩展的字典,则忽略 if (!File.Exists(extStopWordDictName)) continue; FillDictSegment(_StopWords, extStopWordDictName); } }
/// <summary> /// 加载介词\副词词典 /// </summary> private void LoadPrepDict() { //建立一个介词\副词词典实例 _PrepDict = new DictionarySegment((char)0); //读取介词\副词词典文件 FillDictSegment(_PrepDict, Dictionary.PATH_DIC_PREP); }
/// <summary> /// 加载后缀词典 /// </summary> private void LoadSuffixDict() { //建立一个后缀词典实例 _SuffixDict = new DictionarySegment((char)0); //读取后缀词典文件 FillDictSegment(_SuffixDict, Dictionary.PATH_DIC_SUFFIX); }