javascript对中文、英文混合词语进行分词。

准备:

1、prototype.js,http://prototype.conio.net/,用于ajax加载汉字字库

2、汉字字库http://cn.minidx.com/index.php?option=com_docman&task=doc_download&gid=47

 ----------------------------------------------------------------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>中英文混合分词</title>
<script language="javascript" src="prototype.js"></script>
<script language="javascript">
var dict = "";
var lastword = "";
//ajax 装载字库
new Ajax.Request('js/dict.txt',{onComplete:function(response){dict=response.responseText+"";}});
var rs = [];
function divide(text){
    if(text.length==0) return true;
    var word = text.substring(0,1)+"";
    var regExp = //w/;
    //英文
    if(regExp.test(word)){
        var tmp = text.replace(/^/s*(/w+)/s*.*$/,"$1");
        text = text.replace(/^/s*/w+/s*/,"");
        rs.push(tmp);
        divide(text);
        return;
    }
   
    var words = [];
    var end = 0;
    var start = -1;
    while((start = dict.indexOf('/r/n'+word,end))!=-1){
        end = dict.indexOf('/r/n',start+1);
        if(start==-1||end==-1) return false;
        if(start>end) return false;
        words.push(dict.substr(start,end-start).replace(/(/r/n|/s)/g,""));
    }   
   
    var tmp = "";
    for(j=0;j<words.length;j++){
        //找到最长的词,当然也可以将所有词保留
        if(text.indexOf(words[j])!=-1&&words[j].length>tmp.length){
            tmp=words[j];
        }
    }
    //词库不存在的词
    if(tmp == ""){
        tmp = word;       
    }
 text=text.replace(tmp,"");
 if(tmp.replace(//s/g,'')!="")
     rs.push(tmp);
    divide(text);
}
function dodivde(){
    var text = $('word').value;
    rs = [];
    divide(text);
    $('dividresult').innerHTML=rs;
}

</script></head>

<body>
<input type="text" name="word" id="word" value="我Welcome欢迎to Mozilla Firefox Help" οnblur="dodivde();" /><input name="do" type="button" value="DO IT" οnclick="dodivde();" />
<span id="dividresult"></span>
</body>
</html>

-------------------------------------------------------------------------------------------------------------------

 

关键是词库。

转载于:https://www.cnblogs.com/Iamlein/archive/2008/05/11/2375942.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值