Jump to content

Any ideas on a better way to do this?


Kagehi Kohn
 Share

You are about to reply to a thread that has been inactive for 3148 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I got this nuts idea of building a sort of shoulder pet, based around the idea that I accidentally "magicked" the thing into existence - it would listen to things going on, and, with certain limits on interval, blab out stuff related to what is being talked about. So, here is what I intended:

1. Pick the first noun in what was said.

2. Feed this to something online, to get a response back.

3. Pull out the first sentence, and repeat that in channel 0 chat.

So, here is the script I managed to, with some dificulty, bang together. It has several issues...

key requestid;
integer delay = 300;
integer last;
integer is_json = FALSE;

default
{
    on_rez(integer param)
    {
        llResetScript();
    }
    
 state_entry()
    {
        //llListen(444, "", llGetOwner(), "");
        llListen(0, "", NULL_KEY, "");
        llResetTime();
    }

    listen (integer chan, string name, key id, string msg)
    {
        if (llGetTime() > 1) //300)
        {
            llResetTime();
            list words = llParseString2List(msg, [" ", ","], ["."]);
            if ((llSubStringIndex(msg, "(") == -1) && (llGetListLength(words) > 0))
            {
                integer step = 0;
                integer len = llGetListLength(words);
                string word;
                for (step = 0; step <= len; ++step)
                {
                    word = llList2String(words, step);
                    //llOwnerSay(word);
                    if (llStringLength(word) > 4)
                    {
                        //llOwnerSay("Here.");
                        requestid = llHTTPRequest("http://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro=&titles=" + llEscapeURL(word), [HTTP_METHOD, "POST"], "");
                        step = len + 1;
                    }
                }
            }
        }
    }
    
    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id = requestid)
        {
            integer test;
            integer end;
            string del;
            do
            {
                test = llSubStringIndex(body, "<");
                if (test != -1)
                {
                    end = llSubStringIndex(body, ">");
                    if (end != -1)
                        body = llDeleteSubString(body, test, end);
                    else
                        body = llDeleteSubString(body, test, llStringLength(body));
                }
            }
            while (test != -1);

            // In theory, this might work instead, to get the correct info.
            // I thought of it after, so haven't tested it.
            //
            //string temp = llJsonGetValue(body, ["query"]);
            //string data = llJsonGetValue(body, ["extract"]);
            
            integer dex = llSubStringIndex(body, "\"extract");
            string data3 = llGetSubString(body, dex, dex + 300) + "\"";
            list sentences = llParseString2List(data3, [":"], ["."]);
            string first_sent = llList2String(sentences, 1);
            if (llStringLength(first_sent) > 0)
                if (llSubStringIndex(first_sent, "may refer to") < 0 && llSubStringIndex(first_sent, "This is a redirect from") < 0)
                {
                    llSay(0, first_sent + ".");
                    llResetTime();
                    //llOwnerSay(first_sent + ".");
                }
        }
    }
}

First issue.. I had to cheat, since I can't find a simple, "Is this a noun" lookup online, or even a dictionary one. Ironically, if Google had kept the old "Google Dictionary" interface, this would be all solved, since you could, back then, pull a json from it, sending it a word. Instead, I look for the first thing longer than 4 leters, and assume its something I want to look for.

Second - Most responses exceed the max string length. So, even though the data is in json, the "string" that contains the body of the page, short of doing a lot of silly stuff to grap more of the body, when the first string is full up (I haven't bothered to look to see if that is even possible), the resulting json is "broken", for most lookups. In short, the json functions won't work,  because, as far as the returned text is concerned, its not a json.

Third - Even if this was correctable, it seems that the "piece" I need, i.e., the extract, is nested in a mess of other parts. So, I would have to do something like:

Check if the I have a value returned when looking for llJsonGetValue(body, "extract").

If no, then use llJson2List, then search the first list item, as above.

Repeat until I either find the result I need, or run out of things to look at.

This is doable... But, requires first fixing the bloody, "My string wasn't long enough." issue.

 

So.. Anyone know some "other" service I can maybe use, to find the right data? Or, heck, I suppose googles new "Define:word" thing might work, if I had some idea how to scrape the stuff I need from it... :p

Link to comment
Share on other sites

It's hardly surprising that you couldn't find a handy "this is a noun" dictionary. After all, how would it tell?  Is "fly" a noun or a verb?  Or "sit"? Or "shop"?  It takes an extremely sophisticated parser to figure that out and, as you can tell from using Google Translate, even they don't always guess right.

Frankly, although this is an interesting scripting challenge, I wonder whether you haven't made life more difficult for yourself than you need to.  First of all, you will potentially create a lot of chat lag in your parcel by keeping a listener open on the PUBLIC_CHANNEL.  The servers will need to listen to every single thing that is said and decide whether it is relevant to your script.  That's likely to get people rather annoyed if they discover that you are the reason for their chat lag.  Aside from that, though, I have to wonder why you don't just click a HUD button to make your pet appear.  That would be WAY easier than doing it the way you plan, would generate no chat lag at all, and would look just as "magical".  Sometimes a script is not the best solution.  :)

Link to comment
Share on other sites

Gah.. Ate my post..

Ok, well.. Thinking I will just keep it as the /444 version, but unlock it for anyone that knows it. The RP for this was that my "alternate" self, a female version of me that showed up, due ot magic going wacko in the city, has been playing around, and came up with this thing. lol So, having it work only with the owner, instead of both of my AVs, when she is on, would be.. annoying. :P

Still, only solution I can really think of would be, if I had a damn idea what I was doing, using the website I pay for to create a PHP, which would take a word, send google a "define" request, scrape the noun definition from it, if there, then pass that back as a json.. Except.. don't know jack about how to do that... lol

Still, need to do something with the page, eventually. Had ideas, but.. Well, the two most obvious ones run into, "I can't wrap my head around how web on prim will actually behave, and many people turn off, 'play media'".

Think they shouldh have created a version of this function that uses the same "permissions" request as you have with teleporting, personally. So, if its "everyone sees it" they can opt to accept seeing it, or not. But, both ideas have similar problems.

1. Interactive signs. Thinking like in one of the recent "futuristic" games, where it detects you approaching a building, and a sign saying "closed", or "here is our menu", etc. "drops down". This one would be OK "if" only the person its supposed to be talking to can see it, in theory, same with a kiosk for a store. Except, not always, but "interacting" with the page is.. iffy.

2. Virtual desktop/single user/but everyone sees what is there, display. See, installed a virtual desktop on my site, but.. since its the client that pulls the page, not SL, I think this means that not everyone "sees" the same content, its streamed "per player", which won't work, if you are looking over someone's shoulder, to see the contents. The only way this "does" work then is if well.. a) you somehow redesing the sight so everyone sees what the one logged in is doing (I don't know how the VD works enough to edit it, never mind fo this, and.. not sure about anything else I might do), b) host the "page" on the object, and "upload" it to the display. The latter is possible, except, you can't tell, I don't think, who is "seing" the resulting page, so, unless they click on it, or something, to see, it has no way of knowing what to show, and it can't give the first person something that is "interactive", but the second one only "what is going on."

In short.. I just don't get how, when, and under what circumstances, "everyone" or "just one person" is "seeing" the web content. And, that.. presents problems, over and above it just not working, "at all", if they don't have playback turned on.

Still, other than using the page to somehow scrape the data I need, its a whole different tangle of of mess of complications. lol

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3148 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...