C#统计英文文本中的单词数并排序
思路如下:
1.使用的Hashtable(高效)集合,记录每个单词出现的次数
2.采用ArrayList对Hashtable中的Keys按字母序排列
3.排序使用插入排序(稳定)
public void StatisticsWords(string path)
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在!");
return;
}
Hashtable ht = new Hashtable(StringComparer.OrdinalIgnoreCase);
StreamReader sr = new StreamReader(path, System.Text.Encoding.UTF8);
string line = sr.ReadLine(); string[] wordArr = null;
int num = 0;
while (line.Length > 0)
{
// MatchCollection mc = Regex.Matches(line, @"\b[a-z]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
//foreach (Match m in mc)
//{
// if (ht.ContainsKey(m.Value))
// {
// num = Convert.ToInt32(ht[m.Value]) + 1;
// ht[m.Value] = num;
// }
// else
// {
// ht.Add(m.Value, 1);
// }
//}
//line = sr.ReadLine(); wordArr = line.Split(' ');
foreach (string s in wordArr)
{
if (s.Length == 0)
continue;
//去除标点
line = Regex.Replace(line, @"[\p{P}*]", "", RegexOptions.Compiled);
//将单词加入哈希表
if (ht.ContainsKey(s))
{
num = Convert.ToInt32(ht[s]) + 1;
ht[s] = num;
}
else
{
ht.Add(s, 1);
}
}
line = sr.ReadLine();
} ArrayList keysList = new ArrayList(ht.Keys);
//对Hashtable中的Keys按字母序排列
keysList.Sort();
//按次数进行插入排序【稳定排序】,所以相同次数的单词依旧是字母序
string tmp = String.Empty;
int valueTmp = 0;
for (int i = 1; i < keysList.Count; i++)
{
tmp = keysList[i].ToString();
valueTmp = (int)ht[keysList[i]];//次数
int j = i;
while (j > 0 && valueTmp > (int)ht[keysList[j - 1]])
{
keysList[j] = keysList[j - 1];
j--;
}
keysList[j] = tmp;//j=0
}
//打印出来
foreach (object item in keysList)
{
Console.WriteLine((string)item + ":" + (string)ht[item]);
}
}
C#统计英文文本中的单词数并排序的更多相关文章
- python统计一个文本中重复行数的方法
python统计一个文本中重复行数的方法 这篇文章主要介绍了python统计一个文本中重复行数的方法,涉及针对Python中dict对象的使用及相关本文的操作,具有一定的借鉴价值,需要的朋友可以参考下 ...
- python统计英文文本中的回文单词数
1. 要求: 给定一篇纯英文的文本,统计其中回文单词的比列,并输出其中的回文单词,文本数据如下: This is Everyday Grammar. I am Madam Lucija And I a ...
- C++语言,统计一篇英文文章中的单词数(用正则表达式实现)
下面的例子展示了如何在C++11中,利用regex_search()统计一篇英文文章中的单词数: #include <iostream> #include <regex> #i ...
- 统计英文文章中各单词的频率,打印频率最高的十个单词(C语言实现)
一.程序思路及相关代码 首先打开文件,代码如下 FILE *fp; char fname[10]; printf("请输入要分析的文件名:\n"); scanf("%s ...
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- HashMap 统计一个字符串中每个单词出现的次数
HashMap 统计一个字符串中每个单词出现的次数 import java.util.HashMap; import java.util.Map; public class Test { public ...
- Perl-统计文本中各个单词出现的次数(NVDIA2019笔试)
1.原题 2.perl脚本 print "================ Method 1=====================\n"; open IN,'<','an ...
- 翻译器DIY它———算在英文文本中的单词数,字符和行数
咳咳.这部分应该是序列化编译器DIY的,然而,在这样做DIY第一次使用前flex 为了练练手,对于后者的理解是有帮助. 在word 我经常看到一个字计数功能,因此,它是如何实现,当然,首先想到的是要经 ...
- 使用hadoop统计多个文本中每个单词数目
程序源码 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Con ...
随机推荐
- C# 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke [问题点数:40分
注意: this.DateTimeRun = true; new Thread(jishi_kernel).Start(); 线程的启动,不能放在 public Form ...
- jq 遍历 each方法
1.选择器+遍历 $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); 2.选择器+遍历 $('div').each(function ...
- 【307】◀▶ Python 相关功能实现
目录: 1. Python 实现下载文件 2. 删除文件名中的点 “.” 3. 让 Python 脚本暂停执行的方法 4. 添 1. Python 实现下载文件 使用 urllib 模块提供的 url ...
- url_encode and url_decode in Shell
之前写过一版 shell下解码url,下面给出另外一个版本 from https://gist.github.com/cdown/1163649 function urlencode() { loca ...
- C++ new到底new什么
VS2010-Debug版本 9: int *obj = new int(6); 00F714CE push //压栈 00F714D0 call operator new (0F711EAh) // ...
- redis cluster test
cp /test/tests/redis.conf /etc redis-server /etc/redis.conf redis-trib.rb create --replicas 1 172.17 ...
- swarmkit test
swarmd -d /tmp/node-1 --listen-control-api /tmp/node-1/swarm.sock --hostname mhc --engine-addr=tcp:/ ...
- solr的客户端操作:使用solrj进行curd操作
导入相关的jar包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-s ...
- linux ifconfig显示 command not found
本人装的是centos7 想看下网络配置 结果显示如图: 正常情况下 ifconfig 是在超级管理员 的所属的目录 sbin/下的命令 现在来查看该目录下. 没有找到,别急 用 yum sear ...
- sqlserver分区表索引
对于提高查询性能非常有效,因此,一般应该考虑应该考虑为分区表建立索引,为分区表建立索引与为普通表建立索引的语法一直,但是,其行为与普通索引有所差异. 默认情况下,分区表中创建的索引使用与分区表相同分区 ...