软件工程2017第二次作业——词频统计

      2017-9-15随笔

       昨天看到老师布置的作业,简单翻阅了一下学长的博客,了解了一下作业的需求,注册好了码农的账号(使用码农是为了版本控制,上传代码),今天借了两本关于c#的书简单看了一些,着重看了文件输出输出流,之前就已经安装好了运行环境microsoft visual studio 2010,今天就用看的知识写了简单的读取test.txt文件并输出在控制台,部分代码如下:

            string PATH = null; 
            Console.Write(">type   ");     //这里输入文本所在目录 例如 f:\test.txt   
            PATH = Console.ReadLine();    
            StreamReader r = File.OpenText(PATH);    
            while(!r.EndOfStream){  
    
                Console.WriteLine(r.ReadLine());  

            }     

运行结果如图

  

  2017-9-16随笔

           今天写代码看到运行结果,之后遇到输出格式有问题的,例如:

        1.昨天写的是输入文件路径,然后读取文件并输出,今天看到要求是输入文件名然后读取文件并输出,稍修改了下,代码如下(注释掉的内容是昨天写的输入路径之后再读取文件输出内容,今天写的绝对路径):

            string Filename;
            Console.Write(">type ");
            Filename = Console.ReadLine();
            string FilePath = @"f:/test.txt";
            string str1="test.txt";
            //string FilePath = null;
            //Console.Write(">type ");
            //FilePath = Console.ReadLine();
            //StreamReader content = File.OpenText(FilePath);//输入文件名,读取文件内容
            StreamReader reader = new StreamReader(FilePath);
            string word = reader.ReadLine();
            if (String.Compare(Filename, str1) == 0) { Console.WriteLine(word.ToString()); }

2.第二个是看着老师那个词频从高到底排列输出,挺漂亮的,可惜自己不会对齐,然后运行结果就是这样的:

                    Console.Write(htKey[i].ToString().PadRight(10,' ') );
                    //Console.Write(htKey[i].ToString() + "    ");
                    Console.WriteLine(htValue[i]);//按词频输出单词和出现的次数,从高到低

    

3.第三个问题是那个单词总数用的是哈希表的键对值个数计算的单词个数,为什么多一个数到现在为止还没想通。

 

2017-9-17随笔

        1、解决了格式对齐问题,格式问题解决的代码及运行截图如下(ps:哈哈,终于完成了功能一)

                   Console.Write(htKey[i].ToString().PadRight(10,' ') );
                   //Console.Write(htKey[i].ToString() + "    ");
                   Console.WriteLine(htValue[i]);//按词频输出单词和出现的次数,从高到低

 

 

 2、完成功能二,见截图:

   

 3、解决了单词个数多一个的问题,将数组里的空值去掉了,见代码:

string[] S = word.Split(new char[] { ' ', ',', '.', '!', '?', ':', ';', '\'', '\"' }, StringSplitOptions.RemoveEmptyEntries);

然后功能一运行截图如下:

 4、本以为功能二完成了,结果换一个.txt文件测试的时候,发现读取不能读两段,只能读连续的一段,刚开始以为是过滤符号时多个空格无法过滤不能读取第二段的文件,然后换用了正则表达式还是测试失败,最终被功能一的一行代码误导了,以为它读取的是.txt文件的所有单词,然后一下午就为了解决这个问题,终于运行成功了,见代码及运行截图:

