Beispiel #1
0
 static void BuildDawgFile(string file)
 {
     var rootPath = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\test\bin\Debug\", "");
     var wordUtil = new WordUtil();
     //加载默认的词频
     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);
 }
Beispiel #2
0
        public void TestFromTxtFile(string file)
        {
            var wordUtil = new WordUtil();
            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 = WordUtil.LoadFrom(ms);
            watcher.Stop();
            Console.WriteLine("load dawg file elapsed time:" + watcher.Elapsed.TotalMilliseconds + "'ms");
            Assert.AreEqual(expectWordCount, wordUtil.Count);
        }