/*
功能:
C#统计文章中单词的重复次数,并且按照次数从高到低排序返回(无法处理中文)
例子:
i am a big boy,how a bout boy? 返回boy(2),i(1),....等。
命名空间为:
using System.Collections.Generic;
using System.Linq;
*/
Dictionary<string, int> gethotstring(string content)
{
Dictionary<string, int> HOT = new Dictionary<string, int>();
string[] s = content.Split(new char[] { ' ' });
for(int i=0;i<s.Length;i++)
{
if(HOT.ContainsKey(s[i]))
{
HOT[s[i]]++;;
}
else
{
HOT[s[i]]=1;
}
}
return HOT.OrderByDescending(r => r.Value).ToDictionary(r => r.Key, r => r.Value);
}
处理返回的字典代码如下:
Dictionary<string, int> MEWHOT=gethotstring(content);
string output=null;
//遍历字典
int size=0;
foreach (KeyValuePair<string, int> kvp in MEWHOT)
{
size++;
if(size>10)break;
output+=kvp.Key+"("+kvp.Value.ToString()+")";
output+="\r\n";
}
注意