using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace wf2
{
    class Program
    {

        static void Main(string[] args)
        {

            //string Filename;
            Console.Write(">wf ");
            string Filename = Console.ReadLine();
            string FilePath = @"f:/war_and_peace.txt";//the_dead_return
           
            StreamReader Reader = new StreamReader(FilePath);
            string word = Reader.ReadToEnd();
          
            
           // string str1 = "test.txt";
            /*string FilePath = null;
            Console.Write(">type ");
            FilePath = Console.ReadLine();
            StreamReader content = File.OpenText(FilePath);//输入文件名,读取文件内容*/
            //StreamReader reader = new StreamReader(FilePath);
            //string word = reader.ReadLine();

            // FileStream file = new FileStream("f:\\test.txt", FileMode.Open);
           // string[] word = file;

            //string[] comline = { ">type test.txt","wf -s test.txt"};
            //if (String.Compare(Filename, str1) == 0)
            //{ Console.WriteLine(word.ToString()); }//比较输入,若输入的是test.txt,然后输出文本结果
            //word = word.ToLower();//全部变为小写字母

            // char[] ch = { ' ', ',', '.', '!', '?', ':', ';', '\'', '\"' };//定义一个字符数组,用空格标点来区别单词
            //string[] S = word.Split(ch);
            //string[] S = word.Split(new char[] { ' ', ',', '.', '!', '?', ':', ';', '\'', '\"' }, StringSplitOptions.RemoveEmptyEntries);
            // word = Regex.Replace(word, @"[^a-zA-Z0-9\u4e00-\u9fa5\s{2,}]", " ");
            word = Regex.Replace(word, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", " ");
            word = Regex.Replace(word, "[!@#$%^&*()`,./;':\"<>`?...]", " ");//用正则表达式来过滤替换标点,用空格替换
            String[] words = word.Split(' ');//分割
            Hashtable ht = new Hashtable();//建立哈希表,键对值存储单词以及个数
            for (int i = 0; i < words.Length; i++)
            {
                if (ht.ContainsKey(words[i]))
                {
                    ht[words[i]] = (int)ht[words[i]] + 1;
                }
                else
                {
                    ht.Add(words[i], 1);
                }
            }
        
            string[] htKey = new string[ht.Count];//存哈希表的键
            int[] htValue = new int[ht.Count];//存哈希表的值
            ht.Keys.CopyTo(htKey, 0);
            ht.Values.CopyTo(htValue, 0);
            Console.WriteLine();
            Console.WriteLine("total " + ht.Count);//输出文本单词个数
            Console.WriteLine();

            Array.Sort(htValue, htKey);//按哈希表的值进行排序 
            int n=0;
            for (int i = htKey.Length - 1; i >= 0; i--)
            {
                if ((string)htKey[i] != "")
                {
                    if(n<10){
                    Console.Write(htKey[i].ToString().PadRight(10, ' '));
                    //Console.Write(htKey[i].ToString() + "    ");
                    Console.WriteLine(htValue[i]);//按词频输出单词和出现的次数,从高到低
                        n++;
                    }

                }
            }

        }
    }
}

 

类型 任务 预计时间 开始时间 结束时间 中断时间 实际用时
C#基础教学视频

看视频了解c#基础

  2017-9-15 12:10 2017-9-15 14:01   111min
看书 c#程序设计教程   2017-9-15 15:30 2017-9-15  17:47 休息30min 107min
写代码 读取.txt文件并输出,运行   2017-9-15  20:20
2017-9-15  20:54
  34min
写博文 写博客 30min 2017-9-15   22:05  2017-9-15 22:25   20min
 看书,查阅  c#程序设计教程,百度    2017-9-16 18:30  2017-9-16 19:30    120min
 写代码  实现功能一    2017-9-16 19:30
 2017-9-16 23:50  洗衣服洗漱等40min  200min
 写博客  写博客    2017-9-16 23:50  2017-9-16 23:59    9min
写代码 改进格式对齐 5min 2017-9-17 9:58 2017-9-17 10:00   2min
写代码 写功能二   2017-9-17 10:05 2017-9-17 11:03 喝水2min 56min
写代码 解决单词个数多一的问题   2017-9-17 15:30 2017-9-17 15:40   10min
写代码 查阅书籍,百度,完善功能二,完整实现   2017-9-17 15:40 2017-9-17 19:40 上厕所喝水看手机消息60min 180min
写博客 写博客   2017-9-17 19:50 2017-9-17 20:04   14min
写代码 整合功能一和功能二,包括测试功能   2017-9-17 20:10 2017-9-18 0:47 回宿舍走路,洗漱,休息120min 158
日期 代码行数 博文字数 知识点
2017-9-15 15 182  
2017-9-16 40 299 compare,split,哈希表,sort
 2017-9-17  33  255  正则表达式,padRight
2018-9-18 85    
posted @ 2017-09-17 21:32  liusx  阅读(291)  评论(0编辑  收藏  举报