当前位置:asp编程网>技术教程>Javascript教程>  正文

javascript自动获取标题中的关键词

1970-01-01 08:00:00   来源:asp编程网    作者:佚名   浏览量:2548   收藏

整理一个自动提示标题关键词的JS,也可以用它来提示内容中的关键词,自已改一下就行了。
javascript脚本中的keylis是词组,放入你网站经常使用的词语进去,自己来扩充关键词。
功能:自动获取表单中的关键词
整理:wangsdong
来源:www.aspprogram.cn
点击下面的运行按纽,然后在新闻标题处输入一些文件,再点击页面上的提取关键词,就可以看到效果了。
注意:这个标题中必须有"贸易","制裁","改造","中国","经济","美国","银行","速度","机电","进出口","商会","彩电","税率","国家","家电","电信","产业","电子","信息","产销","俄罗斯","检察官","检察","调查","欧盟","争端","投资","受挫","改革","实行","临时","价格","季度","扬言","演示简单","后台","工业","电器"中的一些词语才可以提取出关键词。这些词需要根据你自己需要添加的。
[code]新闻标题:<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();//关键词name
var 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>
[/code]


关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网