Visit Superuser

AskWikipedia Question

From Superuser Wiki

From http://osgrid.org/forums/viewtopic.php?f=5&t=47

This script does multiple things:

  • listens for chat
  • checks for who/what/where/when/why questions with "???"
  • calls an external server with a query
  • parses out the xml'ish reply
  • come up with a random reply when it should respond but can't.
  • times out and resets the reply
  • formats the strings to a fit a width requirement
  • if touched and it has a url to offer it does so

The backend is a perl script that passes the query to http://www.AskWiki.com , scrapes the reply, and sends it back to the prim in a xml-ultralight format.

Now you can access all the sum of human knowledge (or at lease wikipedia) from a prim!

==================================================================
//lsl

// AskWikiClient
// interface to a simple all-knowing answer bot
// provided by your code friends at Daxtron Labs

integer listenChannel;
integer listenControl;

key http_request_id;
string trigger="???"; //"i wonder";
integer timeout=30;

string gURL="";
string gLabel="";
key last_speaker;

// See if its a question for wikipedia
// if so put in the request

integer askWikipedia(string Question)
{
float rand;
integer wikiHandle=0;
string LQuestion = llToLower(Question);
if(llSubStringIndex(LQuestion,"what")>=0) {wikiHandle = 1;}
if(llSubStringIndex(LQuestion,"who ")>=0) {wikiHandle = 1;}
if(llSubStringIndex(LQuestion,"where ")>=0) {wikiHandle = 1;}
if(llSubStringIndex(LQuestion,"when ")>=0) {wikiHandle = 1;}
if(llSubStringIndex(LQuestion,"why ")>=0) {wikiHandle = 1;}
if(llSubStringIndex(LQuestion,"how ")>=0) {wikiHandle = 1;}
if (wikiHandle==1)
{
string sourceURL="http://66.14.82.104:8096/question="+llEscapeURL(Question);
integer HTTP_OK=200;
key requestingResident=NULL_KEY;
http_request_id = llHTTPRequest(sourceURL,[],"");

rand = llFrand(3);
if( rand < 1.0 ) {
llSay(0,"Good Question ...");
} else if( rand < 2.0 ) {
llSay(0,"Let me think...");
} else if( rand < 3.0 ) {
llSay(0,"Consulting Wikipedia...");
}
return 1;
}
return 0;
}


integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
return ~llSubStringIndex(haystack, needle);
}


// makes redoes strings to have proper boundries
// it will insert a \n at the next space after wrap characters

string wordwrap(string instring,integer wrap)
{
string outstring="";
integer i;
integer count=0;
for(i=0;i<llStringLength(instring);i++)
{
count++;
string subc = llGetSubString(instring,i,i);
outstring +=subc;
if ((count>wrap) && (subc == " "))
{
count=0;
outstring += "\n";
}
}
outstring +="\n";
return(outstring);

}

// central place to present a response
// also sets a display reset timer

emitResponse(string Response)
{
//llSay(0,"initial response="+Response);
string showResponse = wordwrap(Response,48);
llSetText(showResponse, <0,1,1>, 1);
llSay(0,showResponse);
llSetTimerEvent(timeout);
}

