protected static void InitWords(string dicPath) { Assembly assembly = Assembly.GetAssembly(typeof(WordsUtility)); using (Stream stream = assembly.GetManifestResourceStream(dicPath)) { while (true) { WORDS w = new WORDS(); byte[] intByte = new byte[4]; stream.Read(intByte, 0, 4); w.KEY = BitConverter.ToInt32(intByte, 0); if (w.KEY == 0) return; stream.Read(intByte, 0, 4); int len = BitConverter.ToInt32(intByte, 0); byte[] str = new byte[len]; stream.Read(str, 0, len); w.WORD = Encoding.Unicode.GetString(str); stream.Read(intByte, 0, 4); len = BitConverter.ToInt32(intByte, 0); w.TREE_PATH = new int[len]; for (int i = 0; i < len; i++) { stream.Read(intByte, 0, 4); w.TREE_PATH[i] = BitConverter.ToInt32(intByte, 0); } stream.Read(intByte, 0, 4); w.RANK = BitConverter.ToSingle(intByte, 0); stream.Read(intByte, 0, 4); w.FLAG_KEY = BitConverter.ToInt32(intByte, 0); Words.Add(w); } } }
public HitWords(WORDS word) { this.KEY = word.KEY; int len = word.TREE_PATH.Length; this.TREE_PATH = new int[len + 1]; Array.Copy(word.TREE_PATH, this.TREE_PATH, word.TREE_PATH.Length); this.TREE_PATH[len] = word.KEY; this.WORD = word.WORD; this.SCORE = word.RANK; }
public void WriteDictionary() { using (Stream stream = new FileStream("words.dic", FileMode.OpenOrCreate)) { List<Region> ls = GetAreas(); StringBuilder builder = new StringBuilder(); List<FLAG_WORDS> order = WordsUtility.FlagWords.OrderByDescending(x => x.WORD.Length).ToList(); foreach (Region region in ls) { string[] ids = region.TreePath.Split(','); int[] id = new int[ids.Length]; for (int i = 0; i < ids.Length; i++) { id[i] = int.Parse(ids[i]); } WORDS w = new WORDS() { KEY = region.Id, WORD = region.Name, TREE_PATH = id, }; w.RANK = 0.1F; FLAG_WORDS fw = order.FirstOrDefault(z => region.Name.EndsWith(z.WORD)); w.RANK = w.RANK / ls.Sum(x => x.Name.Contains(w.WORD) ? 1 : 0); if (fw != null) { w.FLAG_KEY = fw.KEY; w.RANK += w.RANK / ls.Sum(x => x.Name.Contains(w.WORD.TrimEnd(fw.WORD)) ? 1 : 0); } w.RANK += 1.0F / w.TREE_PATH.Length; stream.Write(BitConverter.GetBytes(w.KEY), 0, 4); byte[] bw = Encoding.Unicode.GetBytes(w.WORD); stream.Write(BitConverter.GetBytes(bw.Length), 0, 4); stream.Write(bw, 0, bw.Length); stream.Write(BitConverter.GetBytes(w.TREE_PATH.Length), 0, 4); foreach (int n in w.TREE_PATH) stream.Write(BitConverter.GetBytes(n), 0, 4); stream.Write(BitConverter.GetBytes(w.RANK), 0, 4); stream.Write(BitConverter.GetBytes(w.FLAG_KEY), 0, 4); string line = string.Format("{0}:【{1}】 ({2}) 【{3}】", w.KEY, w.WORD, w.RANK, region.TreePath); builder.AppendLine(line); Console.WriteLine(line); } File.AppendAllText("result.txt", builder.ToString()); } }
protected static void InitWords(string dicPath) { Assembly assembly = Assembly.GetAssembly(typeof(WordsUtility)); using (Stream stream = assembly.GetManifestResourceStream(dicPath)) { while (true) { WORDS w = new WORDS(); byte[] intByte = new byte[4]; stream.Read(intByte, 0, 4); w.KEY = BitConverter.ToInt32(intByte, 0); if (w.KEY == 0) { return; } stream.Read(intByte, 0, 4); int len = BitConverter.ToInt32(intByte, 0); byte[] str = new byte[len]; stream.Read(str, 0, len); w.WORD = Encoding.Unicode.GetString(str); stream.Read(intByte, 0, 4); len = BitConverter.ToInt32(intByte, 0); w.TREE_PATH = new int[len]; for (int i = 0; i < len; i++) { stream.Read(intByte, 0, 4); w.TREE_PATH[i] = BitConverter.ToInt32(intByte, 0); } stream.Read(intByte, 0, 4); w.RANK = BitConverter.ToSingle(intByte, 0); stream.Read(intByte, 0, 4); w.FLAG_KEY = BitConverter.ToInt32(intByte, 0); Words.Add(w); } } }