您的位置:首页 > 其它

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

2017-09-17 21:32 281 查看
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:102017-9-15 14:01111min
看书c#程序设计教程2017-9-15 15:302017-9-15 17:47休息30min107min
写代码读取.txt文件并输出,运行2017-9-15 20:20[b]2017-9-15 20:54
[/b]

34min
写博文写博客30min2017-9-15 22:052017-9-15 22:2520min
看书,查阅c#程序设计教程,百度2017-9-16 18:302017-9-16 19:30120min
写代码实现功能一2017-9-16 19:302017-9-16 23:50洗衣服洗漱等40min200min
写博客写博客2017-9-16 23:502017-9-16 23:599min
写代码改进格式对齐5min2017-9-17 9:582017-9-17 10:002min
写代码写功能二2017-9-17 10:052017-9-17 11:03喝水2min56min
写代码解决单词个数多一的问题2017-9-17 15:302017-9-17 15:4010min
写代码查阅书籍,百度,完善功能二,完整实现2017-9-17 15:402017-9-17 19:40上厕所喝水看手机消息60min180min
写博客写博客2017-9-17 19:502017-9-17 20:0414min
写代码整合功能一和功能二,包括测试功能2017-9-17 20:102017-9-18 0:47回宿舍走路,洗漱,休息120min158
日期代码行数博文字数知识点
2017-9-1515182
2017-9-1640299compare,split,哈希表,sort
2017-9-1733255正则表达式,padRight
2018-9-1885
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: