Topic: 帮我看看,哪里错了,头晕脑涨了(关于字符匹配度)

  Print this page

1.帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) Copy to clipboard
Posted by: pn526
Posted on: 2005-07-26 17:21

请使用准确的文字描述作为标题
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.printlnLight Bulb;
}
}
}
%>

</body>
</html>

帮我看看,弄不出来!3q

2.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: why
Posted on: 2005-07-26 19:52

I don't know why you would put your implementation directly in a jsp, which would be hard to debug.

3.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: why] Copy to clipboard
Posted by: pn526
Posted on: 2005-07-26 19:55

why wrote:
I don't know why you would put your implementation directly in a jsp, which would be hard to debug.

我的输入页面已经写好了,就直接把s付值了.这样检测的时候就不用执行第一页面了.这个应该说不是很大的问题把.

4.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: pn526
Posted on: 2005-07-26 19:56

问题出在了76行

5.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: why
Posted on: 2005-07-26 20:35

pn526 wrote:
我的输入页面已经写好了,就直接把s付值了.这样检测的时候就不用执行第一页面了.这个应该说不是很大的问题把.

If I were you, I would pull the function out and debug -- most probably prepare a unit test suite for it.

问题出在了76行

p1=1;p2=1; Sad
Java is zero-based.

6.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: why] Copy to clipboard
Posted by: pn526
Posted on: 2005-07-26 20:41

why wrote:
If I were you, I would pull the function out and debug -- most probably prepare a unit test suite for it.

p1=1;p2=1; Sad
Java is zero-based.

我改成p1=0;p2=0;也会报同样的错.我就改来改去.两天了没有搞明白错在哪里?
不过还是很感谢你啊!

7.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: pn526
Posted on: 2005-07-27 10:01

java.lang.ArrayIndexOutOfBoundsException: 4
76行提示这个错误.过不去!

8.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: why
Posted on: 2005-07-27 11:48

pn526 wrote:
java.lang.ArrayIndexOutOfBoundsException: 4
76行提示这个错误.过不去!

That's why I would pull the function out and debug -- don't you know how to use a debugger?

You have only corrected p1=1;p2=1;
Don't you think that all the conditional statements that involve p1 & p2 might have to be changed as well?

9.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: pn526
Posted on: 2005-07-27 17:07

谢谢.我再想想.我反映有点慢!
谢谢why大哥!

10.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-07-28 07:24

pn526 wrote:

我写个程序关于字符匹配度的.弄不出来.是参考一个pascal的程序来的.
关键词:字符串匹配度
'John' and 'John' = 100%
'John' and 'Jon' = 75%
'Jim' and 'James' = 40%
"Luke Skywalker" and 'Darth Vader' = 0%


What kind of stupid definition of "字符串匹配度"?

"Luke Skywalker" and 'Darth Vader' = 0%

??????????????????????

I did not waste my time to read the code, since it does not worth to read, let alone embed code in JSP!!!

I don't know why Mr. WHY bothered to read / help on this kind of stuff?
==================================================
To pn526:
Throw the code into GC, do something meaningful or useful!

However, if you have no choice and really need to do this, ie. it is a HW assignment or so, then listen to Mr. WHY, put your code into a standalone program, compile it, run it, test it, make it do the "right" stupid things as specified. Then you can cut and paste into your JSP, and test it again!

Happy learning and good luck!

11.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: gongshi] Copy to clipboard
Posted by: why
Posted on: 2005-07-28 10:15

gongshi wrote:
What kind of stupid definition of "字符串匹配度"?

...
I did not waste my time to read the code, since it does not worth to read, let alone embed code in JSP!!!

I don't know why Mr. WHY bothered to read / help on this kind of stuff?

Frankly I didn't read what the 字符串匹配度 is, and I haven't read the code except line 76 and the few lines around it.
In fact I would try to answer or redirect all the topics posted on this board if the posters don't request 緊急幫忙 or 詳細解釋.Smile

As an administrator, I don't think I could use words like stupid, garbage, etc.Embaressed

12.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: pn526
Posted on: 2005-07-28 10:26

谢谢二位.不论怎样.十分感谢.
gongshi和why大哥!

13.Re:帮我看看,哪里错了,头晕脑涨了(关于字符匹配度) [Re: pn526] Copy to clipboard
Posted by: lvmingtao
Posted on: 2005-07-28 15:52

好贴子 ~~~斑竹请留下~~~~巫山


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923