Tuesday, December 20, 2011

Can JavaScript be used to program a human language translator?

I'm trying to think up ideas for projects. I was wondering if JavaScript could be used to create a human language translator? For example: If I were to type Hello and click a button to have it converted to spanish...the program would output: Hola.





Is this possible with this language? I'm not looking to do it in a different language. If it is possible, any suggestions,links, etc...that would help get me started?|||Use the Google Translate API:





http://code.google.com/apis/ajaxlanguage…





Here's a sample...





googleTranslationTest.html


~~~~~~~~~~~~~~~~~~~~~~~~~~


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;meta http-equiv = "content-type" content = "text/html; charset=utf-8" /%26gt;


%26lt;script type = "text/javascript" src = "http://www.google.com/jsapi"%26gt;%26lt;/script%26gt;


%26lt;script type = "text/javascript" src = "googleTranslationController.js"%26gt;%26lt;/scrip…


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;form%26gt;


Enter text in any language, pick a language to %26lt;br %26gt;


which to translate, and click "translate."




%26lt;textarea id="text" cols="60" rows="5"%26gt;%26lt;/textarea%26gt;



%26lt;select id="toLang"%26gt;


%26lt;option value="en" selected="true" %26gt;English%26lt;/option%26gt;


%26lt;option value="fr" %26gt;Français%26lt;/option%26gt;


%26lt;option value="de" %26gt;Deutsch%26lt;/option%26gt;


%26lt;option value="fi" %26gt;Suomen%26lt;/option%26gt;


%26lt;option value="it" %26gt;Italiano%26lt;/option%26gt;


%26lt;option value="pt" %26gt;Português%26lt;/option%26gt;


%26lt;option value="ru" %26gt;Русский%26lt;/option%26gt;


%26lt;option value="zh-CN" %26gt;漢語%26lt;/option%26gt;


%26lt;/select%26gt;


%26lt;input type = "button" value = "translate" onclick = "doTranslate();" /%26gt;


%26lt;/form%26gt;


%26lt;div id = "gBranding"%26gt;%26lt;/div%26gt;


%26lt;p id = "translation"%26gt;%26lt;/p%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





googleTranslationController.js


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// depends on pre-inclusion of http://www.google.com/jsapi


google.load("language", "1");// load language API (version 1) to page


function get(eid) {return document.getElementById( eid );} // convenience function


;


function doTranslate(){


var text = get( 'text' ).value;// get text to be translated


// detect source language of text - send result to translate() for handling


google.language.detect( text, translate );


}


;


function translate( r ) {


if (r.error) {// if from language detection failed, error property exists


showTranslatedText( r );// send response with error to display handler


return;// exit immediately


}


//...otherwise, attempt translation - implicit {else} of preceding {if}


var fromLang = r.language;// source language determined by API


var toLang = get( 'toLang' ).value;// get language to which to translate


var text = get( 'text' ).value;// get text to be translated


// attempt translation - send result to function showTranslatedText()


google.language.translate( text, fromLang, toLang, showTranslatedText );


}


;


function showTranslatedText( r ) {


// if error, replace translation test with error message


if (r.error) r.translation = 'translation error: ' + r.error.message;


var t = get( 'translation' );// get reference to target display element


t.innerHTML = r.translation;// insert translated text or error message


}


;


window.onload = function() { google.language.getBranding( 'gBranding' ); };|||I'm not a JS expert, but I am a programming expert. With my limited experience with JavaScript, I think it would be a poor choice for a human language translator (HLT), because all the JS code has to be downloaded to the client computer. An HLT would have thousands of words, and that just wouldn't be feasible for most browsers. Even if the receiving computer had enough memory, it would take too long to receive. To build an HLT, you could use JS in conjunction with something else (a server-side language). But since you're not interested in that, I won't elaborate. HTH|||Well you can try of course ,it can be done with JavaScript.


that's why there is


JavaScript + Ajax.! :D

No comments:

Post a Comment