请使用准确的文字描述作为标题
Your next post without a proper Subject will be removed.
Original subject : 帮我看看,哪里错了,头晕脑涨了
我写个程序关于字符匹配度的.弄不出来.是参考一个pascal的程序来的.
关键词:字符串匹配度
'John' and 'John' = 100%
'John' and 'Jon' = 75%
'Jim' and 'James' = 40%
"Luke Skywalker" and 'Darth Vader' = 0%
function StrSimilar (s1, s2: string): Integer;
var hit: Integer; // Number of identical chars
p1, p2: Integer; // Position count
l1, l2: Integer; // Length of strings
pt: Integer; // for counter
diff: Integer; // unsharp factor
hstr: string; // help var for swapping strings
// Array shows is position is already tested
test: array [1..255] of Boolean;
begin
// Test Length and swap, if s1 is smaller
// we alway search along the longer string
if Length(s1) < Length(s2) then begin
hstr:= s2; s2:= s1; s1:= hstr;
end;
// store length of strings to speed up the function
l1:= Length (s1);
l2:= Length (s2);
p1:= 1; p2:= 1; hit:= 0;
// calc the unsharp factor depending on the length
// of the strings. Its about a third of the length
diff:= Max (l1, l2) div 3 + ABS (l1 - l2);
// init the test array
for pt:= 1 to l1 do test[pt]:= False;
// loop through the string
repeat
// position tested?
if not test[p1] then begin
// found a matching character?
if (s1[p1] = s2[p2]) and (ABS(p1-p2) <= diff) then begin
test[p1]:= True;
Inc (hit); // increment the hit count
// next positions
Inc (p1); Inc (p2);
if p1 > l1 then p1:= 1;
end else begin
// Set test array
test[p1]:= False;
Inc (p1);
// Loop back to next test position if end of the string
if p1 > l1 then begin
while (p1 > 1) and not (test[p1]) do Dec (p1);
Inc (p2)
end;
end;
end else begin
Inc (p1);
// Loop back to next test position if end of string
if p1 > l1 then begin
repeat Dec (p1); until (p1 = 1) or test[p1];
Inc (p2);
end;
end;
until p2 > Length(s2);
// calc procentual value
Result:= 100 * hit DIV l1;
end;
我自己用java写的如下.
<%@ page contentType="text/html; charset=GBK" import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.lang.*"%>
<%!
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
public void jspInit()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:aim");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
public void jspDestory()
{
try
{
stmt.close();
con.close();
rs.close();
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
%>
<html>
<head>
<title>
result
</title>
</head>
<body bgcolor="#ffffff">
<%
int p1,p2,l2,l1,diff,pt,i=0,num=0;
String swap,s;
boolean test[]=new boolean[255];
s="fdddd";//request.getParameter("txt");
rs=stmt.executeQuery("SELECT * FROM [table]");
while(rs.next())
{
num++;
}
int hit[]=new int[num];
float result[]=new float[num];
String str[]=new String[num];
rs=stmt.executeQuery("SELECT * FROM [table]");
while(rs.next())
{
str[i]=rs.getString("content");
if( s.length()>str[i].length())
{
swap=str[i];
str[i]=s;
s=swap;
}
l2=s.length();l1=str[i].length();
p1=1;p2=1;
for(i=0;i<num;i++)
{hit[i]=0;}
//diff=Math.max(l1,l2)/3+Math.abs(l1-l2);
for(pt=0;pt<l1;pt++)
{test[pt]=false;}
while(p2<l2)
{
if(!test[p1])
{
if(str[i].charAt(p1)==s.charAt(p2))//&&(Math.abs(p1-p2)<=diff))
{
test[p1]=true;
hit[i]=hit[i]+1;
p1++;p2++;
if(p1>l1){p1=1;}
}
else
{
test[p1]=false;
p1++;
if(p1>l1)
{
while(p1>1&&!test[p1])
{p1--;}
p2++;
}
}
i++;
}
else
{
if(p1>l1)
{
while((p1==1)||(test[p1]))
{p2++;}
}
}
}
/*for(p1=0;p1<l1[i];p1++)
{
for(p2=0;p2<s.length();p2++)
{
if(s.charAt(p2)==str[i].charAt(p1))
{
hit[i]++;
}
}
}
*/
for(i=0;i<num-1;i++)
{
result[i]=hit[i]/l1;
if(result[i]>=result[i+1])
{
out.println;
}
}
}
%>
</body>
</html>
帮我看看,弄不出来!3q