110,577
社区成员
发帖
与我相关
我的任务
分享
//string content="文章内容"
string[] a=content.Split(new char[]{'\n',' ',',',';','.'});
foreach (string tmpStr in a)
{
//记数操作
}
//参考
private void button1_Click(object sender, EventArgs e)
{
List <KeyValuePair <string, int> > L = cutWord(this.richTextBox1.Text);
this.richTextBox2.Text = " ";
int count = 0;
for (int i = L.Count-1; i > 0; i--)
{
if (count > 15)
{
break;
}
count++;
this.richTextBox2.Text += L[i].Key + "[ " + L[i].Value + "]\n ";
}
}
private List <KeyValuePair <string, int> > cutWord(string article)
{
Dictionary <string, int> D = new Dictionary <string, int> ();
//if len(escape(x)) /len(x)=6 then isGB=true else isGB=false
//HttpUtility..::
System.Text.RegularExpressions.Regex Re = new System.Text.RegularExpressions.Regex(@"[^\u4e00-\u9fa5]+ ");
for (int l = 2; l <= 4; l++)
{
for (int i = 0; i < article.Length-l; i++)
{
string theWord = article.Substring(i, l);
if (Re.Replace(theWord, " ") == theWord)
{
if (D.ContainsKey(theWord))
{
D[theWord]++;
}
else
{
D.Add(theWord,1);
}
}
}
}
List <KeyValuePair <string, int> > L = new List <KeyValuePair <string, int> > ();
foreach (KeyValuePair <string, int> K in D)
{
if (K.Value > 1)
{
L.Add(K);
}
}
L.Sort(delegate(KeyValuePair <String, int> a, KeyValuePair <String, int> b)
{
if (a.Value == b.Value)
{
if (a.Key.Length == b.Key.Length)
{
return 0;
}
else
{
if (a.Key.Length > b.Key.Length)
{
return 1;
}
else
{
return -1;
}
}
}
if (a.Value > b.Value)
{
return 1;
}
else
{
return -1;
}
} );
return (L);
}
循环所有单词
if (list.ContainsKey(word))
{
list[word]++;
}
else
{
list[word] = 1;
}
words = new string[list.Count];
count = new int[list.Count];
list.Keys.CopyTo(words);
list.Values.CopyTo(count);
Array.Sort(count, words);
SortWords();
private void SortWords()
{
int n1 = 0;
int n2 = 0;
string temp = "";
for (int i = 1; i < list.Count; i++)
{
n1 = count[i - 1];
n2 = count[i];
if (n1 == n2) //词频出现相同
{
if (String.Compare(words[i - 1], words[i]) > 0) //比较单词大小
{
temp = words[i - 1];
words[i - 1] = words[i];
words[i] = temp; //把两个单词反转,因为出现次数是相同的,所以count数组是不用反转的
SortWords(); //递归调用,直到单词不再需要反转的时候
break; //因为已用递归不需重复执行,跳出循环
}
}
}
}