using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace HowManyRaws
{
class Program
{
static void Main(string[] args)
{
string inputFilePath = @"test.txt";
string outputFilePath = @"output.log";
int rawNumber = 0;
Dictionary<string, int> dict = new Dictionary<string, int>();
//Recursive traversal directory, statistics, stored in $dict
CountWords(inputFilePath, dict, out rawNumber);
//Sort and output to file
OutputLogTo(outputFilePath, dict, rawNumber);
//Wait for input
System.Console.ReadLine();
}
/// <summary>
/// Count word occurrences in a directory of UTF-8-encoded text files
/// </summary>
/// <param name="filepath">filepath</param>
/// <param name="dict">directory to store "wordscount"</param>
static void CountWords(string filepath, Dictionary<string, int> dict, out int rawnumber)
{
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
string txt;
rawnumber = 0;
while ((txt = sr.ReadLine()) != null)
{
rawnumber++;
//Replace "," "." and "!" with " "
txt = txt.Replace(",", " ").Replace(".", " ").Replace("!", " ").Replace("?", " ");
string[] txtsp = txt.Split(' ');
for (int i = 0; i < txtsp.Length; i++)
{
//Skip when 0-length
if (txtsp[i].Length == 0)
{
continue;
}
//不区分大小写
string word = txtsp[i].ToLower();
if (dict.ContainsKey(word))
{
dict[word]++;
}
else
{
dict.Add(word, 1);
}
}
}
fs.Close();
sr.Close();
}
/// Outputs a file (UTF-8) containing a list
/// sorted (a) by frequency and (b) alphabetically within the same frequency.
static void OutputLogTo(string outputFilePath, Dictionary<string, int> dict, int rawnumber)
{
//If the file exists, append it.
StreamWriter sw = new StreamWriter(
outputFilePath, true, Encoding.GetEncoding("utf-8"));
sw.WriteLine("Raw Number: {0}, Words Number: {1}", rawnumber, dict.Count);
sw.Close();
Console.WriteLine("Raw Number: {0}, Words Number: {1}", rawnumber, dict.Count); //For debug
}
}
}
行数和单词数统计
最新推荐文章于 2023-04-12 23:10:22 发布