Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Translator Open_BabelFish_Translator  

Open_BabelFish_Translator

Open BabelFish Query

Category: Translator
By : falados
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Open_BabelFish_Translator
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Open_BabelFish_Translator_1.lsl
Get file # 2. Open_BabelFish_Translator_2.lsl
1 // This file is part of Open Babel Fish.
2 //
3 // Open Babel Fish is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Open Babel Fish is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Open Babel Fish. If not, see <http://www.gnu.org/licenses/>
15 //
16 // Author: Falados Kapuskas
17 // Date: 3/25/2008
18 // Version: 1.3
19
20
21 string URL;
22
23 key owner;
24 integer ENCODE = 0;
25 integer DECODE = 1;
26 string MY_NAME = "Babel Fish";
27
28 //Rate Limit Queueing
29 key gDataserverRequest;
30 float rate = 1.0; //Every second
31 integer MessageQueue_length;
32 list MessageQueue_lp;
33 list MessageQueue_data;
34 list MessageQueue_name;
35
36 //Translation Stuff
37 list trans_requests;
38 list http_parm = [
39 HTTP_METHOD, "POST",
40 HTTP_MIMETYPE,"application/x-www-form-urlencoded"
41 ];
42
43 //Function: queue_message
44 //Description: Queues a message for translation later
45 queue_message(string data, string langpair, string name) {
46 //Hack for english to english
47 if( langpair == "en|en" ) {
48 llSetObjectName(name);
49 llOwnerSay(data);
50 } else {
51 ++MessageQueue_length;
52 MessageQueue_lp += langpair;
53 MessageQueue_data += data;
54 MessageQueue_name += name;
55 }
56 }
57
58 //Function: send_queued_message
59 //Description: Sends the first message on the queue
60 send_queued_message() {
61 if( MessageQueue_length > 0 )
62 {
63 //Prepare the request
64 string request = translate_text( llList2String(MessageQueue_data,0), llList2String(MessageQueue_lp,0) );
65 string translate_name = llList2String(MessageQueue_name,0);
66
67 //Remove the queued item
68 MessageQueue_lp = llDeleteSubList(MessageQueue_lp,0,0);
69 MessageQueue_data = llDeleteSubList(MessageQueue_data,0,0);
70 MessageQueue_name = llDeleteSubList(MessageQueue_name,0,0);
71
72 //Fire it
73 trans_requests += [llHTTPRequest( URL, http_parm, request ),translate_name];
74 --MessageQueue_length;
75 }
76 }
77
78 //Function: translate_text
79 //Description: This funciton simply assemples the proper POST data that must
80 //be sent to the php script. It returns that data
81 string translate_text( string in, string pair )
82 {
83 llOwnerSay(pair);
84 string request = "action=translate&langpair=" + pair + "&text=" + llEscapeURL(in);
85 return request;
86 }
87
88 default {
90 {
91 gDataserverRequest = llGetNotecardLine("URL",0);
92 owner = llGetOwner();
93 }
94 changed( integer change )
95 {
97 }
98 dataserver(key req, string data)
99 {
100 if( req == gDataserverRequest )
101 {
102 URL = data;
103 state active;
104 }
105 }
106 }
107 state active
108 {
110 {
111 if( URL == "" || URL == EOF ) {
112 llOwnerSay("Add a URL in the URL Notecard");
113 } else {
114 llSetTimerEvent(rate);
115 }
116 }
117 changed( integer change )
118 {
120 }
121 link_message(integer sender_number, integer number, string message, key id)
122 {
123 if( number == -10 ) {
124 list opts = llCSV2List((string)id);
125 queue_message(message,llList2String(opts,0),llList2String(opts,1));
126 }
127 }
128 timer()
129 {
130 //Pop 1 off the message queue every RATE seconds
131 send_queued_message();
132 }
133 //From the server - Say the message that was received
134 http_response( key request_id, integer status, list metadata, string body )
135 {
136 //Find the request in the pending queue
137 integer i = llListFindList(trans_requests,[request_id]);
138 string translate_name = llList2String(trans_requests,i+1);
139 if(status != 200)
140 {
141 if( status == 499 ) llOwnerSay("Request Timed Out.");
142 else llOwnerSay("ERROR " + (string)status);
143 return;
144 }
145 //If found, procede with textual display
146 if( i != -1 ) {
147 llSetObjectName(translate_name );
148 if( translate_name == llKey2Name(owner) ) {
149 //If the owner's text is being translated, the Babel Fish should speak it to others
150 llSay( 0 , llUnescapeURL( body ) );
151 } else {
152 //Otherwise, this is the translation you hear.
153 llOwnerSay( llUnescapeURL( body ) );
154 }
155 llSetObjectName(MY_NAME); //Reset name
156 //Remove the request from the queue
157 trans_requests = llDeleteSubList(trans_requests,i,i+1);
158 }
159 }
160 }

