Author Topic: Find The Closet Matching String  (Read 1207 times)

My point is that you don't need several returns

Edit: I just took a better look at your code. Why also are you setting three variables to %file.readLine();?
One variable for each line.

Oh, I see what you were trying to do.
You should instead try putting a name then a space and then the ID. getWord would be used to get the ID value.

Oh, I see what you were trying to do.
You should instead try putting a name then a space and then the ID. getWord would be used to get the ID value.
But I don't want to do that.

The point is the code I posted does not work.

you don't need a while loop if you know how many times you are going to loop, unless you didn't know what you were doing when you added a delete and return at the end of the while loop.  The thing will stop looping if it hits a return, of course.


I'm not really understanding the if statement you have.  You also have no way of comparing lines to eachother when more than one possible match is found.

I'm not familiar with the strstr() function, so I am going to write this with an imaginary function strcom() which will return a float between 0 and 1 depending on how similar the strings are.  It will return a 1 with a perfect match.


Code: [Select]
function getID(%name)
{
%file = new FileObject();
%file.openForRead("config/Client/ChatBot/BL_IDs.txt");
%bestid = "-1";

while(!%file.isEOF())
{
%badName = %file.readLine();
%ID = %file.readLine();
%blank = %file.readLine();

%match = strcom(%badname, %name);

if(%match > %bestmatch)
{
%bestmatch = %match;
%bestid = %id;

if(%bestmatch == 1)
break;
}
}

%file.close();
%file.delete();

return %bestid;
}

you don't need a while loop if you know how many times you are going to loop, unless you didn't know what you were doing when you added a delete and return at the end of the while loop.  The thing will stop looping if it hits a return, of course.


I'm not really understanding the if statement you have.  You also have no way of comparing lines to eachother when more than one possible match is found.

I'm not familiar with the strstr() function, so I am going to write this with an imaginary function strcom() which will return a float between 0 and 1 depending on how similar the strings are.  It will return a 1 with a perfect match.


Code: [Select]
function getID(%name)
{
%file = new FileObject();
%file.openForRead("config/Client/ChatBot/BL_IDs.txt");
%bestid = "-1";

while(!%file.isEOF())
{
%badName = %file.readLine();
%ID = %file.readLine();
%blank = %file.readLine();

%match = strcom(%badname, %name);

if(%match > %bestmatch)
{
%bestmatch = %match;
%bestid = %id;

if(%bestmatch == 1)
break;
}
}

%file.close();
%file.delete();

return %bestid;
}
That's not too useful because as it is strCom() does not exist.

That's not too useful because as it is strCom() does not exist.

I basically handed you the entire everything except that, because there wasn't much you were doing right.  I am assuming strstr was the comparison function you could maybe figure out how to adapt to that code, because I have never used it before.

Ok looking at strstr, it doesn't seem very useful at all.  I might write you up some version of the strcom() thing if I have time.

Edit: this might work

function strcom(%sa, %sb)
{
   if(%sa $= %sb)
      return 1;
   if(%sa $= "" || %sb $= "")
      return 0;
   if(strlen(%sb) > strlen(%sa))
   {
      %temp = %sa;
      %sa = %sb;
      %sb = %temp;
   }
   if(strpos(%sa, %sb) > -1)
   {
      %leftovers = strlen(%sa) - strlen(%sb);
      %acc = 1 - strlen(%sa)/%leftovers;
      return %acc;
   }
   return 0;
}
« Last Edit: March 21, 2012, 10:39:27 PM by Nexus »

Goddamn it jes we aren't going to spoon feed you the answer here. Use a bit of logic, I got a version to work in a matter of minutes. I've already told you why the way you are trying to do is flawed

Edit: @Nexus the strStr if statement will grab the first match it finds, yes. This is potentially a problem, absolutely. Jes can write his own string comparison function (like you said) if he needs this function to work better than just grabbing the first match.
« Last Edit: March 21, 2012, 10:11:22 PM by Treynolds416 »

Check how many charachters in a row from the second string match the charachters in the first string, with a larger priority on the beginning than the middle or end. Then divide that number by the total charachter count.
It's just an idea, but i'm pretty sure thats how the findclientbyname function does it.

Check how many charachters in a row from the second string match the charachters in the first string, with a larger priority on the beginning than the middle or end. Then divide that number by the total charachter count.
It's just an idea, but i'm pretty sure thats how the findclientbyname function does it.

No, findclientbyname is literally a single strpos() for each name and returns the lowest one besides -1, if it even compares to find the one with the earliest match

Also I think the strcom() I wrote up should do the trick just fine.