/// <summary> /// 从文本文件读取字典 /// </summary> /// <param name="fileName"></param> static public T_DictFile LoadFromTextDict(String fileName) { T_DictFile dictFile = new T_DictFile(); String dictStr = CFile.ReadFileToString(fileName, "utf-8"); String[] words = CRegex.Split(dictStr, "\r\n"); foreach (String word in words) { String[] wp = CRegex.Split(word, @"\|"); if (wp == null) { continue; } if (wp.Length != 2) { continue; } int pos = 0; try { pos = int.Parse(wp[1]); } catch { continue; } T_DictStruct dict = new T_DictStruct(); dict.Word = wp[0]; dict.Pos = pos; if (dict.Word.Contains("一") || dict.Word.Contains("二") || dict.Word.Contains("三") || dict.Word.Contains("四") || dict.Word.Contains("五") || dict.Word.Contains("六") || dict.Word.Contains("七") || dict.Word.Contains("八") || dict.Word.Contains("九") || dict.Word.Contains("十")) { dict.Pos |= (int)T_POS.POS_A_M; } if (dict.Word == "字典") { dict.Pos = (int)T_POS.POS_D_N; } dictFile.Dicts.Add(dict); } return(dictFile); }
static public T_DictFile LoadFromBinFileEx(String fileName) { T_DictFile dictFile = new T_DictFile(); dictFile.Dicts = new List <T_DictStruct>(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); byte[] version = new byte[32]; fs.Read(version, 0, version.Length); String ver = Encoding.UTF8.GetString(version, 0, version.Length); String verNumStr = CRegex.GetMatch(ver, "KTDictSeg Dict V(.+)", true); if (verNumStr == null || verNumStr == "") { //1.3以前版本 fs.Close(); return(LoadFromBinFile(fileName)); } while (fs.Position < fs.Length) { byte[] buf = new byte[sizeof(int)]; fs.Read(buf, 0, buf.Length); int length = BitConverter.ToInt32(buf, 0); buf = new byte[length]; T_DictStruct dict = new T_DictStruct(); fs.Read(buf, 0, buf.Length); dict.Word = Encoding.UTF8.GetString(buf, 0, length - sizeof(int) - sizeof(double)); dict.Pos = BitConverter.ToInt32(buf, length - sizeof(int) - sizeof(double)); dict.Frequency = BitConverter.ToDouble(buf, length - sizeof(double)); dictFile.Dicts.Add(dict); } fs.Close(); return(dictFile); }