default_display()
{
string showDefault = wordwrap("Ask a question with '???' to get a Wikipedia answer (maybe). Touch me to get the Wikipedia article used to provide the answer.",48);
string showResponse=wordwrap(showDefault,48);
llSay(0,showResponse);
llSetText(showResponse, <0,1,1>, 1);
llSetTimerEvent(0);

}
////////////
// States //
////////////
default {

state_entry()
{
listenChannel = 0;
listenControl = llListen(listenChannel,"",NULL_KEY,"");
default_display();
}

on_rez(integer start_param)
{
//llResetScript();
listenChannel = 0;
listenControl = llListen(listenChannel,"",NULL_KEY,"");
default_display();
}

touch_start(integer times)
{

if (llStringLength(gURL)>0)
{
key target_key = last_speaker;
//key target_key = llDetectedKey(0);
llSay(0," target_key ="+(string)target_key+" URL="+gURL);
llLoadURL(target_key, "Load "+gLabel, gURL);
}
else
{
llSay(0," No gURL present");
}
}

timer()
{
gURL = "";
gLabel="";
default_display();
}

// listen remembers the ID of who spoke so if touched
// it can send them the justification URL
// it also only responds if "?" and trigger are in the message
// so something like 'I wonder what is the capital of Russia ?'

listen(integer channelIn, string name, key id, string message)
{
float rand;
if (llSubStringIndex(message,"?") >= 0)
{

integer triggerp = llSubStringIndex(llToLower(message),trigger);
if(triggerp<0) {return;}
// remove trigger
integer endp = llStringLength(message)-1;
integer startp = triggerp+llStringLength(trigger);
message = llGetSubString(message,startp,endp);
integer tryWikip = askWikipedia(message);
last_speaker = id;
if (tryWikip == 1) return;

rand = llFrand(19.0);
if( rand < 1.0 ) {
llWhisper(0,"Yes.");
} else if( rand < 2.0 ) {
llWhisper(0,"Signs Point To Yes.");
} else if( rand < 3.0 ) {
llWhisper(0,"My Sources Say No.");
} else if( rand < 4.0 ) {
llWhisper(0,"Without A Doubt.");
} else if( rand < 5.0 ) {
llWhisper(0,"Reply Hazy Try Again.");
} else if( rand < 6.0 ) {
llWhisper(0,"Don't Count On It.");
} else if( rand < 7.0 ) {
llWhisper(0,"Most Likely.");
} else if( rand < 8.0 ) {
llWhisper(0,"Ask Again Later.");
} else if( rand < 9.0 ) {
llWhisper(0,"My Reply Is No.");
} else if( rand < 10.0 ) {
llWhisper(0,"Outlook Good.");
} else if( rand < 11.0 ) {
llWhisper(0,"It Is Decidedly So.");
} else if( rand < 12.0 ) {
llWhisper(0,"Cannot Predict Now.");
} else if( rand < 13.0 ) {
llWhisper(0,"Concentrate And Ask Again.");
} else if( rand < 14.0 ) {
llWhisper(0,"Yes Definitely.");
} else if( rand < 15.0 ) {
llWhisper(0,"As I See It Yes.");
} else if( rand < 16.0 ){
llWhisper(0,"Better Not Tell You Now.");
} else if( rand < 16.0 ){
llWhisper(0,"It Is Certain.");
} else if( rand < 17.0 ){
llWhisper(0,"Outlook Not So Good.");
} else if( rand < 18.0 ){
llWhisper(0,"Very Doubtful.");
} else {
llWhisper(0,"You May Rely On It.");
}

}

}

http_response(key request_id, integer status, list metadata, string body)
{
llSay(0,"This just in ...");
// if (request_id == http_request_id)
{
string startAns="<ans>";
string endAns="</ans>";
string startQues="<ques>";
string endQues="</ques>";
string wikiAns="Some crazy talk...";
string wikiQues="Something you said...";
gURL ="";
gLabel="";


integer startIndex = llSubStringIndex(body,startAns);
integer endIndex = llSubStringIndex(body,endAns);
if ( endIndex >=0 && startIndex>=0 && endIndex> startIndex)
{
wikiAns = llGetSubString(body,startIndex +llStringLength(startAns),endIndex-1);
}
else
{
//llWhisper(0,"startIndex="+(string)startIndex);
llWhisper(0,"endIndex="+(string)endIndex);
llWhisper(0,"body="+body);
llWhisper(0,"status="+(string)status);
llWhisper(0,"metadata="+(string)metadata);
llWhisper(0,"bodylength="+(string)llStringLength(body));
}

startIndex = llSubStringIndex(body,startQues);
endIndex = llSubStringIndex(body,endQues);
if ( endIndex >=0 && startIndex>=0 && endIndex> startIndex)
{
wikiQues = llGetSubString(body,startIndex +llStringLength(startQues),endIndex-1);
}
else
{
//llWhisper(0,"startIndex="+(string)startIndex);
llWhisper(0,"endIndex="+(string)endIndex);
llWhisper(0,"body="+body);
llWhisper(0,"status="+(string)status);
llWhisper(0,"metadata="+(string)metadata);
llWhisper(0,"bodylength="+(string)llStringLength(body));
}

string askResponse = "Wikip sez '"+wikiAns+"' to '"+wikiQues+"'";
//llSay(0,"askResp="+askResponse);
emitResponse(askResponse);

// touch me jump to urls
startIndex = llSubStringIndex(body,"<gurl>");
endIndex = llSubStringIndex(body,"</gurl>");
if ( endIndex >=0 && startIndex>=0 && endIndex> startIndex)
{
gURL = llGetSubString(body,startIndex +llStringLength(startQues),endIndex-1);
}
startIndex = llSubStringIndex(body,"<glabel>");
endIndex = llSubStringIndex(body,"</glabel>");
if ( endIndex >=0 && startIndex>=0 && endIndex> startIndex)
{
gLabel = llGetSubString(body,startIndex +llStringLength(startQues),endIndex-1);
}

}
}


}