计算文本相似度,应用场景有很多,比如SEO的伪原创检测,毕业论文的检测等,都能产生商业价值,下面是最简单的算法,比较两个文本中含有多少相同的字,适用于只有几个字的情况.如果是大段的或长篇的文章,则需要按句子拆法等方法去检测.下面代码仅供参考:
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="计算文本相似度";left=261;top=413;right=456;bottom=449;z=2};
edit={cls="edit";left=13;top=8;right=753;bottom=386;edge=1;multiline=1;z=1}
)
/*}}*/
import console;
winform.button.oncommand = function(id,event){
var txt1 = "简码编程";
var txt2 = "简码编程教程好棒";
//先判断哪个字数多
var 相同字数 = 0;
var 相似度 = 0;
var txt1Len = string.len(txt1);
var txt2Len = string.len(txt2);
if( txt1Len >= txt2Len ){
//winform.edit.print( "txt1Len >= txt2Len")
var txt2Tab = string.split(txt2)
//console.varDump(txt2Tab)
for(i=1;table.count(txt2Tab);1){
if(string.find(txt1,txt2Tab[i])){
相同字数++;
}
}
相似度 = 相同字数/txt1Len;
winform.edit.print( "相同字数:",相同字数,"长句字数:",txt1Len,"相似度:",相似度)
}else {
//winform.edit.print( "txt1Len < txt2Len")
var txt1Tab = string.split(txt1)
//console.varDump(txt1Tab)
for(i=1;table.count(txt1Tab);1){
if(string.find(txt2,txt1Tab[i])){
相同字数++;
}
}
相似度 = 相同字数/txt2Len;
winform.edit.print( "相同字数:",相同字数,"长句字数:",txt2Len,"相似度:",相似度)
}
}
winform.show()
win.loopMessage();