Jump to content
UBot Underground

Adapting a JavaScript PROTOTYPE object to a Ubot script


Recommended Posts

I have a problem.

 

I am attempting to adapt a JavaScript  function I found online (http://joshaven.com/string_score/) to a project I am working on. I am attempting to create a function that compares two words and, based upon the level of fuzziness, returns a value between 0 and 1.  I would suppose this would be similar to how Google and other search engines implement, spell check, give suggestions on misspell words, etc.  Background info to the concept can be found here http://en.wikipedia.org/wiki/Approximate_string_matching . Once I have the fuzzy index, I can determine if it is a close enough approximation to consider the two words a match.  In short, I am taking two lists of names, one  of participants attended and one of all attendees listed for an event.  This will allow me to keep track of who has and has not attended, even if they do not spell their name precisely the way we have in our attendee list (i.e. hyphenated names, different variations of names, etc.).

 

I have found a great tutorial online from VaultBoss at http://www.ubotstudio.com/forum/index.php?/topic/12285-tutorial-example-codes-how-to-use-javascript-with-ubot-studio/  (and thank you).  However, the JavaScript used involves creating a prototype and I am not as familiar with how to adapt the the new object/function to the example above.  The 1st part of the code from the fuzzy match is ...

/**
 * Scores a string against another string.
 *  'Hello World'.score('he');     //=> 0.5931818181818181
 *  'Hello World'.score('Hello');  //=> 0.7318181818181818
 */
String.prototype.score = function(abbreviation, fuzziness) {
  // If the string is equal to the abbreviation, perfect match.
  if (this == abbreviation) {return 1;}
  //if it's not a perfect match and is empty return 0
  if(abbreviation == "") {return 0;}
~~~~~~~~CODE REMOVED FOR BREVITY~~~~~~~~
  return final_score;
};

I also am not able to track the return value from the adapted Ubot script "HexSTR", the value returned from the actual JavaScript.

 

Rather than post all of my code here. I am attaching the script I have thus far for my bot (again, thanks VaultBoss) along with comments at the bottom.

 

Thank you

 

 

FUZZY UBOT CODE.txt

Link to post
Share on other sites

place the define of VaultBoss example  on the top of the script then it will work
it is very important if u define a function  to place it in top of your script

...
String.prototype.score = function(abbreviation, fuzziness)

im not so near to prototype but this looks like a anonymous function

as i know that means u cant get a global return...

but im not 100% sure

Link to post
Share on other sites

alternatively you could use something like the php function i have in the example pack for the PHP Compiler

http://content.screencast.com/users/lowridertj/folders/Ubot%20Studio%20Plugins/media/bdf73657-3cfa-48ba-bafe-eb2692d20b17/Similarity%20between%20text%20percentage.png

 

Your solution i took at a look and i wasn't able to figure anything out with it on why its not returning any value.

Link to post
Share on other sites

fine TJ
but this is not a solution this is a promo for your php plugin
and it dont doesnt help ppl to understand how to deal with javascript inside ubot

  • Like 1
Link to post
Share on other sites

its not a promo.

 

i tried to help with his script and couldn't get it to return anything.  spent my time on it.  couldn't get it to work and offered an alternative.  It does similarly to what he was asking and if i have something that will work for his needs im absolutely going to offer it.

 

sorry you see it differently.

Link to post
Share on other sites

dont understand me wrong tj
your php plugin rocks  im sure

but at the end  ppl want to understand how to implement js / jquery / and all the other js-frameworks

 

even that a defined function in ubot has to be on top of a ubotscript  can not find in the official doku...
to say nothing of to deal with javascript too

 

what the hell we need more and more things implement if that stuff we even got has no documentation?!

Link to post
Share on other sites

i understand, im going to try to look at the OPs script again and see if i can figure out what is going on with it..   Im not the best by far with javascript, but at least if i can get it to echo (return) back anything would be a start in the right direction

Link to post
Share on other sites

Ok so what is a bit more confusing is that

 

return($eval("function square(number1)
\{
     var c = number1*number1;
     // Now we will return the result
     return c;
\}

var x = 4;
// Here we invoke the function and capture the result
var des = square(x);
var res = des/2;
document.write(\" The result - \"+res);

//return res;
"))



will write tot he browser with the document write.

However if you comment that out and remove the // in front of the return, it wont set that value to the return, a set variable etc.

  • Like 1
Link to post
Share on other sites

Ok so what is a bit more confusing is that

 

return($eval("function square(number1)

\{

     var c = number1*number1;

     // Now we will return the result

     return c;

\}

 

var x = 4;

// Here we invoke the function and capture the result

var des = square(x);

var res = des/2;

document.write(\" The result - \"+res);

 

//return res;

"))

 

will write tot he browser with the document write.

However if you comment that out and remove the // in front of the return, it wont set that value to the return, a set variable etc.

 

 

Whenever you want UBot to return JS variable to $eval command you need to call that variable at the end of the script (I just added last line of code = "res;").

 

Here is the solution:

$eval("function square(number1)
\{
     var c = number1*number1;
     // Now we will return the result
     return c;
\}

var x = 4;
// Here we invoke the function and capture the result
var des = square(x);
var res = des/2;

//return res;
res;
")

EDIT: Now I noticed that this was not code in question, so I'll also check that one out.

Link to post
Share on other sites

I got it working, here is the solution that will return the result of the .score method:

set(#result, $eval("String.prototype.score=function(m,s)\{var f=0,q=m.length,g=this,p=g.length,o,k,e=1,j;if(g==m)\{return 1\}for(var d=0,r,n,h,a,b,l;d<q;++d)\{h=m[d];a=g.indexOf(h.toLowerCase());b=g.indexOf(h.toUpperCase());l=Math.min(a,;n=(l>-1)?l:Math.max(a,;if(n===-1)\{if(s)\{e+=1-s;continue\}else\{return 0\}\}else\{r=0.1\}if(g[n]===h)\{r+=0.1\}if(n===0)\{r+=0.6;if(d===0)\{o=1\}\}else\{if(g.charAt(n-1)===\" \")\{r+=0.8\}\}g=g.substring(n+1,p);f+=r\}k=f/q;j=((k*(q/p))+k)/2;j=j/e;if(o&&(j+0.15<1))\{j+=0.15\}return j\};

var test = \'Hello World\'.score(\'he\');
test;"), "Global")

  • Like 1
Link to post
Share on other sites

Sorry, I created duplicated post somehow, when I edited previous answer, so I deleted it and used this text instead.

 

However, previous post will solve your problem Siegfried.

Link to post
Share on other sites

I had a lot of head banging before I discovered the end of script thing.

 

There should be a sticky thread for java there's a lot of cool functions that are pretty much cut and paste (bit of editing to get working in ubot)

I would be willing to share the ones I have got integrated.

Link to post
Share on other sites

Thanks UbotDev -

set(#result, $eval("String.prototype.score=function(m,s)\{var f=0,q=m.length,g=this,p=g.length,o,k,e=1,j;if(g==m)\{return 1\}for(var d=0,r,n,h,a,b,l;d<q;++d)\{h=m[d];a=g.indexOf(h.toLowerCase());b=g.indexOf(h.toUpperCase());l=Math.min(a,;n=(l>-1)?l:Math.max(a,;if(n===-1)\{if(s)\{e+=1-s;continue\}else\{return 0\}\}else\{r=0.1\}if(g[n]===h)\{r+=0.1\}if(n===0)\{r+=0.6;if(d===0)\{o=1\}\}else\{if(g.charAt(n-1)===\" \")\{r+=0.8\}\}g=g.substring(n+1,p);f+=r\}k=f/q;j=((k*(q/p))+k)/2;j=j/e;if(o&&(j+0.15<1))\{j+=0.15\}return j\};

var test = \'Hello World\'.score(\'he\');
test;"), "Global")

But If I want to use UbotStudio Variables in place of  - 'hello World" and 'He' - how can I do it?

 

Thanks.

Link to post
Share on other sites

Awesome!

Thanks a million everyone! I think that, beyond my simple needs, this is a great addition to the Ubot JavaScript code snippets in general. I hope this helps a lot of other folks as well!

Thanks to BotGuru and especially UBotDev for taking time out of your day. Much appreciated.


And YES, it does work for me, with variables replacing the words 'hello world' and 'he'.

Edited by Siegfried88
Link to post
Share on other sites

Awesome!

 

Thanks a million everyone!  I think that, beyond my simple needs, this is a great addition to the Ubot JavaScript code snippets in general.  I hope this helps a lot of other folks as well!

 

Thanks to BotGuru and especially UBotDev for taking time out of your day.  Much appreciated.

No problem, happy to help.

 

Looks like an interesting idea for checking attendees, let us know how that goes. :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...