词频统计-功能二

一.完成一个小程序

今天的任务是处理一本英文小说。我处理的英文小说是《war and peace》

本想着用多线程来解决的,发现看书还不是太懂,并不能真正动手编程。

我在编程的过程中主要遇到了以下两个问题:

1.在对整个英文小说进行单词总数统计遇见困难。

2.字符串数组的空字符串处理问题。因为我开始用空格来代替标点等不是英文单词来进行对单词分割,其中用到了正则表达式,后来统计对单词出现频数时,发现空格竟然排第一名,有4万多空格。

最终我的处理方式是使用List泛型集合的ForEach方法循环获取非空空字符串。

参考博客链接http://blog.csdn.net/orichisonic/article/details/49334397

这个博主提供了三种处理字符串数组的方法,大家感兴趣,可以学习一下。

我的实现代码如下:

namespace wd
{
    class Program
    {
        
        //static Dictionary<string, int> result = new Dictionary<string, int>(50000);
        static void Main(string[] args)
        {
            
            string theBookName;
            Console.Write(">wf  ");
            theBookName = Console.ReadLine();
            string path = @"F:hellowar_and_peace";
            string[] fs = Directory.GetFiles(path);
            
            string rline = null;
            foreach (string file in fs)
            {
                rline = ReadFile(file);
                rline = rline.ToLower();
                //使用正则表达式
                //Regex regex = new Regex(@"[A-Za-z]+[A-Za-z0-9]*");
                rline=Regex.Replace(rline, @"[^a-zA-Z0-9u4e00-u9fa5s]"," ");
                rline = Regex.Replace(rline, "[!@#$%^&*()`,./;':"<>`?...]"," ");
                string[]S= rline.Split(' ');
                //使用lambda表达式筛选过滤掉数组中空字符串
                //S = S.Where(S >= !string.IsNullOrEmpty(S)).ToArray();
                // var outputResult = from KVP in result
                                 //  orderby KVP.Value descending
                                  // select new StringBuilder(KVP.Key).Append(" ").Append(KVP.Value);
                //foreach (var str in outputResult)
                // {
                //   Console.WriteLine(str);

                //}

                /*
                 *使用List泛型集合的ForEach方法循环获取非空空字符串
                 *这里使用了匿名方法
                 */
                List<string> list = new List<string>();
                S.ToList().ForEach(
                (s) =>
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        list.Add(s);
                    }
                }
                );
                S = list.ToArray();
                Console.WriteLine("total  " + rline.Length + " words");
                Console.WriteLine();
                Hashtable ha = new Hashtable();
                for (int i = 0; i < S.Length; i++)
                {
                    if (ha.ContainsKey(S[i]))
                    {
                        ha[S[i]] = (int)ha[S[i]] + 1;
                    }
                    else
                    {
                        ha.Add(S[i], 1);
                    }
                }
                string[] arrKey = new string[ha.Count];//存哈希表的键
                int[] arrValue = new int[ha.Count];//存哈希表的值
                ha.Keys.CopyTo(arrKey, 0);
                ha.Values.CopyTo(arrValue, 0);
                Array.Sort(arrValue, arrKey);//按哈希表的值进行排序
                wd.Program p = new wd.Program();
                p.ShowArr2(arrKey, arrValue);

                   // Console.ReadKey();

            }
        }
       
        //读取文件
        private static string ReadFile(string file)
        {
            string readLine;
            FileStream fs = new FileStream(file, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            readLine = sr.ReadToEnd();
            sr.Close();
            fs.Close();
            return readLine;
          
        }
        //输出

         public void ShowArr1(string[] keyArray, int[] valueArray)
        {
            for (int i = keyArray.Length-1; i >=0; i--)
            {
                Console.WriteLine(keyArray[i].ToString().PadRight(15));
                Console.WriteLine(valueArray[i].ToString());
            }
        }
        //
        public void ShowArr2(string[] keyArray, int[] valueArray)
        {
            if (keyArray.Length < 10)
            {
                ShowArr1( keyArray, valueArray);
            }
            else
            {
                int sum = 0;
                for (int j = keyArray.Length - 1; j >=0; j--)
                {
                    Console.Write(keyArray[j].ToString().PadRight(15));
                    Console.WriteLine(valueArray[j].ToString());
                    sum++;
                    if (sum >= 10) break;
                }
            
            }

        }
       
    }
}

实现的截图如下:

二.例行报告

1.PSP(personal software process)个人软件过程

类型 任务 开始时间 结束时间 中断时间 净时间
看书 学习多线程和泛型 2017.9.17 10:30 2017.9.17 12:00 90min
编程 写代码 2017.9.17  14:30 2017.9.17 17:00 回复消息5min,厕所10min 2h15min
写作 写随笔 2017.9.17 17:00 2017.9.17 17:30 30min

2.进度条

  代码行 博文字数 知识点
第二周 166 750 见博客词频统计-功能二
原文地址:https://www.cnblogs.com/huyourongmonkey/p/7536388.html