Open_BabelFish_Translator

Open BabelFish

Category: Translator
By : falados
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 // This file is part of Open Babel Fish.
2 //
3 // Open Babel Fish is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Open Babel Fish is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Open Babel Fish. If not, see <http://www.gnu.org/licenses/>
15 //
16 // Author: Falados Kapuskas
17 // Date: 7/20/2008
18 // Version: 1.4
19
20
21 //CONSTANT PARAMETERS
22
23 float AUTO_TRANSLATE_POLL_TIME = 40.0; //Amount of time between auto-translation announcements
24 string NATIVE_LANGUAGE;
25 integer AUTO;
26 string MY_NAME = "Babel Fish";
27
28 integer LANGUAGE_ANNOUNCE = 234235; //Please keep this standard amongs all revisions
29 //If you dont, then the auto-detect language wont work for you.
30
31 integer CONTROL_CHANNEL = 55; //This is the channel on which you chat commands
32 //This can be changed to whatever
33
34 //Globals
35 key owner;
36 string PARTIAL; //Partial name for 'tr' command
37 integer FIRST = TRUE; //First run of the translator state
38 string USAGE;
39 integer master_listen;
40
41 //Listeners
42 list listeners = [];
43
44 //Function: translate
45 //Description: Sends a message out for translation.
46 translate(string data, string langpair, string name) {
47 llMessageLinked(LINK_THIS,-10,data,langpair + "," + name);
48 }
49
50 //Function: add_listener
51 //Description: Adds a listener to the translate list by name.
52 add_listener( string name, string pair ) {
53 if( pair == NATIVE_LANGUAGE + "|" + NATIVE_LANGUAGE ) {
54 return;
55 }
56 integer i = llListFindList( listeners, [name,pair]);
57 integer j = llListFindList( listeners, [name]);
58 if( j == -1 ) {
59 listeners += [name,pair];
60 string text;
61 //Tell the user a new listener was added
62 if( name == llKey2Name(owner) ) {
63 text = "Your language changed";
64 } else {
65 text = name + " added";
66 }
67 translate(
68 text,
69 "en|" + NATIVE_LANGUAGE,
70 MY_NAME
71 );
72 } else if( i == -1) {
73 listeners = llListReplaceList( listeners , [pair] , j+1 , j+1 );
74 }
75 }
76
77 //Funciton: remove_name
78 //Description: Remove a listener from the list
79 remove_name( string name ){
80 integer i = llListFindList( listeners , [name]);
81 if( i != -1 ) {
82 translate(
83 name + " removed",
84 "en|" + NATIVE_LANGUAGE,
85 MY_NAME
86 );
87 listeners = llDeleteSubList( listeners, i,i+1);
88 }
89 }
90
91 // ------ STATES ------- //
92
93 //Setup State
94 default
95 {
96 on_rez(integer p) { if(llGetOwner() != owner) llResetScript(); }
98 {
99 owner = llGetOwner();
100 llMessageLinked(LINK_THIS,-3,"setup","root");
101 }
102 link_message(integer sender_number, integer number, string message, key id)
103 {
104 if( number == 0 && message == "init" && id == "dialog" ) {
105 llSetTimerEvent(0.0);
106 state setup;
107 }
108 }
109 timer() {
110 llMessageLinked(LINK_THIS,-3,"setup","root");
111 }
112 }
113
114 //State: setup
115 //Description: Attempts to retrieve the native langauge of the current user
116 state setup {
118 state_entry() {
119 llMessageLinked(LINK_THIS,0,"ask_lang",owner);
120 }
121 link_message(integer sender_number, integer number, string message, key id)
122 {
123 if( number == -1 && id == owner) {
124 NATIVE_LANGUAGE = message;
125 state translator;
126 }
127 if( number == -4 && message == "timeout" && id == owner ) {
128 llMessageLinked(LINK_THIS,0,"ask_lang",owner);
129 }
130 }
131 }
132
133 //State: translator(_loop)
134 //Description: This is the main loop where all the magic happens.
135 //This state accepts user commands, adds listeners, and does the translations
136 //The main HTTP request line is in here.
137 //This state also does the setup to determining the user's native language
138 //The NATIVE_LANGUAGE variable is extremely important when it comes to most
139 //functions, especially automatic translation.
140 state translator_loop { state_entry() { state translator;}}
141 state translator {
142 on_rez(integer p) { if(llGetOwner() != owner) llResetScript(); }
143 state_entry() {
144 //If this is the first run through, say the usage line
145 if(FIRST) {
146 string tr = "|";
147 if(NATIVE_LANGUAGE == "en") tr = "";
148 //TODO: This usage line should be updated when new functions come out
149 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "autotrans" + tr + " <name> to automatically translate someone.";
150 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "translate" + tr + " <name> to ask for translation.";
151 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "remove" + tr + " <name> to remove a translated person.";
152 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "remove all" + tr + " to remove all translated people.";
153 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "speak" + tr + " to speak another language through " + MY_NAME;
154 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "list" + tr + " to display the people who are now translated.";
155 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "help" + tr + " to display this message.";
156 USAGE += "\nType /" + (string)CONTROL_CHANNEL + " " + tr + "reset" + tr + " to reset Open Babel Fish.";
157
158 translate(
159 USAGE,
160 "en|" + NATIVE_LANGUAGE,
161 MY_NAME
162 );
163 FIRST = FALSE;
164 }
165
166 //Listen for Auto-Language Detection
167 llListen(LANGUAGE_ANNOUNCE,"",NULL_KEY,"");
168
169 //Listen for Commands
170 master_listen = llListen(CONTROL_CHANNEL,"",llGetOwner(),"");
171
172 //Listen to all the people who need to be translated
173
174 //Listen For All People
175 if( llGetListLength(listeners) )
176 llListen(0,"","","");
177
178 //Set time between announcing your native language to others
179 llSetTimerEvent(AUTO_TRANSLATE_POLL_TIME);
180 }
181 //Commands and auto-translate in here
182 listen ( integer channel, string name, key id, string message )
183 {
184 message = llStringTrim( message, STRING_TRIM);
185
186 //Chat commands
187 if( channel == CONTROL_CHANNEL ) {
188 llSetObjectName( MY_NAME );
189 integer i = llSubStringIndex(message," ");
190 string command = llGetSubString(message,0,i-1);
191 string param = llGetSubString(message,i+1,-1);
192 integer len = llStringLength( message );
193 if( command == "autotrans") {
194 PARTIAL = param;
195 AUTO=TRUE;
196 llSensor("",NULL_KEY,AGENT,30,TWO_PI);
197 }
198 if( command == "translate" ) { //Ask for Translation
199 AUTO = FALSE;
200 PARTIAL = param;
201 llSensor("",NULL_KEY,AGENT,30,TWO_PI);
202 }
203 if( command == "remove" ) { //Remove Name
204 if( param == "all" ) {
205 listeners = [];
206 translate(
207 "All listeners removed",
208 "en|" + NATIVE_LANGUAGE,
209 MY_NAME
210 );
211 state translator_loop;
212 } else {
213 remove_name( param );
214 }
215 //Reset Listens
216 state translator_loop;
217 }
218 if( message == "help" ) {
219 translate(
220 USAGE,
221 "en|" + NATIVE_LANGUAGE,
222 MY_NAME
223 );
224 }
225 if( message == "list" ) {
226 integer nl = llGetListLength(listeners)/2;
227 integer j;
228 for( j = 0; j < nl; ++j) {
229 llOwnerSay(llList2String(listeners,j*2) + " : " + llList2String(listeners,j*2+1) );
230 }
231 }
232 if( message == "speak" ) {
233 llMessageLinked(LINK_THIS,0,"ask_lang",owner);
234 }
235 if( message == "reset") llResetScript();
236 }
237 //Listen for translations
238 if( channel == 0 ) {
239 integer i = llListFindList( listeners , [name] );
240 if( i != -1) {
241 //Langpair found, translate the message
242 translate(
243 message,
244 llList2String( listeners , i+1),
245 name
246 );
247 }
248 }
249 //Auto-translate messagse
250 if( channel == LANGUAGE_ANNOUNCE ) {
251 llMessageLinked(LINK_THIS,-2,message,id);
252 }
253 }
254
255
256 //This event is mainly for partial-name matching
257 //PARTIAL is set with the partial name. if the partial name is found.
258 //The user gets asked the "What language?" dialog. Upon their response, they will
259 //be added to the listeners list.
260 sensor(integer tn) {
261 integer i = 0;
262 string announce;
263 string p;
264 string n;
265 p = llToLower(PARTIAL);
266 for( i = 0; i < tn; ++i) {
268 if(llSubStringIndex(n,p) != -1) { //Partial name locate inside fullname
269 //Ask them for their language
270 if( AUTO ) {
271 add_listener( llDetectedName(i), "auto|" + NATIVE_LANGUAGE );
272 announce = "Automatically Translating: " + llDetectedName(i);
273 translate(
274 announce,
275 "en|" + NATIVE_LANGUAGE,
276 MY_NAME
277 );
278 } else {
279 announce = "Asking " + llDetectedName(i) + " for translation";
280 translate(
281 announce,
282 "en|" + NATIVE_LANGUAGE,
283 MY_NAME
284 );
286 }
287 return;
288 }
289 }
290 //If for loop exits, must be nothing found
291 announce = "None found";
292 translate(
293 announce,
294 "en|" + NATIVE_LANGUAGE,
295 MY_NAME
296 );
297 }
298 no_sensor()
299 {
300 string announce = "None found";
301 translate(
302 announce,
303 "en|" + NATIVE_LANGUAGE,
304 MY_NAME
305 );
306 }
307 timer()
308 {
310 llSay(LANGUAGE_ANNOUNCE,NATIVE_LANGUAGE);
311 }
312
313 link_message(integer sender_number, integer number, string message, key id)
314 {
315 if( number == -1 ) { //Is this a translation addition from the dialog?
316 //from this: llMessageLinked(LINK_THIS,-1,llList2String(language_names,i),id);
317 if( id == owner ) //Self Translate
318 add_listener( llKey2Name(id), NATIVE_LANGUAGE + "|" + message );
319 else //Translate Other
320 add_listener( llKey2Name(id), message + "|" + NATIVE_LANGUAGE);
321 state translator_loop;
322 }
323 if( number == -4 && message == "timeout") {
324 translate(
325 "Language request timeout - " + llKey2Name(id),
326 "en|" + NATIVE_LANGUAGE,
327 MY_NAME
328 );
329 }
330 }
331 }

Back to the Best Free Tools in Second Life and OpenSim.