行数和单词数统计

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
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值