C# 实现统计字符串中不同单词的出现次数【百度笔试题】
2011-12-30 13:38
681 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AlgorithmDemo
{
/*一个文本文件里面记录了很多内容,主要由各种字符组成,如字母、数字、中文、各种标点符号等。
* 请编写程序分析这个文本文件里的字符串中不同单词的出现次数。
* 单词的定义,满足其中一条的就算一个单词:
* a.纯数字组成的(如:123)
* b.纯字母组成的(如:abdsd)
* c.小数形式的(如:123.456)
* d.中文,一个中文算一个单词
* */
public class 单词统计
{
public Dictionary<string, int>[] StatItemCount(string strs)
{
//存放结果数组 0其它符号,1字母,2数字,3汉字,
Dictionary<string, int>[] value = new Dictionary<string, int>[4] {
new Dictionary<string, int> { }
, new Dictionary<string, int> { }
, new Dictionary<string, int> { }
, new Dictionary<string, int> { } };
StringBuilder tStr = new StringBuilder();
char tChar;
int perType = 0;//前一此字符类型 取值与结果数组对应 0其它符号,1字母,2数字,3汉字,
for (int i = 0; i < strs.Length; i++)
{
tChar = strs[i];
if (perType == 1)//上次是字母单词
{
if (!isLetter(tChar))//如果当前字符不是字母
{
CountWords(value[perType], tStr);
tStr.Clear();
}
}
else if (perType == 2)//上次的是数字
{
if (!isNumber(tChar))//如果当前字符不是数字
{
if (tChar == '.' && i + 1 < strs.Length && Match(strs[i + 1], numRegex))//如果是“.”,下一位如果为数字则加入
{
tStr.Append(tChar).Append(strs[i + 1]);
i = i + 1;
continue;
}
else
{
CountWords(value[perType], tStr);
tStr.Clear();
}
}
}
if (isLetter(tChar))//如果当前是字母
{
perType = 1;
tStr.Append(tChar);
}
else if (isNumber(tChar))//如果当前是数字
{
perType = 2;
tStr.Append(tChar);
}
else if (isChinese(tChar))//如果当前是汉字
{
perType = 3;
CountWords(value[3], tChar);
tStr.Clear();
}
else
{
perType = 0;
CountWords(value[0], tChar);
tStr.Clear();
}
}
return value;
}
private void CountWords(Dictionary<string, int> counts, object key)
{
if (!counts.ContainsKey(key.ToString()))
counts[key.ToString()] = 0;
counts[key.ToString()] = counts[key.ToString()] + 1;
}
string numRegex = "[0-9]";
string letterRegex = "[A-Za-z]";
string chineseRegex = "[\u4e00-\u9fa5]";
private bool isLetter(char c)
{
return Match(c, letterRegex);
}
private bool isNumber(char c)
{
return Match(c, numRegex);
}
private bool isChinese(char c)
{
return Match(c, chineseRegex);
}
private bool Match(object source, object regex)
{
return System.Text.RegularExpressions.Regex.IsMatch(source.ToString(), regex.ToString());
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AlgorithmDemo
{
/*一个文本文件里面记录了很多内容,主要由各种字符组成,如字母、数字、中文、各种标点符号等。
* 请编写程序分析这个文本文件里的字符串中不同单词的出现次数。
* 单词的定义,满足其中一条的就算一个单词:
* a.纯数字组成的(如:123)
* b.纯字母组成的(如:abdsd)
* c.小数形式的(如:123.456)
* d.中文,一个中文算一个单词
* */
public class 单词统计
{
public Dictionary<string, int>[] StatItemCount(string strs)
{
//存放结果数组 0其它符号,1字母,2数字,3汉字,
Dictionary<string, int>[] value = new Dictionary<string, int>[4] {
new Dictionary<string, int> { }
, new Dictionary<string, int> { }
, new Dictionary<string, int> { }
, new Dictionary<string, int> { } };
StringBuilder tStr = new StringBuilder();
char tChar;
int perType = 0;//前一此字符类型 取值与结果数组对应 0其它符号,1字母,2数字,3汉字,
for (int i = 0; i < strs.Length; i++)
{
tChar = strs[i];
if (perType == 1)//上次是字母单词
{
if (!isLetter(tChar))//如果当前字符不是字母
{
CountWords(value[perType], tStr);
tStr.Clear();
}
}
else if (perType == 2)//上次的是数字
{
if (!isNumber(tChar))//如果当前字符不是数字
{
if (tChar == '.' && i + 1 < strs.Length && Match(strs[i + 1], numRegex))//如果是“.”,下一位如果为数字则加入
{
tStr.Append(tChar).Append(strs[i + 1]);
i = i + 1;
continue;
}
else
{
CountWords(value[perType], tStr);
tStr.Clear();
}
}
}
if (isLetter(tChar))//如果当前是字母
{
perType = 1;
tStr.Append(tChar);
}
else if (isNumber(tChar))//如果当前是数字
{
perType = 2;
tStr.Append(tChar);
}
else if (isChinese(tChar))//如果当前是汉字
{
perType = 3;
CountWords(value[3], tChar);
tStr.Clear();
}
else
{
perType = 0;
CountWords(value[0], tChar);
tStr.Clear();
}
}
return value;
}
private void CountWords(Dictionary<string, int> counts, object key)
{
if (!counts.ContainsKey(key.ToString()))
counts[key.ToString()] = 0;
counts[key.ToString()] = counts[key.ToString()] + 1;
}
string numRegex = "[0-9]";
string letterRegex = "[A-Za-z]";
string chineseRegex = "[\u4e00-\u9fa5]";
private bool isLetter(char c)
{
return Match(c, letterRegex);
}
private bool isNumber(char c)
{
return Match(c, numRegex);
}
private bool isChinese(char c)
{
return Match(c, chineseRegex);
}
private bool Match(object source, object regex)
{
return System.Text.RegularExpressions.Regex.IsMatch(source.ToString(), regex.ToString());
}
}
}
相关文章推荐
- 利用Map实现的统计每个输入字符串或则单词出现的次数
- mysql自定义函数实现统计一个字符串在另一个长字符串中出现的次数
- F# 统计一段英文文章中不同单词出现的次数
- PHP实现统计所有字符在字符串中出现次数的方法
- 微软笔试题 统计英文电子书中出现次数最多的k个单词
- (转)F# 统计一段英文文章中不同单词出现的次数
- 统计一段字符串中单词和字母各出现的次数
- 【百度2012年10月20日笔试】2.求字符串中连续重复字母的最大重复次数, 如: aabbbbcadd 的最大字母重复次数为 4, 有四个连续的b. 要求用递归实现.
- Java实现统计一篇文章中每个单词出现的次数
- 一个笔试面试经常问的问题——统计字符串中出现最多的字符及次数
- 集合框架_统计字符串中每个字符出现的次数案例代码实现
- Java实现统计某字符串在另一个字符串中出现的次数
- 金山2014秋招笔试题——统计单词在一篇文章中出现的次数和位置
- java 正则表达式查找某段字符串中所有小写字母开头的单词并统计次数,按出现次数排序
- 编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
- Java编程实现统计一个字符串中各个字符出现次数的方法
- 统计分析文章中英文单词出现次数及频率(C++实现)
- 实现给定一个字符串剔除重复字符,并统计各个字符出现的次数
- 统计一串字符串中出现次数最多和次多的单词(华为上机考试题)