Tip: you can modify some code before running
News title:<input type="text" size="50" name="keywords" id="keywords" /><br /><br /><input type="button" onclick="lastIndexDemo();" value="提取关键词"/><br /><br /><input type="text" size="50" name="tags" id="tags" /><script>var keylis = new Array("贸易","制裁","改造","中国","经济","美国","银行","速度","机电","进出口","商会","彩电","税率","国家","家电","电信","产业","电子","信息","产销","俄罗斯","调查","争端","投资","受挫","实行","临时","价格","季度","扬言","演示简单","后台","工业","电器");var keydrop="";function delHtmlTag(str) { return str.replace(/<[^>]+>/g,"");//去掉所有的html标记 }function lastIndexDemo(a){var str = delHtmlTag(document.getElementById("keywords").value);//获取字符串var keys = new Array;//词表存储序列var titles = new Array;var key = new Array;//关键词对象存储序列var gotkey = new Array();//关键词对象var name = new Array();//关键词namevar address = new Array();//关键词在词表中位置var times = new Array();//关键词在本篇目中的出现次数var tfx = new Array();//关键词tfx值var stopkey = new Array();//关键词是否为停用词var desc = new Array();//关键词排名var strkey;// 声明变量预存关键字var strl = str.length;//获取字符串长度getkeywords(keys,titles);//初始化关键词表和标题序列getkey(str,strkey,strl,keys,key,name,address,stopkey);//获取关键词,词表位置,是否停用timesn(times,address);//获取n(出现多少次)gettfx(times,tfx);toobject(key,address,times,tfx,stopkey,name);outresult(key,address,times,tfx,stopkey,name,str);}function getkeywords(keys,titles){var titl = 1;var keyl = keylis.length;var keyd = keydrop.length;for(i=0;i<keyl;i++){keys[i] = keylis[i];}for(i=0;i<keyd;i++){keys[i+keyl] = keydrop[i].childNodes[0].nodeValue;}for(i=0;i<titl;i++){titles[i] = document.getElementById("keywords").value;}}function timesn(times,address){var k = 0;for(i=0;i<address.length;i++){for(j=0;j<address.length;j++){if(address[i] == address[j]){k = k+1;}}times.push(k);k = 0;}}function gettfx(times,tfx){var k = Math.log(10);var l;var j;var m;var n;for(i=0;i<times.length;i++){l = times[i]/1;j = Math.log(l);n = times[i]*j;tfx.push(n.toFixed(3));}}function toobject(key,address,times,tfx,stopkey,name){var gotdkey = new Array;key["name"] = name;key["address"] = address;key["tfx"] = tfx;key["stopkey"] = stopkey;key["times"] = times;}function getkey(str,strkey,strl,keys,key,name,address,stopkey){for(k=strl;k>0;k--){//控制循环次数label:for(j=6;j>0;j--)//通过最大关键字长度控制循环{var strkey = str.substr(k-j, j);//确定预检索字符串 strl-j 是位置 j是长度for(i=0;i<keys.length;i++)//通过关键字字库的数量确定循环次数{if(keys[i]==strkey){//如果现有关键字与字库匹配name.push(strkey);address.push(i);if(i>keylis.length){stopkey.push(false);}else{stopkey.push(true);}k-=j;k++;break label;}}}}}function outresult(key,address,times,tfx,stopkey,name,str){var outtags = document.getElementById("tags");var indesc = 100;var intfx = "";var outkeyarray = new Array();var outwordarray = new Array();var outtfxarray = new Array();for(i=0;i<name.length;i++){if(key["stopkey"][i] == true){outkeyarray.push(key["name"][i]);outtfxarray.push(key["tfx"][i]);} }for(i=0;i<outkeyarray.length;i++){for(j=outkeyarray.length;j>i;j--){if(outkeyarray[i] == outkeyarray[j]){outkeyarray = outkeyarray.slice(0,j).concat(outkeyarray.slice(j+1,outkeyarray.length));outtfxarray = outtfxarray.slice(0,j).concat(outtfxarray.slice(j+1,outtfxarray.length));}}}outtags.value = outkeyarray.join(",");}</script>
Tip: you can modify some code before running