//按行读取输入文件并统计 public WordCalculate Input(WordCalculate datanumber, WordTrie wtrie) { FileStream fs = null; StreamReader sr = null; String dataline = String.Empty; try { fs = new FileStream(this.pathIn, FileMode.Open); sr = new StreamReader(fs); while ((dataline = sr.ReadLine()) != null) { datanumber.Calculate(dataline, wtrie); //按行统计数据 } } catch { Console.WriteLine("wrong!"); } finally { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } } return(datanumber); }
public static Result Maintest() { WordIO io = new WordIO(); WordCalculate datanumber = new WordCalculate(); WordTrie wtrie = new WordTrie(); Result res = new Result(); io.pathIn = "F:\\Demo.txt"; io.pathOut = "F:\\Result.txt"; datanumber = io.Input(datanumber, wtrie); //按行读取文件并统计 io.Output(datanumber, wtrie); res.charactersnumber = datanumber.charactersnumber; res.wordsnumber = datanumber.wordsnumber; res.linesnumber = datanumber.linesnumber; return(res); }
//将统计数据输出并写到输出文件 public void Output(WordCalculate datanumber, WordTrie wtrie) { FileStream fs = null; StreamWriter sw = null; List <WordTrie.ListUnit> WordList = new List <WordTrie.ListUnit>(); try { fs = new FileStream(this.pathOut, FileMode.Create); sw = new StreamWriter(fs); WordList = wtrie.Sort(); sw.WriteLine("字符总数为:{0}", datanumber.charactersnumber); sw.WriteLine("单词总数为:{0}", datanumber.wordsnumber); sw.WriteLine("有效行数为:{0}", datanumber.linesnumber); sw.WriteLine("\n词频\t单词\n"); Console.WriteLine("字符总数为:{0}", datanumber.charactersnumber); Console.WriteLine("单词总数为:{0}", datanumber.wordsnumber); Console.WriteLine("有效行数为:{0}", datanumber.linesnumber); Console.WriteLine("\n词频\t单词\n"); for (int i = 0; (i < 10 && i < WordList.Count); i++) { sw.WriteLine("{0}\t{1}", WordList[i].WordNum, WordList[i].Word); Console.WriteLine("{0}\t{1}", WordList[i].WordNum, WordList[i].Word); } } catch { Console.WriteLine("文档写入失败!"); } finally { if (sw != null) { sw.Close(); } if (fs != null) { fs.Close(); } } }