Difference between revisions of "User talk:Perry Mizin"

From Second Life Wiki
Jump to navigation Jump to search
Line 40: Line 40:
From Güatebad to Güateworse, No comments...
From Güatebad to Güateworse, No comments...


Buscando en Internet he encontrado este [http://developer.yahoo.com/search/web/V1/spellingSuggestion.html API de Yahoo] comprobador de ortografía, aunque creo que por ahora sólo soporta el lenguage inglés sería interesante añadirlo al proyecto
Buscando en Internet he encontrado este [http://developer.yahoo.com/search/web/V1/spellingSuggestion.html API de Yahoo] comprobador de ortografía, aunque creo que por ahora sólo soporta el lenguage inglés por ahora sería interesante añadirlo al proyecto


Searching on the web I crossend in my way with this spell checker [http://developer.yahoo.com/search/web/V1/spellingSuggestion.html Yahoo´s API] , I think that it only supports English language althought it would be interesting to add it to the project
Searching on the web I crossed in my way with this spell checker [http://developer.yahoo.com/search/web/V1/spellingSuggestion.html Yahoo´s API] , I think that it only supports English language by now althought it would be interesting to add it to the project


----
----
Line 56: Line 56:
//November 12, 2009
//November 12, 2009
//LSL Script copyright 2006-2009 by Hank Ramos
//LSL Script copyright 2006-2009 by Hank Ramos
//Web Server Services powered by Google
//Web Server Services powered by Google  
//Variables
//Variables
list requestedTranslations;
list requestedTranslations;
Line 62: Line 62:
list agentsInTranslation;
list agentsInTranslation;
list requestList;
list requestList;
integer listenID;
integer listenID;  
integer isMaster = 1;
integer isMaster = 1;
integer autoLanguage = TRUE;
integer autoLanguage = TRUE;
integer enabled = FALSE;
integer enabled = FALSE;
integer showTranslation = FALSE;
integer showTranslation = FALSE;
integer tranObjects = TRUE;
integer tranObjects = TRUE;  
integer isInitialized = FALSE;
integer isInitialized = FALSE;
string  options;
string  options;
Line 74: Line 74:
integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
{
return ~llSubStringIndex(haystack, needle);
    return ~llSubStringIndex(haystack, needle);
}
}


sendIM(key imid, string imstr)
sendIM(key imid, string imstr)
{
{
llInstantMessage(imid, llUnescapeURL(imstr));
    llInstantMessage(imid, llUnescapeURL(imstr));
if (imid != llGetOwner())
    if (imid != llGetOwner())
{
    {
llOwnerSay(llUnescapeURL(imstr) + " @" + llKey2Name(imid));
    llOwnerSay(llUnescapeURL(imstr) + " @" + llKey2Name(imid));
}
    }
}
}


detectLanguage(string dtcmsg, key dtckey)
detectLanguage(string dtcmsg, key dtckey)
{
{
dtcmsg = "detect?v=1.0&q=" + dtcmsg;
    dtcmsg = "detect?v=1.0&q=" + llEscapeURL(dtcmsg);
tempValue = llHTTPRequest("http://ajax.googleapis.com/ajax/services/language/" + dtcmsg, [HTTP_METHOD, "GET", HTTP_MIMETYPE, "plain/text;charset=utf-8"], " ");
    tempValue = llHTTPRequest("http://ajax.googleapis.com/ajax/services/language/" + dtcmsg, [HTTP_METHOD, "GET", HTTP_MIMETYPE, "plain/text;charset=utf-8"], " ");
if (tempValue != NULL_KEY)
    if (tempValue != NULL_KEY)
{
    {
tempDetections = (tempDetections=[]) + tempDetections + [tempValue, dtckey];
        tempDetections = (tempDetections=[]) + tempDetections + [tempValue, dtckey];
}
    }
}
}


checkThrottle(integer chkn, string chkmsg, list chkli)
checkThrottle(integer chkn, string chkmsg, list chkli)
{
{
key returnValue;
    key returnValue;
//loop though list and remove items older than 25 seconds
    //loop though list and remove items older than 25 seconds
chkmsg = "translate?v=1.0&q=" + chkmsg;
    chkmsg = "translate?v=1.0&q=" + chkmsg;
requestList = (requestList=[]) + requestList + [llGetTime()];
    requestList = (requestList=[]) + requestList + [llGetTime()];
returnValue = llHTTPRequest("http://ajax.googleapis.com/ajax/services/language/" + chkmsg, [HTTP_METHOD, "GET", HTTP_MIMETYPE, "plain/text;charset=utf-8"], "");
    returnValue = llHTTPRequest("http://ajax.googleapis.com/ajax/services/language/" + chkmsg, [HTTP_METHOD, "GET", HTTP_MIMETYPE, "plain/text;charset=utf-8"], "");
if (returnValue != NULL_KEY)
    if (returnValue != NULL_KEY)
{
    {
//Translation
            //Translation
requestedTranslations = (requestedTranslations=[]) + requestedTranslations + [returnValue, llList2CSV(chkli)];
            requestedTranslations = (requestedTranslations=[]) + requestedTranslations + [returnValue, llList2CSV(chkli)];
}
    }
else
    else
{
    {
llSleep(40.0); //Something has gone horribly wrong, sleep 40 seconds to clear throttle
        llSleep(40.0); //Something has gone horribly wrong, sleep 40 seconds to clear throttle
}
    }
}
}


string checkLanguage(string chklantmpStr)
string checkLanguage(string chklantmpStr)
{
{
if (chklantmpStr == "")
    if (chklantmpStr == "")
{
    {
chklantmpStr = "en";
        chklantmpStr = "en";
}
    }
else if (chklantmpStr == "und")
    else if (chklantmpStr == "und")
{
    {
chklantmpStr = "el";
        chklantmpStr = "el";
}
    }
if (llGetSubString(chklantmpStr, 0, 1) == "zh")
    if (llGetSubString(chklantmpStr, 0, 1) == "zh")
{
    {
chklantmpStr = "zh-CN";
        chklantmpStr = "zh-CN";
}
    }
else
    else
{
    {
chklantmpStr = llGetSubString(chklantmpStr, 0, 1);
        chklantmpStr = llGetSubString(chklantmpStr, 0, 1);
}
    }
return chklantmpStr;
    return chklantmpStr;
}
}


addAgent(key avid, string avlan, integer chklan)
addAgent(key avid, string avlan, integer chklan)
{
{
integer avliPos;
    integer avliPos;
integer idNum;
    integer idNum;
if (~llListFindList(agentsInTranslation, (list)avid))
    if (~llListFindList(agentsInTranslation, (list)avid))
{
    {
avliPos = llListFindList(agentsInTranslation, (list)avid);
        avliPos = llListFindList(agentsInTranslation, (list)avid);
agentsInTranslation = llListReplaceList(agentsInTranslation, [avlan, chklan], avliPos + 1, avliPos + 2);
        agentsInTranslation = llListReplaceList(agentsInTranslation, [avlan, chklan], avliPos + 1, avliPos + 2);
}
    }
else
    else
{
    {
@at;
        @at;
idNum = llRound(llFrand(2000000)) + 1;
        idNum = llRound(llFrand(2000000)) + 1;
if(~llListFindList(agentsInTranslation, (list)idNum))
        if(~llListFindList(agentsInTranslation, (list)idNum))
{
        {      
jump at;
            jump at;
}
        }
agentsInTranslation = (agentsInTranslation=[]) + agentsInTranslation + [avid, avlan, chklan, idNum];
        agentsInTranslation = (agentsInTranslation=[]) + agentsInTranslation + [avid, avlan, chklan, idNum];
llMessageLinked(LINK_THIS, 64562349, avlan, avid);
        llMessageLinked(LINK_THIS, 64562349, avlan, avid);      
}
    }
}
}


string addNewAgent(key newavid)
string addNewAgent(key newavid)
{
{
string speakerLan;
    string speakerLan;  
if (llGetAgentSize(newavid) != ZERO_VECTOR)
    if (llGetAgentSize(newavid) != ZERO_VECTOR)
{
    {
speakerLan  = checkLanguage(llGetAgentLanguage(newavid));
        speakerLan  = checkLanguage(llGetAgentLanguage(newavid));
if (speakerLan == "")
        if (speakerLan == "")
{
        {
speakerLan = "en";
            speakerLan = "en";
addAgent(newavid, speakerLan, 0);
            addAgent(newavid, speakerLan, 0);
}
        }
else
        else
{
        {
addAgent(newavid, speakerLan, 0);
            addAgent(newavid, speakerLan, 0);
}
        }
}
    }
return speakerLan;
    return speakerLan;
}
}


key getAgentKey(integer agentID)
key getAgentKey(integer agentID)
{
{
if(~llListFindList(agentsInTranslation, (list)agentID))
    if(~llListFindList(agentsInTranslation, (list)agentID))
{
    {
integer gkliPos = llListFindList(agentsInTranslation, (list)agentID);
        integer gkliPos = llListFindList(agentsInTranslation, (list)agentID);
return llList2Key(agentsInTranslation, gkliPos - 3);
        return llList2Key(agentsInTranslation, gkliPos - 3);
}
    }
else
    else
{
    {
return "";
        return "";
}
    }
}
}


processHTTPResponse(integer ntype, string bodyStr, list httli)
processHTTPResponse(integer ntype, string bodyStr, list httli)
{
{
integer spkliPos;
    integer spkliPos;
list    recepientList;
    list    recepientList;
key    recepientID;
    key    recepientID;
string  recepientLanguage;
    string  recepientLanguage;
string  languagePair;
    string  languagePair;
key    speakerID;
    key    speakerID;
string  speakerName;
    string  speakerName;
string  speakerLanguage;
    string  speakerLanguage;
string  translatedText;
    string  translatedText;
string  lantmpStr;
    string  lantmpStr;
integer z;
    integer z;
list    paramtmpli;
    list    paramtmpli;
//===================
    //===================
//Process Translation
    //Process Translation
//===================
    //===================
speakerID  = llList2Key(httli, 1);
        speakerID  = llList2Key(httli, 1);
speakerName = llKey2Name(speakerID);
        speakerName = llKey2Name(speakerID);
if (speakerName == "")
        if (speakerName == "")
{
        {
speakerName = llList2String(llGetObjectDetails(speakerID, [OBJECT_NAME]), 0);
            speakerName = llList2String(llGetObjectDetails(speakerID, [OBJECT_NAME]), 0);
}
        }
recepientList = llParseString2List(llList2String(httli, 2), ["@"], []);
        recepientList = llParseString2List(llList2String(httli, 2), ["@"], []);
paramtmpli = llParseStringKeepNulls(llList2String(httli, 3), ["|"],[]);
        paramtmpli = llParseStringKeepNulls(llList2String(httli, 3), ["|"],[]);
recepientLanguage = llList2String(paramtmpli, 1);
        recepientLanguage = llList2String(paramtmpli, 1);
languagePair = llDumpList2String(paramtmpli, ">>");
        languagePair = llDumpList2String(paramtmpli, ">>");  
//Perform Text Cleanup
        //Perform Text Cleanup
translatedText = llGetSubString(bodyStr, llSubStringIndex(bodyStr, "{\"translatedText\":\"") + 18, llSubStringIndex(bodyStr, "\",\"detectedSourceLanguage\":\""));
        translatedText = llGetSubString(bodyStr, llSubStringIndex(bodyStr, "{\"translatedText\":\"") + 18, llSubStringIndex(bodyStr, "\",\"detectedSourceLanguage\":\""));


//Reverse order if Recepient Language is Hebrew or Arabic
        //Reverse order if Recepient Language is Hebrew or Arabic
if ((recepientLanguage == "iw") || (recepientLanguage == "ar"))
        if ((recepientLanguage == "iw") || (recepientLanguage == "ar"))
{
        {
lantmpStr = "";
            lantmpStr = "";
for(z = llStringLength(translatedText);z >= 0; z--)
            for(z = llStringLength(translatedText);z >= 0; z--)
{
            {
lantmpStr = (lantmpStr="") + lantmpStr + llGetSubString(translatedText, z, z);
                lantmpStr = (lantmpStr="") + lantmpStr + llGetSubString(translatedText, z, z);  
}
            }
translatedText = lantmpStr;
            translatedText = lantmpStr;
}
        }  
lantmpStr = speakerName + "(" + languagePair + "): " + translatedText;
        lantmpStr = speakerName + "(" + languagePair + "): " + translatedText;
if (showTranslation)
        if (showTranslation)
{
        {
sendIM(speakerID, lantmpStr);
            sendIM(speakerID, lantmpStr);
}
        }  
for (z = 0; z < llGetListLength(recepientList); z += 1)
        for (z = 0; z < llGetListLength(recepientList); z += 1)
{
        {
recepientID = getAgentKey(llList2Integer(recepientList, z));
            recepientID = getAgentKey(llList2Integer(recepientList, z));
if (recepientID != "")
            if (recepientID != "")
{
            {
recepientLanguage = llList2String(agentsInTranslation, llListFindList(agentsInTranslation, [recepientID]) + 1);
                recepientLanguage = llList2String(agentsInTranslation, llListFindList(agentsInTranslation, [recepientID]) + 1);
if (recepientLanguage != speakerLanguage)
                if (recepientLanguage != speakerLanguage)
{
                {
sendIM(recepientID, lantmpStr);
                    sendIM(recepientID, lantmpStr);
}
                }
}
            }
}
        }
return;
        return;  
}
}
 
default
default
{
{
state_entry()
    state_entry()
{
    {
llSetTimerEvent(5);
        llSetTimerEvent(5);
}
    }
 
link_message(integer sender_num, integer num, string lstr, key lid)
    link_message(integer sender_num, integer num, string lstr, key lid)
{
    {
list   ltmpli;
        list ltmpli;
if (num == 8434532)
        if (num == 8434532)
{
        {
enabled = (integer)lstr;
            enabled = (integer)lstr;
}
        }
else if (num == 3342976)
        else if (num == 3342976)
{
        {
//Send Preferences
            //Send Preferences
options = lstr;
            options = lstr;
if (isInitialized == FALSE) return;
            if (isInitialized == FALSE) return;
ltmpli = llCSV2List(options);
            ltmpli = llCSV2List(options);
showTranslation = llList2Integer(ltmpli, 0);
            showTranslation = llList2Integer(ltmpli, 0);
tranObjects = llList2Integer(ltmpli, 1);
            tranObjects = llList2Integer(ltmpli, 1);
autoLanguage = llList2Integer(ltmpli, 2);
            autoLanguage = llList2Integer(ltmpli, 2);
}
        }
}
    }
 
sensor(integer num_detected)
    sensor(integer num_detected)
{
    {
integer s;
        integer s;
key    sid;
        key    sid;
for (s = 0; s < num_detected; s += 1)
        for (s = 0; s < num_detected; s += 1)
{
        {
sid = llDetectedKey(s);
            sid = llDetectedKey(s);
if (~llListFindList(agentsInTranslation, (list)sid)) { }
            if (~llListFindList(agentsInTranslation, (list)sid)) { }
else
            else
{
            {
addNewAgent(sid);
                addNewAgent(sid);
}
            }
}
        }
}
    }
 
   
no_sensor() { }
    no_sensor() { }
 
   
listen(integer channel, string name, key id, string message)
    listen(integer channel, string name, key id, string message)
{
    {
integer w;
        integer w;
string  spkrLan;
        string  spkrLan;
string  recepLan;
        string  recepLan;
integer recepID;
        integer recepID;
integer lstliPos;
        integer lstliPos;
string  lanpair;
        string  lanpair;
list    translationCache;
        list    translationCache;
//Multiplexor Code
        //Multiplexor Code
//Translator Engine Code
        //Translator Engine Code
if ((!enabled) && (isMaster == 1))
        if ((!enabled) && (isMaster == 1))
{
        {
return;
            return;
}
        }  
if (!tranObjects)
        if (!tranObjects)
{
        {
if (llGetAgentSize(id) == ZERO_VECTOR)
            if (llGetAgentSize(id) == ZERO_VECTOR)
{
            {
return;
            return;
}
            }  
else
            else
{
            {
detectLanguage(llEscapeURL(message), id);
                detectLanguage(message, id);
}
            }
}
        }
else
        else
{
        {
detectLanguage(llEscapeURL(message), id);
            detectLanguage(message, id);
}
        }
 
           
if (~llListFindList(agentsInTranslation, (list)id))
        if (~llListFindList(agentsInTranslation, (list)id))
{
        {
lstliPos = llListFindList(agentsInTranslation, (list)id);
            lstliPos = llListFindList(agentsInTranslation, (list)id);        
spkrLan = llList2String(agentsInTranslation, lstliPos + 1);
            spkrLan = llList2String(agentsInTranslation, lstliPos + 1);
}
        }
else
        else
{
        {
spkrLan = addNewAgent(id);
            spkrLan = addNewAgent(id);
}
        }
//===============================
                //===============================
//Formulate Translation Requests
                //Formulate Translation Requests
//===============================
                //===============================
if (id == llGetOwner())
        if (id == llGetOwner())
{
        {
for (w = 0; w < llGetListLength(agentsInTranslation); w += 4)
            for (w = 0; w < llGetListLength(agentsInTranslation); w += 4)
{
            {
//Loop through translation group and do appropriate translations as needed
                //Loop through translation group and do appropriate translations as needed
recepID = llList2Integer(agentsInTranslation, w + 3);
                recepID = llList2Integer(agentsInTranslation, w + 3);
recepLan =  checkLanguage(llList2String(agentsInTranslation, w + 1));
                recepLan =  checkLanguage(llList2String(agentsInTranslation, w + 1));
if (spkrLan != recepLan)
                if (spkrLan != recepLan)  
{
                {
lanpair = spkrLan + "|" + recepLan;
                    lanpair = spkrLan + "|" + recepLan;
if (~llListFindList(translationCache, (list)lanpair))
                    if (~llListFindList(translationCache, (list)lanpair))
{
                    {
lstliPos = llListFindList(translationCache, (list)lanpair);
                        lstliPos = llListFindList(translationCache, (list)lanpair);
translationCache = llListReplaceList(translationCache, [llList2String(translationCache, lstliPos + 1) + "@" + (string)recepID], lstliPos + 1, lstliPos + 1);
                        translationCache = llListReplaceList(translationCache, [llList2String(translationCache, lstliPos + 1) + "@" + (string)recepID], lstliPos + 1, lstliPos + 1);
}
                    }
else
                    else
{
                    {
translationCache = (translationCache=[]) + translationCache + [lanpair, recepID];
                        translationCache = (translationCache=[]) + translationCache + [lanpair, recepID];
}
                    }
}
                }
}
            }            
//Process Requests
            //Process Requests
if (translationCache != [])
            if (translationCache != [])
{
            {
for (w = 0; w < llGetListLength(translationCache); w += 2)
                for (w = 0; w < llGetListLength(translationCache); w += 2)
{
                {
//====================================
                    //====================================
//Translation
                    //Translation
//====================================
                    //====================================
//Forumulate and Send Translation Request
                    //Forumulate and Send Translation Request
lanpair = "|" + llList2String(llParseStringKeepNulls(llList2String(translationCache, w), ["|"],[]), 1);
                    lanpair = "|" + llList2String(llParseStringKeepNulls(llList2String(translationCache, w), ["|"],[]), 1);  
checkThrottle(0, llEscapeURL(message) + "&langpair=" + llEscapeURL(lanpair), [llGetTime(), id , llList2String(translationCache, w + 1), llList2String(translationCache, w)]);
                    checkThrottle(0, llEscapeURL(message) + "&langpair=" + llEscapeURL(lanpair), [llGetTime(), id , llList2String(translationCache, w + 1), llList2String(translationCache, w)]);
}
                }  
}
            }  
}
        }
else
        else
{
        {
lstliPos = llListFindList(agentsInTranslation, (list)llGetOwner());
            lstliPos = llListFindList(agentsInTranslation, (list)llGetOwner());
recepID = llList2Integer(agentsInTranslation, lstliPos + 3);
            recepID = llList2Integer(agentsInTranslation, lstliPos + 3);
recepLan =  checkLanguage(llList2String(agentsInTranslation , lstliPos + 1 ));
            recepLan =  checkLanguage(llList2String(agentsInTranslation , lstliPos + 1 ));
if (spkrLan != recepLan)
            if (spkrLan != recepLan)  
{
            {
lanpair = spkrLan + "|" + recepLan;
                lanpair = spkrLan + "|" + recepLan;
if (~llListFindList(translationCache, (list)lanpair))
                if (~llListFindList(translationCache, (list)lanpair))
{
                {
lstliPos = llListFindList(translationCache, (list)lanpair);
                    lstliPos = llListFindList(translationCache, (list)lanpair);
translationCache = llListReplaceList(translationCache, [llList2String(translationCache, lstliPos + 1) + "@" + (string)recepID], lstliPos + 1, lstliPos + 1);
                    translationCache = llListReplaceList(translationCache, [llList2String(translationCache, lstliPos + 1) + "@" + (string)recepID], lstliPos + 1, lstliPos + 1);
}
                }
else
                else
{
                {
translationCache = (translationCache=[]) + translationCache + [lanpair, recepID];
                    translationCache = (translationCache=[]) + translationCache + [lanpair, recepID];
}
                }                                        
if (translationCache != [])
                if (translationCache != [])
{
                {
//====================================
                        //====================================
//Translation
                        //Translation
//====================================
                        //====================================
//Forumulate and Send Translation Request
                        //Forumulate and Send Translation Request
lanpair = "|" + llList2String(llParseStringKeepNulls(llList2String(translationCache, lstliPos), ["|"],[]), 1);
                        lanpair = "|" + llList2String(llParseStringKeepNulls(llList2String(translationCache, lstliPos), ["|"],[]), 1);  
checkThrottle(0, llEscapeURL(message) + "&langpair=" + llEscapeURL(lanpair), [llGetTime(), id , llList2String(translationCache, lstliPos + 1), llList2String(translationCache, lstliPos)]);
                        checkThrottle(0, llEscapeURL(message) + "&langpair=" + llEscapeURL(lanpair), [llGetTime(), id , llList2String(translationCache, lstliPos + 1), llList2String(translationCache, lstliPos)]);
}
                }  
}
            }                            
}
        }  
}
    }
 
http_response(key request_id, integer status, list metadata, string body)
    http_response(key request_id, integer status, list metadata, string body)
{
    {
//Process Resonse Code
        //Process Resonse Code
integer httliPos;
        integer httliPos;
string htttmpStr;
        string htttmpStr;


if (~llListFindList(requestedTranslations, (list)request_id))
        if (~llListFindList(requestedTranslations, (list)request_id))
{
        {
httliPos = llListFindList(requestedTranslations, [request_id]);
            httliPos = llListFindList(requestedTranslations, [request_id]);
processHTTPResponse(0, body, llCSV2List(llList2String(requestedTranslations, httliPos + 1)));
            processHTTPResponse(0, body, llCSV2List(llList2String(requestedTranslations, httliPos + 1)));
requestedTranslations = llDeleteSubList(requestedTranslations, httliPos, httliPos + 1);
            requestedTranslations = llDeleteSubList(requestedTranslations, httliPos, httliPos + 1);
return;
            return;
}
        }
if (~llListFindList(tempDetections, (list)request_id))
        if (~llListFindList(tempDetections, (list)request_id))
{
        {
integer tmpliPos = llListFindList(tempDetections, (list)request_id);
            integer tmpliPos = llListFindList(tempDetections, (list)request_id);
key speakerID = llList2Key(tempDetections, tmpliPos + 1);
            key speakerID = llList2Key(tempDetections, tmpliPos + 1);
tempDetections = llDeleteSubList(tempDetections, tmpliPos, tmpliPos + 3);
            tempDetections = llDeleteSubList(tempDetections, tmpliPos, tmpliPos + 3);
string tmptmpStr = llToLower(llGetSubString(body, llSubStringIndex(body, "{\"language\":\"") + 13, llSubStringIndex(body, "\",\"isReliable\":") - 1));
            string tmptmpStr = llToLower(llGetSubString(body, llSubStringIndex(body, "{\"language\":\"") + 13, llSubStringIndex(body, "\",\"isReliable\":") - 1));
if (contains(tmptmpStr, "err") == 0 && contains(tmptmpStr, "unn") == 0 )
            if (contains(tmptmpStr, "err") == 0 && contains(tmptmpStr, "unn") == 0 )
{
            {
tmptmpStr = checkLanguage(tmptmpStr);
                tmptmpStr = checkLanguage(tmptmpStr);
if (~llListFindList(agentsInTranslation, (list)speakerID))
                if (~llListFindList(agentsInTranslation, (list)speakerID))
{
                {
integer tmpavliPos = llListFindList(agentsInTranslation, (list)speakerID);
                    integer tmpavliPos = llListFindList(agentsInTranslation, (list)speakerID);
string actlan = llList2String(agentsInTranslation, tmpavliPos + 1);
                    string actlan = llList2String(agentsInTranslation, tmpavliPos + 1);
if (actlan != tmptmpStr)
                    if (actlan != tmptmpStr)
{
                    {
agentsInTranslation = llListReplaceList(agentsInTranslation,[llList2Integer(agentsInTranslation,tmpavliPos + 2) + 1] , tmpavliPos + 2, tmpavliPos + 2);
                        agentsInTranslation = llListReplaceList(agentsInTranslation,[llList2Integer(agentsInTranslation,tmpavliPos + 2) + 1] , tmpavliPos + 2, tmpavliPos + 2);
}
                    }  
else
                    else  
{
                    {
agentsInTranslation = llListReplaceList(agentsInTranslation,[0], tmpavliPos + 2, tmpavliPos + 2);
                        agentsInTranslation = llListReplaceList(agentsInTranslation,[0], tmpavliPos + 2, tmpavliPos + 2);
}
                    }
if (llList2Integer(agentsInTranslation,tmpavliPos + 2) >= 4) //if any av talks on any language 4 times in a row
                    if (llList2Integer(agentsInTranslation,tmpavliPos + 2) >= 4) //if any av talks on any language 4 times in a row
{
                    {
if (tmptmpStr == "")
                        if (tmptmpStr == "")
{
                        {
return;
                            return;
}
                        }
else
                        else
{
                        {
addAgent(speakerID, tmptmpStr, 0);
                            addAgent(speakerID, tmptmpStr, 0);
}
                        }
}
                    }              
}
                }              
}
            }          
}
        }            
}
    }


timer()
    timer()
{
    {
llSetTimerEvent(0.0);
        llSetTimerEvent(0.0);
if (isInitialized == FALSE)
        if (isInitialized == FALSE)
{
        {
enabled = TRUE;
            enabled = TRUE;
listenID = llListen(0, "", "", "");
            listenID = llListen(0, "", "", "");  
llMessageLinked(LINK_THIS, 6877259, (string)enabled, NULL_KEY);
            llMessageLinked(LINK_THIS, 6877259, (string)enabled, NULL_KEY);
isInitialized = TRUE;
            isInitialized = TRUE;
string mylanguage = llGetAgentLanguage(llGetOwner());
            string mylanguage = llGetAgentLanguage(llGetOwner());
if (llStringLength(mylanguage) < 1)
            if (llStringLength(mylanguage) < 1)
{
            {
mylanguage = "en";
                mylanguage = "en";
}
            }
agentsInTranslation = (agentsInTranslation=[]) + agentsInTranslation + [llGetOwner() , mylanguage , 0 , llRound(llFrand(2000000)) + 1];
            agentsInTranslation = (agentsInTranslation=[]) + agentsInTranslation + [llGetOwner() , mylanguage , 0 , llRound(llFrand(2000000)) + 1];
}
        }
integer r;
        integer r;
string  tmrtmpStr;
        string  tmrtmpStr;
list    tmrnewli;
        list    tmrnewli;  
integer x;
        integer x;
list  newList;
        list  newList;
float timeElapsed;
        float timeElapsed;
for (x = 0; x < llGetListLength(tempDetections); x += 2)
        for (x = 0; x < llGetListLength(tempDetections); x += 2)
{
        {
timeElapsed = llGetTime() - llList2Float(llCSV2List(llList2String(tempDetections, x + 1)), 0);
            timeElapsed = llGetTime() - llList2Float(llCSV2List(llList2String(tempDetections, x + 1)), 0);
if (timeElapsed < 60.0)
            if (timeElapsed < 60.0)  
newList = (newList=[]) + newList + llList2List(tempDetections, x, x + 1);
                newList = (newList=[]) + newList + llList2List(tempDetections, x, x + 1);
}
        }
tempDetections = newList;
        tempDetections = newList;
newList = [];
        newList = [];
for (x = 0; x < llGetListLength(requestedTranslations); x += 2)
        for (x = 0; x < llGetListLength(requestedTranslations); x += 2)
{
        {
timeElapsed = llGetTime() - llList2Float(llCSV2List(llList2String(requestedTranslations, x + 1)), 0);
            timeElapsed = llGetTime() - llList2Float(llCSV2List(llList2String(requestedTranslations, x + 1)), 0);
if (timeElapsed < 60.0)
            if (timeElapsed < 60.0)  
{
            {
newList = (newList=[]) + newList + llList2List(requestedTranslations, x, x + 1);
                newList = (newList=[]) + newList + llList2List(requestedTranslations, x, x + 1);
}
            }
}
        }
requestedTranslations = newList;
        requestedTranslations = newList;
if (isMaster > 0)
        if (isMaster > 0)
{
        {
for (r = 0; r < llGetListLength(agentsInTranslation); r += 4)
            for (r = 0; r < llGetListLength(agentsInTranslation); r += 4)
{
            {
tmrtmpStr = llList2String(llGetObjectDetails(llList2Key(agentsInTranslation, r), [OBJECT_POS]), 0);
                tmrtmpStr = llList2String(llGetObjectDetails(llList2Key(agentsInTranslation, r), [OBJECT_POS]), 0);
if ((llVecDist(llGetPos(), (vector)tmrtmpStr) <= 20.0) && (tmrtmpStr != ""))
                if ((llVecDist(llGetPos(), (vector)tmrtmpStr) <= 20.0) && (tmrtmpStr != ""))
{
                {
tmrnewli = (tmrnewli=[]) + tmrnewli + llList2List(agentsInTranslation, r, r + 3);
                    tmrnewli = (tmrnewli=[]) + tmrnewli + llList2List(agentsInTranslation, r, r + 3);
}
                }
}
            }
agentsInTranslation = tmrnewli;
            agentsInTranslation = tmrnewli;
}
        }
if ((autoLanguage) && isMaster > 0)
        if ((autoLanguage) && isMaster > 0)
{
        {
llSensor("", NULL_KEY, AGENT, 20.0, PI);
            llSensor("", NULL_KEY, AGENT, 20.0, PI);
}
        }
llSetTimerEvent(4.0); // restart timer
        llSetTimerEvent(4.0); // restart timer
}
    }
}
}


</lsl>
</lsl>

Revision as of 07:45, 19 December 2009

Bienvenid@

Hola, soy Perry Mizin y este será mi humilde intento de contribucion al proyecto "Traductor Universal de Google" creado por Hank Ramos.

El siguiente codigo fué originalmente creado por Hank Ramos, empezaré modificando algunas funciones a mi manera... No os riais

Welcome

Hello, I am Perry Mizin and this will be my humble try to contribute with the project "Universal Google Translator" created by Hank Ramos.

The next code was originally created by Hank Ramos, I will start modifiying some functions on my way... Don´t laugh


OK

Bueno, despues de respiñar todo lo que he podido, creo que el uso de memoria y la velocidad estan practicamente igual que en la version original pero bueno... en la variedad está el gusto no? Ya he probado un poco el codigo, y parece que funciona aunque estoy teniendo algunos problemas... Por ejemplo creo que el sistema de traduccion no funciona correctamente, y no me refiero al sistema de traduccion de Google (en mi opinion, si no es el mejor, es uno de los mejores del mundo), sino a que aun queda mucho trabajo por hacer en el script...

El siguiente paso sera intentar dividir el script en dos, o si es posible en tres para repartir el uso de memoria. Tambien me gustaria probar un truco que uso a menudo para traducir paginas en un idioma que no entiendo, lo que hago es que en vez de traducir directamente al español, primero lo traduzco a inglés, y luego desde inglés al español, parece una tonteria y un gasto innecesario, pero si lo hago es porque así suelo entender mejor las traducciones. Quiero por un lado el sistema de comunicacion y evaluacion entre traductores, y por otro el sistema de traduccion entre avatares, ¿es muy dificil? no se ya lo veremos...

Well, after saving as much as I could, I think the memory usage and the speed are technically the same as in the original script, but well, in the variety is the taste, isnt it?. I have tested a bit the code above already, and it seems to work altought i´m having some problems... For instance i think the translation system is not working properly, and i dont mean the Google´s translation system (in my opinion, if is not the best, one of the bests), but that there is still much work to do in the script...

The next step will be to try to divide the script in 2, or 3 if is possible, so we can spread the memory usage. Also I would like to test a trick i use often for to translate languages that i dont understand, what I do first is to translate that language into English, and then from English to Spanish, it seems a nonsense and unnecesary waste, but if i do it is becouse so i usually understand the translations better. I want by one part the translators communications and evaluation system, and by other the translation system between avatars, is it very difficult? I don´t know , we will see...


Es dificil... me está costando pero ya veremos... Its difficult... i am struggling but we will see...

chu chu chu


De Güatemala a Güatepeor, Sin comentarios...

From Güatebad to Güateworse, No comments...

Buscando en Internet he encontrado este API de Yahoo comprobador de ortografía, aunque creo que por ahora sólo soporta el lenguage inglés por ahora sería interesante añadirlo al proyecto

Searching on the web I crossed in my way with this spell checker Yahoo´s API , I think that it only supports English language by now althought it would be interesting to add it to the project


ESTO ES EL TRADUCTOR "fracaso" por ahora... Necesito limpiar el codigo y rehacer algunas cosas

THIS IS THE TRANSLATOR "mess" by now... I need to clean the code and rework some things


<lsl> //Personal Translator //Universal Translator (Mods made in Perry Mizin) //Version 1.9.0 //November 12, 2009 //LSL Script copyright 2006-2009 by Hank Ramos //Web Server Services powered by Google //Variables list requestedTranslations; list tempDetections; list agentsInTranslation; list requestList; integer listenID; integer isMaster = 1; integer autoLanguage = TRUE; integer enabled = FALSE; integer showTranslation = FALSE; integer tranObjects = TRUE; integer isInitialized = FALSE; string options; key tempValue; //Functions integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex {

   return ~llSubStringIndex(haystack, needle);

}

sendIM(key imid, string imstr) {

   llInstantMessage(imid, llUnescapeURL(imstr));
   if (imid != llGetOwner())
   {
   llOwnerSay(llUnescapeURL(imstr) + " @" + llKey2Name(imid));
   }

}

detectLanguage(string dtcmsg, key dtckey) {

   dtcmsg = "detect?v=1.0&q=" + llEscapeURL(dtcmsg);
   tempValue = llHTTPRequest("http://ajax.googleapis.com/ajax/services/language/" + dtcmsg, [HTTP_METHOD, "GET", HTTP_MIMETYPE, "plain/text;charset=utf-8"], " ");
   if (tempValue != NULL_KEY)
   {
       tempDetections = (tempDetections=[]) + tempDetections + [tempValue, dtckey];
   }

}

checkThrottle(integer chkn, string chkmsg, list chkli) {

   key returnValue;
   //loop though list and remove items older than 25 seconds
   chkmsg = "translate?v=1.0&q=" + chkmsg;
   requestList = (requestList=[]) + requestList + [llGetTime()];
   returnValue = llHTTPRequest("http://ajax.googleapis.com/ajax/services/language/" + chkmsg, [HTTP_METHOD, "GET", HTTP_MIMETYPE, "plain/text;charset=utf-8"], "");
   if (returnValue != NULL_KEY)
   {
           //Translation
           requestedTranslations = (requestedTranslations=[]) + requestedTranslations + [returnValue, llList2CSV(chkli)];
   }
   else
   {
       llSleep(40.0); //Something has gone horribly wrong, sleep 40 seconds to clear throttle
   }

}

string checkLanguage(string chklantmpStr) {

   if (chklantmpStr == "")
   {
       chklantmpStr = "en";
   }
   else if (chklantmpStr == "und")
   {
       chklantmpStr = "el";
   }
   if (llGetSubString(chklantmpStr, 0, 1) == "zh")
   {
       chklantmpStr = "zh-CN";
   }
   else
   {
       chklantmpStr = llGetSubString(chklantmpStr, 0, 1);
   }
   return chklantmpStr;

}

addAgent(key avid, string avlan, integer chklan) {

   integer avliPos;
   integer idNum;
   if (~llListFindList(agentsInTranslation, (list)avid))
   {
       avliPos = llListFindList(agentsInTranslation, (list)avid);
       agentsInTranslation = llListReplaceList(agentsInTranslation, [avlan, chklan], avliPos + 1, avliPos + 2);
   }
   else
   {
       @at;
       idNum = llRound(llFrand(2000000)) + 1;
       if(~llListFindList(agentsInTranslation, (list)idNum))
       {       
           jump at;
       }
       agentsInTranslation = (agentsInTranslation=[]) + agentsInTranslation + [avid, avlan, chklan, idNum];
       llMessageLinked(LINK_THIS, 64562349, avlan, avid);        
   }

}

string addNewAgent(key newavid) {

   string speakerLan; 
   if (llGetAgentSize(newavid) != ZERO_VECTOR)
   {
       speakerLan  = checkLanguage(llGetAgentLanguage(newavid));
       if (speakerLan == "")
       {
           speakerLan = "en";
           addAgent(newavid, speakerLan, 0);
       }
       else
       {
           addAgent(newavid, speakerLan, 0);
       }
   }
   return speakerLan;

}

key getAgentKey(integer agentID) {

   if(~llListFindList(agentsInTranslation, (list)agentID))
   {
       integer gkliPos = llListFindList(agentsInTranslation, (list)agentID);
       return llList2Key(agentsInTranslation, gkliPos - 3);
   }
   else
   {
       return "";
   }

}

processHTTPResponse(integer ntype, string bodyStr, list httli) {

   integer spkliPos;
   list    recepientList;
   key     recepientID;
   string  recepientLanguage;
   string  languagePair;
   key     speakerID;
   string  speakerName;
   string  speakerLanguage;
   string  translatedText;
   string  lantmpStr;
   integer z;
   list    paramtmpli;
   //===================
   //Process Translation
   //===================
       speakerID  = llList2Key(httli, 1);
       speakerName = llKey2Name(speakerID);
       if (speakerName == "")
       {
           speakerName = llList2String(llGetObjectDetails(speakerID, [OBJECT_NAME]), 0);
       }
       recepientList = llParseString2List(llList2String(httli, 2), ["@"], []);
       paramtmpli = llParseStringKeepNulls(llList2String(httli, 3), ["|"],[]);
       recepientLanguage = llList2String(paramtmpli, 1);
       languagePair = llDumpList2String(paramtmpli, ">>"); 
       //Perform Text Cleanup
       translatedText = llGetSubString(bodyStr, llSubStringIndex(bodyStr, "{\"translatedText\":\"") + 18, llSubStringIndex(bodyStr, "\",\"detectedSourceLanguage\":\""));
       //Reverse order if Recepient Language is Hebrew or Arabic
       if ((recepientLanguage == "iw") || (recepientLanguage == "ar"))
       {
           lantmpStr = "";
           for(z = llStringLength(translatedText);z >= 0; z--)
           {
               lantmpStr = (lantmpStr="") + lantmpStr + llGetSubString(translatedText, z, z); 
           }
           translatedText = lantmpStr;
       }   
       lantmpStr = speakerName + "(" + languagePair + "): " + translatedText;
       if (showTranslation)
       {
           sendIM(speakerID, lantmpStr);
       } 
       for (z = 0; z < llGetListLength(recepientList); z += 1)
       {
           recepientID = getAgentKey(llList2Integer(recepientList, z));
           if (recepientID != "")
           {
               recepientLanguage = llList2String(agentsInTranslation, llListFindList(agentsInTranslation, [recepientID]) + 1);
               if (recepientLanguage != speakerLanguage)
               {
                   sendIM(recepientID, lantmpStr);
               }
           }  
       }
       return;   

}

default {

   state_entry()
   {
        llSetTimerEvent(5);
   }

   link_message(integer sender_num, integer num, string lstr, key lid)
   {
       list ltmpli;
       if (num == 8434532)
       {
           enabled = (integer)lstr;
       }
       else if (num == 3342976)
       {
           //Send Preferences
           options = lstr;
           if (isInitialized == FALSE) return;
           ltmpli = llCSV2List(options);
           showTranslation = llList2Integer(ltmpli, 0);
           tranObjects = llList2Integer(ltmpli, 1);
           autoLanguage = llList2Integer(ltmpli, 2);
       }
   }

   sensor(integer num_detected)
   {
       integer s;
       key     sid;
       for (s = 0; s < num_detected; s += 1)
       {
           sid = llDetectedKey(s);
           if (~llListFindList(agentsInTranslation, (list)sid)) { }
           else
           {
               addNewAgent(sid);
           }
       }
   }
   
   no_sensor() { }
   
   listen(integer channel, string name, key id, string message)
   {
       integer w;
       string  spkrLan;
       string  recepLan;
       integer recepID;
       integer lstliPos;
       string  lanpair;
       list    translationCache;
       //Multiplexor Code
       //Translator Engine Code
       if ((!enabled) && (isMaster == 1))
       {
           return;
       } 
       if (!tranObjects)
       {
           if (llGetAgentSize(id) == ZERO_VECTOR)
           {
           return;
           } 
           else
           {
               detectLanguage(message, id);
           }
       }
       else
       {
           detectLanguage(message, id);
       }
           
       if (~llListFindList(agentsInTranslation, (list)id))
       {
           lstliPos = llListFindList(agentsInTranslation, (list)id);          
           spkrLan = llList2String(agentsInTranslation, lstliPos + 1);
       }
       else
       {
           spkrLan = addNewAgent(id);
       }
               //===============================
               //Formulate Translation Requests
               //===============================
       if (id == llGetOwner())
       {
           for (w = 0; w < llGetListLength(agentsInTranslation); w += 4)
           {
               //Loop through translation group and do appropriate translations as needed
               recepID = llList2Integer(agentsInTranslation, w + 3);
               recepLan =  checkLanguage(llList2String(agentsInTranslation, w + 1));
               if (spkrLan != recepLan) 
               {
                   lanpair = spkrLan + "|" + recepLan;
                   if (~llListFindList(translationCache, (list)lanpair))
                   {
                       lstliPos = llListFindList(translationCache, (list)lanpair);
                       translationCache = llListReplaceList(translationCache, [llList2String(translationCache, lstliPos + 1) + "@" + (string)recepID], lstliPos + 1, lstliPos + 1);
                   }
                   else
                   {
                       translationCache = (translationCache=[]) + translationCache + [lanpair, recepID];
                   }
               }
           }              
           //Process Requests
           if (translationCache != [])
           {
               for (w = 0; w < llGetListLength(translationCache); w += 2)
               {
                   //====================================
                   //Translation
                   //====================================
                   //Forumulate and Send Translation Request
                   lanpair = "|" + llList2String(llParseStringKeepNulls(llList2String(translationCache, w), ["|"],[]), 1); 
                   checkThrottle(0, llEscapeURL(message) + "&langpair=" + llEscapeURL(lanpair), [llGetTime(), id , llList2String(translationCache, w + 1), llList2String(translationCache, w)]);
               } 
           }   
       }
       else
       {
           lstliPos = llListFindList(agentsInTranslation, (list)llGetOwner());
           recepID = llList2Integer(agentsInTranslation, lstliPos + 3);
           recepLan =  checkLanguage(llList2String(agentsInTranslation , lstliPos + 1 ));
           if (spkrLan != recepLan) 
           {
               lanpair = spkrLan + "|" + recepLan;
               if (~llListFindList(translationCache, (list)lanpair))
               {
                   lstliPos = llListFindList(translationCache, (list)lanpair);
                   translationCache = llListReplaceList(translationCache, [llList2String(translationCache, lstliPos + 1) + "@" + (string)recepID], lstliPos + 1, lstliPos + 1);
               }
               else
               {
                   translationCache = (translationCache=[]) + translationCache + [lanpair, recepID];
               }                                          
               if (translationCache != [])
               {
                       //====================================
                       //Translation
                       //====================================
                       //Forumulate and Send Translation Request
                       lanpair = "|" + llList2String(llParseStringKeepNulls(llList2String(translationCache, lstliPos), ["|"],[]), 1); 
                       checkThrottle(0, llEscapeURL(message) + "&langpair=" + llEscapeURL(lanpair), [llGetTime(), id , llList2String(translationCache, lstliPos + 1), llList2String(translationCache, lstliPos)]);
               } 
           }                             
       }   
   }

   http_response(key request_id, integer status, list metadata, string body)
   {
       //Process Resonse Code
       integer httliPos;
       string htttmpStr;
       if (~llListFindList(requestedTranslations, (list)request_id))
       {
           httliPos = llListFindList(requestedTranslations, [request_id]);
           processHTTPResponse(0, body, llCSV2List(llList2String(requestedTranslations, httliPos + 1)));
           requestedTranslations = llDeleteSubList(requestedTranslations, httliPos, httliPos + 1);
           return;
       }
       if (~llListFindList(tempDetections, (list)request_id))
       {
           integer tmpliPos = llListFindList(tempDetections, (list)request_id);
           key speakerID = llList2Key(tempDetections, tmpliPos + 1);
           tempDetections = llDeleteSubList(tempDetections, tmpliPos, tmpliPos + 3);
           string tmptmpStr = llToLower(llGetSubString(body, llSubStringIndex(body, "{\"language\":\"") + 13, llSubStringIndex(body, "\",\"isReliable\":") - 1));
           if (contains(tmptmpStr, "err") == 0 && contains(tmptmpStr, "unn") == 0 )
           {
               tmptmpStr = checkLanguage(tmptmpStr);
               if (~llListFindList(agentsInTranslation, (list)speakerID))
               {
                   integer tmpavliPos = llListFindList(agentsInTranslation, (list)speakerID);
                   string actlan = llList2String(agentsInTranslation, tmpavliPos + 1);
                   if (actlan != tmptmpStr)
                   {
                       agentsInTranslation = llListReplaceList(agentsInTranslation,[llList2Integer(agentsInTranslation,tmpavliPos + 2) + 1] , tmpavliPos + 2, tmpavliPos + 2);
                   } 
                   else 
                   {
                       agentsInTranslation = llListReplaceList(agentsInTranslation,[0], tmpavliPos + 2, tmpavliPos + 2);
                   }
                   if (llList2Integer(agentsInTranslation,tmpavliPos + 2) >= 4) //if any av talks on any language 4 times in a row
                   {
                       if (tmptmpStr == "")
                       {
                           return;
                       }
                       else
                       {
                           addAgent(speakerID, tmptmpStr, 0);
                       }
                   }                
               }                
           }            
       }              
   }
   timer()
   {
       llSetTimerEvent(0.0);
       if (isInitialized == FALSE)
       {
           enabled = TRUE;
           listenID = llListen(0, "", "", ""); 
           llMessageLinked(LINK_THIS, 6877259, (string)enabled, NULL_KEY);
           isInitialized = TRUE;
           string mylanguage = llGetAgentLanguage(llGetOwner());
           if (llStringLength(mylanguage) < 1)
           {
               mylanguage = "en";
           }
           agentsInTranslation = (agentsInTranslation=[]) + agentsInTranslation + [llGetOwner() , mylanguage , 0 , llRound(llFrand(2000000)) + 1];
       }
       integer r;
       string  tmrtmpStr;
       list    tmrnewli; 
       integer x;
       list  newList;
       float timeElapsed;  
       for (x = 0; x < llGetListLength(tempDetections); x += 2)
       {
           timeElapsed = llGetTime() - llList2Float(llCSV2List(llList2String(tempDetections, x + 1)), 0);
           if (timeElapsed < 60.0) 
               newList = (newList=[]) + newList + llList2List(tempDetections, x, x + 1);
       }
       tempDetections = newList;
       newList = [];
       for (x = 0; x < llGetListLength(requestedTranslations); x += 2)
       {
           timeElapsed = llGetTime() - llList2Float(llCSV2List(llList2String(requestedTranslations, x + 1)), 0);
           if (timeElapsed < 60.0) 
           {
               newList = (newList=[]) + newList + llList2List(requestedTranslations, x, x + 1);
           }
       }
       requestedTranslations = newList;
       if (isMaster > 0)
       {
           for (r = 0; r < llGetListLength(agentsInTranslation); r += 4)
           {
               tmrtmpStr = llList2String(llGetObjectDetails(llList2Key(agentsInTranslation, r), [OBJECT_POS]), 0);
               if ((llVecDist(llGetPos(), (vector)tmrtmpStr) <= 20.0) && (tmrtmpStr != ""))
               {
                   tmrnewli = (tmrnewli=[]) + tmrnewli + llList2List(agentsInTranslation, r, r + 3);
               }
           }
           agentsInTranslation = tmrnewli;
       }
       if ((autoLanguage) && isMaster > 0)
       {
           llSensor("", NULL_KEY, AGENT, 20.0, PI);
       }
       llSetTimerEvent(4.0); // restart timer
   }

}

</lsl>