.NET2.0泛型就是爽!写了一个分词算法,抖一抖代码.感谢chenzq!

超级大笨狼 2007-09-14 05:00:24
winform中拖一个按钮,两个RichText
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace cutWord
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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);

}
}
}

测试Sohu首页关键字和频率:
女人[20]
搜狐[16]
手机[13]
游戏[13]
博客[13]
明星[11]
留学[10]
美女[9]
大学[9]
上海[9]
中国[9]
北京[9]
奥运[9]
曝光[9]
开盘[9]
视频[8]

晕!女人排第一
新浪:

中国[22]
视频[20]
手机[14]
上海[13]
新浪[13]
北京[12]
北大[12]
国际[12]
投资[11]
清华[11]
博客[11]
直播[11]
大学[10]
留学[10]
项目[9]
赚钱[9]

再看CSDN首页:
软件[16]
程序[13]
程序员[11]
序员[11]
开发[11]
大会[7]
微软[7]
我的[7]
专区[6]
技术[6]
中国[5]
使用[5]
我们[5]
模式[5]
系统[5]
人才[5]


...全文
2419 121 打赏 收藏 转发到动态 举报
写回复
用AI写文章
121 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyfer 2008-12-03
  • 打赏
  • 举报
回复
只是算词频,并不能真正搞到关键字.
dupoutVIP 2008-04-07
  • 打赏
  • 举报
回复
UP
超级大笨狼 2007-09-19
  • 打赏
  • 举报
回复
<script language="JavaScript">
var arrTemp=[["一个",1289],["秦可卿",493],["这个",935],["就是",922]];
arrTemp = arrTemp.sort(function(a,b){return(a[1]-b[1])})
alert(arrTemp);
</script>
超级大笨狼 2007-09-19
  • 打赏
  • 举报
回复
靠,js数组同样可以玩匿名函数排序.

<script language="JavaScript">
var arrTemp=[["一个",1289],["秦可卿",493],["这个",935],["就是",922]];
function newSort(a,b)
{
var a1 = a[1];
var b1 = b[1];
if(a1>b1)return 1;
if(a1==b1)return 0;
if(a1<b1)return -1;
}
arrTemp = arrTemp.sort(newSort)
alert(arrTemp);
</script>
sd5923150 2007-09-18
  • 打赏
  • 举报
回复
好用在于,不用显示类型转换这一步,个人观点
liusong_china 2007-09-18
  • 打赏
  • 举报
回复
@_@
metest 2007-09-18
  • 打赏
  • 举报
回复
jf
ycg_893 2007-09-18
  • 打赏
  • 举报
回复
up
超级大笨狼 2007-09-18
  • 打赏
  • 举报
回复
减少了很多代码和命名,也同时让我对delegate,接口,泛型有了进一步的了解,感谢小泉chenzq,你太有才了!~~
超级大笨狼 2007-09-18
  • 打赏
  • 举报
回复
这里是一个关键的技巧:用匿名函数代替类和接口的实现
L.Sort(
delegate(KeyValuePair<String, int> a, KeyValuePair<String, int> b)
{
return (a.Value - b.Value);
}
);
如果不用上面的,就要这样写,增加一个类:
internal class myCompare : System.Collections.Generic.IComparer<KeyValuePair<String, int>>
{

public int Compare(KeyValuePair<String, int> a, KeyValuePair<String, int> b)
{
return (a.Value - b.Value);
}


}
//凋用的地方这样写:
myCompare mp=new myCompare();
L.Sort(mp.Compare);
amandag 2007-09-18
  • 打赏
  • 举报
回复
收藏
超级大笨狼 2007-09-18
  • 打赏
  • 举报
回复
缓存后对比就可以了.
超级大笨狼 2007-09-18
  • 打赏
  • 举报
回复
词库有了;
只是这里没法展示词库.

缓存后排除重复的就可以了.
kirinboy 2007-09-18
  • 打赏
  • 举报
回复
关键是词库啊,这种单纯的字符串操作没有什么意义……
huheng_0_0 2007-09-18
  • 打赏
  • 举报
回复
study
zhchg6666 2007-09-18
  • 打赏
  • 举报
回复
mark
watson110 2007-09-18
  • 打赏
  • 举报
回复
study~~~~~~~
appaappf 2007-09-18
  • 打赏
  • 举报
回复
傻子来了,一切都躲开阿,给我1分
liujiayu10 2007-09-18
  • 打赏
  • 举报
回复
不错,借鉴一下
shanminmin 2007-09-18
  • 打赏
  • 举报
回复
up
加载更多回复(101)

7,765

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