Individual Project - Word frequency program by HJB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class v
{
public int n { get; set; }
public string s { get; set; }
public v(int nu, string st)
{
this.n = nu;
this.s = st;
}
}
class Program
{
static void Main(string[] args)
{
if (args.Length ==1)
{
Dictionary<string, v> ht = new Dictionary<string, v>();
if (args.Length == 0)
{
Console.WriteLine("please input the correct file adress");
}
string path = args[0];
if (!Directory.Exists(path))
{
Console.WriteLine("wrong path");
}
else
{
if ((Directory.GetFiles(path).Length == 0) && (Directory.GetDirectories(path).Length == 0))
Console.WriteLine("empty directory");
else
{
string[] file = (Directory.GetFiles(path, "*", SearchOption.AllDirectories)).Where(s => s.EndsWith(".txt") || s.EndsWith(".h") || s.EndsWith(".cpp") || s.EndsWith(".cs")).ToArray();
int l = file.Length;
for (int i = 0; i < l; i++)
{
string s = "(\\b[a-zA-Z]{3}[A-Za-z0-9]+)|(\\b[a-zA-Z]{3})|((_[a-zA-Z]{3}[A-Za-z0-9]+)|(_[a-zA-Z]{3}))";
string article = File.ReadAllText(file[i]);
foreach (Match m in Regex.Matches(article, s))
{
string c = m.ToString();
if (c.Contains("_"))
{
c = c.Replace("_", "");
}
string n = c.ToLower();
if (ht.ContainsKey(n))
{
v w = (v)ht[n];
if (c.CompareTo(w.s) >= 0)
{
w.n += 1;
w.s = c;
}
else
w.n += 1;
}
else
{
v w = new v(1, c);
ht.Add(n, w);
}
}
}
}
}
var re = ht.OrderByDescending(v => v.Value.n).ThenBy(v => v.Value.s);
StreamWriter sw = new StreamWriter(path + "\\黄敬博.txt", false);
foreach (var skey in re)
{
v w = (v)ht[skey.Key];
sw.WriteLine(w.s + "\t" + w.n);
}
sw.Flush();
}
if (args[0].Equals("-e2"))
{
int j = 1;
Dictionary<string, v> ht = new Dictionary<string, v>();
if (args.Length == 0)
{
Console.WriteLine("please input the correct file adress");
}
string path = args[1];
if (!Directory.Exists(path))
{
Console.WriteLine("wrong path");
}
else
{
if ((Directory.GetFiles(path).Length == 0) && (Directory.GetDirectories(path).Length == 0))
Console.WriteLine("empty directory");
else
{
string[] file = (Directory.GetFiles(path, "*", SearchOption.AllDirectories)).Where(s => s.EndsWith(".txt") || s.EndsWith(".h") || s.EndsWith(".cpp") || s.EndsWith(".cs")).ToArray();
int l = file.Length;
for (int i = 0; i < l; i++)
{
string s = @"\b[A-Za-z]{3,}[A-Za-z0-9]*\s{1}\b[A-Za-z]{3,}[A-Za-z0-9]*";
string article = File.ReadAllText(file[i]);
Regex reg = new Regex(s);
Match m = reg.Match(article, 0);
while (m.Success)
{
String word = m.ToString();
string n = word.ToLower();
if (ht.ContainsKey(n))
{
v w = (v)ht[n];
if (word.CompareTo(w.s) >= 0)
{
w.n += 1;
w.s = word;
}
else
w.n += 1;
}
else
{
v w = new v(1, word);
ht.Add(n, w);
}
m = reg.Match(article, m.Index + m.ToString().IndexOf(' '));
}
}
}
}
var re = ht.OrderByDescending(v => v.Value.n).ThenBy(v => v.Value.s);
StreamWriter sw = new StreamWriter(path + "\\黄敬博.txt", false);
foreach (var skey in re)
{
v w = (v)ht[skey.Key];
sw.WriteLine(w.s + "\t" + w.n);
sw.Flush();
j++;
if (j > 10)
break;
}
}
if (args[0].Equals("-e3") )
{
int j = 1;
Dictionary<string, v> ht = new Dictionary<string, v>();
if (args.Length == 0)
{
Console.WriteLine("please input the correct file adress");
}
string path = args[1];
if (!Directory.Exists(path))
{
Console.WriteLine("wrong path");
}
else
{
if ((Directory.GetFiles(path).Length == 0) && (Directory.GetDirectories(path).Length == 0))
Console.WriteLine("empty directory");
else
{
string[] file = (Directory.GetFiles(path, "*", SearchOption.AllDirectories)).Where(s => s.EndsWith(".txt") || s.EndsWith(".h") || s.EndsWith(".cpp") || s.EndsWith(".cs")).ToArray();
int l = file.Length;
for (int i = 0; i < l; i++)
{
string s = @"\b[A-Za-z]{3,}[A-Za-z0-9]*\s{1}\b[A-Za-z]{3,}[A-Za-z0-9]*\s{1}\b[A-Za-z]{3,}[A-Za-z0-9]*";
string article = File.ReadAllText(file[i]);
Regex reg = new Regex(s);
Match m = reg.Match(article, 0);
while (m.Success)
{
String word = m.ToString();
string n = word.ToLower();
if (ht.ContainsKey(n))
{
v w = (v)ht[n];
if (word.CompareTo(w.s) >= 0)
{
w.n += 1;
w.s = word;
}
else
w.n += 1;
}
else
{
v w = new v(1, word);
ht.Add(n, w);
}
m = reg.Match(article, m.Index + m.ToString().IndexOf(' '));
}
}
}
}
var re = ht.OrderByDescending(v => v.Value.n).ThenBy(v => v.Value.s);
StreamWriter sw = new StreamWriter(path+"\\黄敬博.txt", false);
foreach (var skey in re)
{
v w = (v)ht[skey.Key];
sw.WriteLine(w.s + "\t" + w.n);
sw.Flush();
j++;
if (j > 10)
break;
}
}
}
}
}
代码总共由3部分组成,每部分对应相对的mode。
mode2、3是基于mode1基础上做出的一点点改变。
这个程序主要利用了Dictionary功能。key设为string,使用时将单词的小写形式作为key。value设为一个小结构,包含一个string类型的单词和一个int类型的次数。
mode1遍历的过程为按照单个单词遍历的,mode2为两个,mode3为三个。
但mode2读取完两个单词需要往回退一个,mode3读取完要往回退两个。
若读取的单词的小写形式在dictionary中已经包含了。则value中的int值+1,然后比较value中的string与当前的单词,选择ASC码靠前的那个保存。
最后按照要求输出。
Individual Project - Word frequency program by HJB的更多相关文章
- Limeng:Individual Project: Word frequency program -BUAA Advanced Software Engineering
11061190-李孟 Implement a console application to tally the frequency of words under a directory (2 mod ...
- Individual Project - Word frequency program - Multi Thread And Optimization
作业说明详见:http://www.cnblogs.com/jiel/p/3978727.html 一.开始写代码前的规划: 1.尝试用C#来写,之前没有学过C#,所以打算先花1天的时间学习C# 2. ...
- Individual Project - Word frequency program——12061154Joy
Description&Requirement: http://www.cnblogs.com/jiel/p/3978727.html 项目时间估计 理解项目要求: 1h 构建项目逻辑: 1h ...
- SoftwareEngineering Individual Project - Word frequency program
说实话前面c#实在没怎么学过.这次写起来感觉非常陌生,就连怎么引用名空间都忘记了.在经过恶补后还是慢慢地适应了. 1.项目预计用时: 构建并写出大概的数据结构,程序框架及模块: 30min 实现文件夹 ...
- Individual Project - Word frequency program
1.项目预计用时 -计划学习C#和百度一些用法的时间:5小时 -项目本身打算写两个类,一个是遍历搜索文件夹的,另外一个用来统计单词.计划用时:5小时 2.项目实际用时 学习C#以及正则表达式的用法:3 ...
- Record for Individual Project ( Word frequency program )
1. 预计时间 ● 对问题总体的理解.规划:10 min ● 设计编写程序:5 h ● 调试: 分模块-40 min; 总体-40min ● 测试(性能分析).改进:1 h 2. 实际用时 ● 对 ...
- THE First Individual Project - Word frequency program
第一次写博客,这次也是本学期写到第一个程序. 老师要求网址:http://www.cnblogs.com/jiel/p/3311400.html#2777556 一.项目预计时间 一开始想使用不熟悉的 ...
- Individual Project - Word frequency program-11061171-MaoYu
BUAA Advanced Software Engineering Project: Individual Project - Word frequency program Ryan Mao (毛 ...
- Project: Individual Project - Word frequency program----11061192zmx
Description & Requirements http://www.cnblogs.com/jiel/p/3311400.html 项目时间估计 理解项目要求: 1小时 构建项目逻辑: ...
随机推荐
- sql注入--mysql
mysql数据库结构: 数据库A --> 表名 --> 列名 --> 数据 数据库B --> 表名 --> 列名 --> 数据 mysql数据库信息: my ...
- 团队作业——Beta冲刺5
团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:完成详情预览界面的开发. 吴松青 今日任务:加入了详情界面的显示感想部分,并完成部分布局. 赖志平 今日任务:美化界面,配图配色,功能完善, ...
- Oracle Spatial中SDO_Geometry说明
Oracle Spatial中SDO_Geometry说明 在ArcGIS中通过SDE存储空间数据到Oracle中有多种存储方式,分别有:二进制Long Raw .ESRI的ST_Geometry以及 ...
- python openpyxl.md
Openpyxl 创建一个工作簿 下面是创建一个工作簿.而每个工作簿至少一个工作表我们可以通过active获取正在运行的工作表. In [1]: from openpyxl import Workbo ...
- python第四十三课——封装性
1.面向对象的三大特性:封装性.继承性.多态性 封装: 封装使用的领悟: 1).生活层面:食品.快递.计算机.明星... 2).计算机层面: ①.模块.类.函数... ②.属性数据的封装与隐藏 权限修 ...
- python open 关于读、写、追加的总结
# -*- coding: utf-8 -*- # 测试文件名为: # text.txt # 测试文件内容为: # abcdefg # 每次操作后将文件复原 # r # 以只读方式打开文件,文件不可写 ...
- BZOJ3173:[TJOI2013]最长上升子序列(Splay)
Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...
- 客户端本地存储的比较及使用window.name数据传输
一:cookie: 1. 什么是cookie? Cookie是在客户端用于存储会话信息的,用户请求页面在web服务器与浏览器之间传递.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cooki ...
- kettle(一) 安装全过程
windows 10 环境 所有资料下载 https://pan.baidu.com/s/1vYZVJ3f0QJHsCWiupSp08A 一.下载kettle pdi-ce-7.1.0.0-12.zi ...
- [笔记] ubuntu下添加windows的字体
方法如下: 第一步:将windows下喜欢的字体文件copy到一个文件夹中,例如将XP里WINDOWS/FONTS中的字体文件(本人比较贪心,把整个文件夹copy了过来……),在linux中命名为xp ...