public void TestFromTxtFile(string file) { var wordUtil = new WordDict(); var expectWordCount = 0; using (var sr = new StreamReader(file, Encoding.UTF8)) { string line = null; while ((line = sr.ReadLine()) != null) { if (line == string.Empty) continue; wordUtil.Add(line); expectWordCount++; } } var watcher = new System.Diagnostics.Stopwatch(); watcher.Start(); var ms = new MemoryStream(); wordUtil.SaveTo(ms); watcher.Stop(); Console.WriteLine("build dawg elapsed time:" + watcher.Elapsed.TotalMilliseconds + "'ms"); watcher.Reset(); watcher.Start(); ms.Position = 0; wordUtil = WordDict.LoadFrom(ms); watcher.Stop(); Console.WriteLine("load dawg file elapsed time:" + watcher.Elapsed.TotalMilliseconds + "'ms"); Assert.AreEqual(expectWordCount, wordUtil.Count); }
static void BuildDawgFile(string file) { var rootPath = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\test\bin\Debug\", ""); var wordUtil = new WordDict(); //加载默认的词频 using (var sr = new StreamReader(rootPath + @"\dict\cwsharp.freq", Encoding.UTF8)) { string line = null; while ((line = sr.ReadLine()) != null) { if (line == string.Empty) continue; var array = line.Split(' '); wordUtil.Add(array[0], int.Parse(array[1])); } } //加载新的词典 using (var sr = new StreamReader(rootPath + @"\dict\cwsharp.dic", Encoding.UTF8)) { string line = null; while ((line = sr.ReadLine()) != null) { if (line == string.Empty) continue; wordUtil.Add(line); } } //保存新的dawg文件 wordUtil.SaveTo(file); }
static void BuildDawgFile(string file) { var rootPath = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\test\bin\Debug\", ""); var wordUtil = new WordDict(); //加载默认的词频 using (var sr = new StreamReader(rootPath + @"\dict\cwsharp.freq", Encoding.UTF8)) { string line = null; while ((line = sr.ReadLine()) != null) { if (line == string.Empty) { continue; } var array = line.Split(' '); wordUtil.Add(array[0], int.Parse(array[1])); } } //加载新的词典 using (var sr = new StreamReader(rootPath + @"\dict\cwsharp.dic", Encoding.UTF8)) { string line = null; while ((line = sr.ReadLine()) != null) { if (line == string.Empty) { continue; } wordUtil.Add(line); } } //保存新的dawg文件 wordUtil.SaveTo(file); }
public void SimpleTest() { var wordUtil = new WordDict(); var words = new string[] { "tap", "taps", "top", "tops" }; var expectWordCount = words.Length; wordUtil.AddWords(words); Assert.AreEqual(expectWordCount, wordUtil.Count); var ms = new MemoryStream(); wordUtil.SaveTo(ms); Assert.Greater(ms.Length, 0); ms.Position = 0; wordUtil = WordDict.LoadFrom(ms); Assert.AreEqual(expectWordCount, wordUtil.Count); }
public void TestFromTxtFile(string file) { var wordUtil = new WordDict(); var expectWordCount = 0; using (var sr = new StreamReader(file, Encoding.UTF8)) { string line = null; while ((line = sr.ReadLine()) != null) { if (line == string.Empty) { continue; } wordUtil.Add(line); expectWordCount++; } } var watcher = new System.Diagnostics.Stopwatch(); watcher.Start(); var ms = new MemoryStream(); wordUtil.SaveTo(ms); watcher.Stop(); Console.WriteLine("build dawg elapsed time:" + watcher.Elapsed.TotalMilliseconds + "'ms"); watcher.Reset(); watcher.Start(); ms.Position = 0; wordUtil = WordDict.LoadFrom(ms); watcher.Stop(); Console.WriteLine("load dawg file elapsed time:" + watcher.Elapsed.TotalMilliseconds + "'ms"); Assert.AreEqual(expectWordCount, wordUtil.Count); }