...

View Full Version : text Translator



skyonline
12-15-2009, 03:09 AM
hai....any of you got free javascripts text Translator code...i really need it ASAP..tq..

i got one but it not work very good when i put 1000++ words...example :


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/why/g, "y");
phrase = phrase.replace(/are/g, "r");
phrase = phrase.replace(/you/g, "u");
phrase = phrase.replace(/for/g, "4");
phrase = phrase.replace(/to/g, "2");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

jmrker
12-15-2009, 04:38 AM
What's the problem with the script ... too slow with 1000++ words?

Note: if 'CASE' is a problem, add this...


phrase = phrase.replace(/why/ig, "y");

to each statment.

skyonline
12-15-2009, 06:18 AM
What's the problem with the script ... too slow with 1000++ words?

Note: if 'CASE' is a problem, add this...


phrase = phrase.replace(/why/ig, "y");

to each statment.

no..ist not about slow...try this and copy on frontpage and preview and tyep peradaban...the result is perhaveban... its not same with true result..the true answer peradaban = civilization... phrase = phrase.replace(/peradaban/ig, "civilization");....why..? its hapen when i put more 1000 ++ words...and many words on list its not same result..


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/ambung/ig, "back-basket");
phrase = phrase.replace(/amin/ig, "amen");
phrase = phrase.replace(/amir/ig, "emir");
phrase = phrase.replace(/ampai/ig, "hang");
phrase = phrase.replace(/ampai-ampai/ig, "jellyfish");
phrase = phrase.replace(/mengampaikan/ig, "to hang clothes out to dry");
phrase = phrase.replace(/terampai/ig, "hung to air");
phrase = phrase.replace(/ampaian/ig, "clothes-line");
phrase = phrase.replace(/ampu/ig, "ingratiate");
phrase = phrase.replace(/mengampu/ig, "to ingratiate");
phrase = phrase.replace(/pengampu/ig, "sycophant");
phrase = phrase.replace(/ampun/ig, "pardon");
phrase = phrase.replace(/keampunan/ig, "forgiveness");
phrase = phrase.replace(/mengampunkan/ig, "to forgive");
phrase = phrase.replace(/pengampun/ig, "one who forgives");
phrase = phrase.replace(/pengampunan/ig, "amnesty");
phrase = phrase.replace(/amuk/ig, "amuck");
phrase = phrase.replace(/mengamuk/ig, "attack without thinking");
phrase = phrase.replace(/pengamuk/ig, "person who runs amuck");
phrase = phrase.replace(/anai-anai/ig, "termite");
phrase = phrase.replace(/anak/ig, "child");
phrase = phrase.replace(/anak angkat/ig, "adopted child");
phrase = phrase.replace(/anak bulan/ig, "new moon");
phrase = phrase.replace(/anak dara/ig, "virgin");
phrase = phrase.replace(/anak merah/ig, "newborn");
phrase = phrase.replace(/anak-beranak/ig, "together with the children");
phrase = phrase.replace(/anak-anak/ig, "many children");
phrase = phrase.replace(/anak-anakan/ig, "doll");
phrase = phrase.replace(/anakanda/ig, "son");
phrase = phrase.replace(/beranak/ig, "to bear a child");
phrase = phrase.replace(/keanak-anakan/ig, "childish");
phrase = phrase.replace(/peranakan/ig, "native");
phrase = phrase.replace(/anasir/ig, "an element");
phrase = phrase.replace(/anasir jahat/ig, "bad social element");
phrase = phrase.replace(/ancam/ig, "menace");
phrase = phrase.replace(/mengancam/ig, "to threaten");
phrase = phrase.replace(/ancaman/ig, "a threat");
phrase = phrase.replace(/terancam/ig, "threatened");
phrase = phrase.replace(/anda/ig, "you");
phrase = phrase.replace(/andai/ig, "if");
phrase = phrase.replace(/andainya/ig, "suppose");
phrase = phrase.replace(/andaian/ig, "assumption");
phrase = phrase.replace(/berandai/ig, "to discuss");
phrase = phrase.replace(/mengandaikan/ig, "suppose");
phrase = phrase.replace(/andam/ig, "beautician who does the make-up for the bride and bridegroom");
phrase = phrase.replace(/mengandam/ig, "to make-up the bride and bridegroom");
phrase = phrase.replace(/andas/ig, "anvil");
phrase = phrase.replace(/anduh/ig, "suspend by means of a sling");
phrase = phrase.replace(/aneh/ig, "bizarre");
phrase = phrase.replace(/aneka/ig, "multifarious");
phrase = phrase.replace(/aneka ragam/ig, "variety show");
phrase = phrase.replace(/beraneka/ig, "having a variety of");
phrase = phrase.replace(/anekarama/ig, "concert");
phrase = phrase.replace(/angan/ig, "imagination");
phrase = phrase.replace(/angan-angan/ig, "imagination");
phrase = phrase.replace(/berangan-angan/ig, "to day-dream");
phrase = phrase.replace(/mengangan-angankan/ig, "to wish for");
phrase = phrase.replace(/terangan-angan/ig, "to constantly wish for");
phrase = phrase.replace(/angau/ig, "lovesick");
phrase = phrase.replace(/anggap/ig, "assume");
phrase = phrase.replace(/menganggap/ig, "to assume");
phrase = phrase.replace(/anggapan/ig, "assumption");
phrase = phrase.replace(/beranggapan/ig, "of the opinion");
phrase = phrase.replace(/anggar/ig, "estimate");
phrase = phrase.replace(/anggaran/ig, "estimation");
phrase = phrase.replace(/menganggarkan/ig, "to estimate");
phrase = phrase.replace(/anggerik/ig, "orchid");
phrase = phrase.replace(/anggota/ig, "limb");
phrase = phrase.replace(/beranggota/ig, "having limbs");
phrase = phrase.replace(/anggota badan/ig, "part of the body");
phrase = phrase.replace(/keanggotaan/ig, "membership");
phrase = phrase.replace(/menganggotai/ig, "to be a member of");
phrase = phrase.replace(/angguk/ig, "nod");
phrase = phrase.replace(/anggukan/ig, "act of nodding");
phrase = phrase.replace(/mengangguk/ig, "to nod");
phrase = phrase.replace(/terangguk-angguk/ig, "to keep nodding");
phrase = phrase.replace(/anggur/ig, "grape");
phrase = phrase.replace(/menganggur/ig, "unemployed");
phrase = phrase.replace(/penganggur/ig, "one who is unemployed");
phrase = phrase.replace(/pengangguran/ig, "unemployment");
phrase = phrase.replace(/angin/ig, "wind");
phrase = phrase.replace(/berangin/ig, "windy");
phrase = phrase.replace(/mengangin/ig, "to winnow");
phrase = phrase.replace(/menganginkan/ig, "to put in a windy place");
phrase = phrase.replace(/peranginan/ig, "holiday resort");
phrase = phrase.replace(/angka/ig, "numerical figure");
phrase = phrase.replace(/perangkaan/ig, "statistics");
phrase = phrase.replace(/angkara/ig, "bad");
phrase = phrase.replace(/angkasa/ig, "space");
phrase = phrase.replace(/angkasawan/ig, "astronaut");
phrase = phrase.replace(/angkat/ig, "lift");
phrase = phrase.replace(/angkatan/ig, "troops");
phrase = phrase.replace(/berangkat/ig, "to depart");
phrase = phrase.replace(/keberangkatan/ig, "departure");
phrase = phrase.replace(/angkuh/ig, "haughty");
phrase = phrase.replace(/keangkuhan/ig, "arrogance");
phrase = phrase.replace(/angkup/ig, "callipers");
phrase = phrase.replace(/angkut/ig, "convey");
phrase = phrase.replace(/mengangkut/ig, "to transport");
phrase = phrase.replace(/berangkut/ig, "to transport several times");
phrase = phrase.replace(/pengangkutan/ig, "transportation");
phrase = phrase.replace(/angsa/ig, "goose");
phrase = phrase.replace(/aniaya/ig, "maltreatment");
phrase = phrase.replace(/menganiayai/ig, "to oppress");
phrase = phrase.replace(/penganiaya/ig, "oppressor");
phrase = phrase.replace(/penganiayaan/ig, "oppression");
phrase = phrase.replace(/anjak/ig, "shift position slightly");
phrase = phrase.replace(/beranjak/ig, "to shift position slightly");
phrase = phrase.replace(/menganjakkan/ig, "to change the position of");
phrase = phrase.replace(/anjal/ig, "elastic");
phrase = phrase.replace(/keanjalan/ig, "flexibility");
phrase = phrase.replace(/menganjal/ig, "to be elastic");
phrase = phrase.replace(/anjing/ig, "dog");
phrase = phrase.replace(/anjung/ig, "projecting facade");
phrase = phrase.replace(/anjungan/ig, "bridge");
phrase = phrase.replace(/anjur/ig, "to jut out");
phrase = phrase.replace(/menganjur/ig, "to jut out");
phrase = phrase.replace(/anjuran/ig, "proposal");
phrase = phrase.replace(/menganjurkan/ig, "to propose");
phrase = phrase.replace(/penganjur/ig, "organizer");
phrase = phrase.replace(/ansur/ig, "progress");
phrase = phrase.replace(/beransur/ig, "to progress in stages");
phrase = phrase.replace(/ansuran/ig, "instalment");
phrase = phrase.replace(/mengansurkan/ig, "to pay by instalments");
phrase = phrase.replace(/antah/ig, "rice grains left unhusked after pounding");
phrase = phrase.replace(/antan/ig, "pestle");
phrase = phrase.replace(/antara/ig, "among");
phrase = phrase.replace(/mengantarai/ig, "to mediate");
phrase = phrase.replace(/perantara/ig, "mediator");
phrase = phrase.replace(/perantaraan/ig, "mediation");
phrase = phrase.replace(/antarabangsa/ig, "international");
phrase = phrase.replace(/antero/ig, "whole");
phrase = phrase.replace(/seantero/ig, "the whole");
phrase = phrase.replace(/anti/ig, "opposed to");
phrase = phrase.replace(/anting/ig, "pendulum");
phrase = phrase.replace(/abai/ig, "neglect");
phrase = phrase.replace(/mengabaikan/ig, "to disregard");
phrase = phrase.replace(/abang/ig, "elder brother");
phrase = phrase.replace(/abang angkat/ig, "foster brother");
phrase = phrase.replace(/abang ipar/ig, "brother-in-law");
phrase = phrase.replace(/abang tiri/ig, "stepbrother");
phrase = phrase.replace(/abdi/ig, "slave");
phrase = phrase.replace(/mengabdikan/ig, "to enslave");
phrase = phrase.replace(/pengabdian/ig, "slavery");
phrase = phrase.replace(/abjad/ig, "alphabet");
phrase = phrase.replace(/abu/ig, "ash");
phrase = phrase.replace(/mengabui/ig, "to cheat");
phrase = phrase.replace(/abuk/ig, "dust");
phrase = phrase.replace(/abung/ig, "pomelo");
phrase = phrase.replace(/acah/ig, "tease");
phrase = phrase.replace(/mengacah-acah/ig, "to tease");
phrase = phrase.replace(/acapkali/ig, "often");
phrase = phrase.replace(/acar/ig, "pickle");
phrase = phrase.replace(/acara/ig, "event");
phrase = phrase.replace(/mengacarakan/ig, "to compere");
phrase = phrase.replace(/pengacara/ig, "master of ceremonies");
phrase = phrase.replace(/aci/ig, "shaft");
phrase = phrase.replace(/acu/ig, "to direct weapon at target");
phrase = phrase.replace(/mengacukan/ig, "to aim weapon at target");
phrase = phrase.replace(/mengacu/ig, "to test");
phrase = phrase.replace(/acuan/ig, "a mould");
phrase = phrase.replace(/acuh/ig, "to care for");
phrase = phrase.replace(/mengacuhkan/ig, "to care for");
phrase = phrase.replace(/acuh tak acuh/ig, "to show indifference");
phrase = phrase.replace(/acum/ig, "provoke");
phrase = phrase.replace(/mengacum/ig, "to provoke");
phrase = phrase.replace(/acuman/ig, "provocation");
phrase = phrase.replace(/pengacum/ig, "instigator");
phrase = phrase.replace(/pengacuman/ig, "agitation");
phrase = phrase.replace(/ada/ig, "have");
phrase = phrase.replace(/adakala/ig, "sometimes");
phrase = phrase.replace(/adakalanya/ig, "sometimes");
phrase = phrase.replace(/adalah/ig, "is");
phrase = phrase.replace(/adapun/ig, "with regard to");
phrase = phrase.replace(/berada/ig, "rich");
phrase = phrase.replace(/diadakan/ig, "to be held");
phrase = phrase.replace(/keadaan/ig, "condition");
phrase = phrase.replace(/mengada-ngada/ig, "to boast");
phrase = phrase.replace(/mengada-ngadakan/ig, "to concoct an excuse");
phrase = phrase.replace(/mengadakan/ig, "to hold");
phrase = phrase.replace(/adab/ig, "politeness");
phrase = phrase.replace(/beradab/ig, "courteous");
phrase = phrase.replace(/peradaban/ig, "civilization");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

rnd me
12-15-2009, 07:04 AM
here a simpler way to do it for most major languages:

live demo (http://danml.com/pub/translate.htm)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>Dan's translate</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="dandavis" />

<style type='text/css'>
body {font-family: tahoma, helvetica, san-serif; color: menutext; background-color: menu; }
#dump { padding-right: 10em; border: 3px solid Black;
top: 3em; left: 1em; width: 80%; height: 88%;
position: absolute; overflow:auto; background-color: AliceBlue;
}
</style>
</head>
<body>
<label for='lang'> Language:
<select size='1' id='lang' name="lang" onchange="var si=this.selectedIndex; App.lc=this.options[si].value; ">
<option value="es">Spanish</option>
<option value="zh-CN">Chinese (Simplified)</option><option value="ru">Russian</option>
<option value="fr">French</option><option value="el">Greek</option><option value="sq">Albanian</option>
<option value="ar">Arabic</option><option value="bg">Bulgarian</option><option value="ca">Catalan</option>
<option value="zh-TW">Chinese (Traditional)</option><option value="hr">Croatian</option>
<option value="cs">Czech</option><option value="da">Danish</option><option value="nl">Dutch</option>
<option value="et">Estonian</option><option value="tl">Filipino</option><option value="fi">Finnish</option>
<option value="gl">Galician</option><option value="de">German</option><option value="iw">Hebrew</option>
<option value="hi">Hindi</option><option value="hu">Hungarian</option><option value="id">Indonesian</option>
<option value="it">Italian</option><option value="ja">Japanese</option><option value="ko">Korean</option>
<option value="lv">Latvian</option><option value="lt">Lithuanian</option><option value="mt">Maltese</option>
<option value="no">Norwegian</option><option value="pl">Polish</option><option value="pt">Portuguese</option>
<option value="ro">Romanian</option><option value="sr">Serbian</option><option value="sk">Slovak</option>
<option value="sl">Slovenian</option><option value="sv">Swedish</option><option value="th">Thai</option>
<option value="tr">Turkish</option><option value="uk">Ukrainian</option><option value="vi">Vietnamese</option>
</select>
</label>

&nbsp;
<input type="button" value="translate!" onclick="doTranslate()" />

<br />
<textarea id="dump">Hello World!</textarea>

<script type='text/javascript'>

var App={
lc: "es",
transUrl: "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&callback=incoming&q=",
enc: (escape||window.encodeURIComponent || window.escape)
};

function doTranslate(){ //do request
el("lang").onchange();
el("dump").style.backgroundColor="#666";
document.title="Translating...";
jsGet( App.transUrl + App.enc( el("dump").value ) + "&context=" + "dd" + "&langpair=en%7C"+App.lc );
}//end doTranslate()


function incoming(id, data, status){ //displays the translated text
el("dump").style.backgroundColor="";
document.title="Dan's translate";
if(status!=200){return;}
el("dump").value=data.translatedText||el("dump").value;
}//end incoming()

//support:
function el(tid) {return document.getElementById(tid);}
function jsGet(turl) {
var d=document, xJs = d.createElement("script");
d.getElementsByTagName("head")[0].appendChild(xJs); xJs.src = turl;
}//end jsGet()

</script>
</body>
</html>


tested ff3,ie8,c3

skyonline
12-15-2009, 07:15 AM
its use google right...i want to make it offline...because i got my own text...i only need jasvascripts

rnd me
12-15-2009, 08:02 AM
its use google right...i want to make it offline...because i got my own text...i only need jasvascripts


//data:
var reps={ "ambung":"back-basket", "amin":"amen", "amir":"emir", "ampai":"hang", "ampai-ampai":"jellyfish", "mengampaikan":"to hang clothes out to dry", "terampai":"hung to air", "ampaian":"clothes-line", "ampu":"ingratiate", "mengampu":"to ingratiate", "pengampu":"sycophant", "ampun":"pardon", "keampunan":"forgiveness", "mengampunkan":"to forgive", "pengampun":"one who forgives", "pengampunan":"amnesty", "amuk":"amuck", "mengamuk":"attack without thinking", "pengamuk":"person who runs amuck", "anai-anai":"termite", "anak":"child", "anak angkat":"adopted child", "anak bulan":"new moon", "anak dara":"virgin", "anak merah":"newborn", "anak-beranak":"together with the children", "anak-anak":"many children", "anak-anakan":"doll", "anakanda":"son", "beranak":"to bear a child", "keanak-anakan":"childish", "peranakan":"native", "anasir":"an element", "anasir jahat":"bad social element", "ancam":"menace", "mengancam":"to threaten", "ancaman":"a threat", "terancam":"threatened", "anda":"you", "andai":"if", "andainya":"suppose", "andaian":"assumption", "berandai":"to discuss", "mengandaikan":"suppose", "andam":"beautician who does the make-up for the bride and bridegroom", "mengandam":"to make-up the bride and bridegroom", "andas":"anvil", "anduh":"suspend by means of a sling", "aneh":"bizarre", "aneka":"multifarious", "aneka ragam":"variety show", "beraneka":"having a variety of", "anekarama":"concert", "angan":"imagination", "angan-angan":"imagination", "berangan-angan":"to day-dream", "mengangan-angankan":"to wish for", "terangan-angan":"to constantly wish for", "angau":"lovesick", "anggap":"assume", "menganggap":"to assume", "anggapan":"assumption", "beranggapan":"of the opinion", "anggar":"estimate", "anggaran":"estimation", "menganggarkan":"to estimate", "anggerik":"orchid", "anggota":"limb", "beranggota":"having limbs", "anggota badan":"part of the body", "keanggotaan":"membership", "menganggotai":"to be a member of", "angguk":"nod", "anggukan":"act of nodding", "mengangguk":"to nod", "terangguk-angguk":"to keep nodding", "anggur":"grape", "menganggur":"unemployed", "penganggur":"one who is unemployed", "pengangguran":"unemployment", "angin":"wind", "berangin":"windy", "mengangin":"to winnow", "menganginkan":"to put in a windy place", "peranginan":"holiday resort", "angka":"numerical figure", "perangkaan":"statistics", "angkara":"bad", "angkasa":"space", "angkasawan":"astronaut", "angkat":"lift", "angkatan":"troops", "berangkat":"to depart", "keberangkatan":"departure", "angkuh":"haughty", "keangkuhan":"arrogance", "angkup":"callipers", "angkut":"convey", "mengangkut":"to transport", "berangkut":"to transport several times", "pengangkutan":"transportation", "angsa":"goose", "aniaya":"maltreatment", "menganiayai":"to oppress", "penganiaya":"oppressor", "penganiayaan":"oppression", "anjak":"shift position slightly", "beranjak":"to shift position slightly", "menganjakkan":"to change the position of", "anjal":"elastic", "keanjalan":"flexibility", "menganjal":"to be elastic", "anjing":"dog", "anjung":"projecting facade", "anjungan":"bridge", "anjur":"to jut out", "menganjur":"to jut out", "anjuran":"proposal", "menganjurkan":"to propose", "penganjur":"organizer", "ansur":"progress", "beransur":"to progress in stages", "ansuran":"instalment", "mengansurkan":"to pay by instalments", "antah":"rice grains left unhusked after pounding", "antan":"pestle", "antara":"among", "mengantarai":"to mediate", "perantara":"mediator", "perantaraan":"mediation", "antarabangsa":"international", "antero":"whole", "seantero":"the whole", "anti":"opposed to", "anting":"pendulum", "abai":"neglect", "mengabaikan":"to disregard", "abang":"elder brother", "abang angkat":"foster brother", "abang ipar":"brother-in-law", "abang tiri":"stepbrother", "abdi":"slave", "mengabdikan":"to enslave", "pengabdian":"slavery", "abjad":"alphabet", "abu":"ash", "mengabui":"to cheat", "abuk":"dust", "abung":"pomelo", "acah":"tease", "mengacah-acah":"to tease", "acapkali":"often", "acar":"pickle", "acara":"event", "mengacarakan":"to compere", "pengacara":"master of ceremonies", "aci":"shaft", "acu":"to direct weapon at target", "mengacukan":"to aim weapon at target", "mengacu":"to test", "acuan":"a mould", "acuh":"to care for", "mengacuhkan":"to care for", "acuh tak acuh":"to show indifference", "acum":"provoke", "mengacum":"to provoke", "acuman":"provocation", "pengacum":"instigator", "pengacuman":"agitation", "ada":"have", "adakala":"sometimes", "adakalanya":"sometimes", "adalah":"is", "adapun":"with regard to", "berada":"rich", "diadakan":"to be held", "keadaan":"condition", "mengada-ngada":"to boast", "mengada-ngadakan":"to concoct an excuse", "mengadakan":"to hold", "adab":"politeness", "beradab":"courteous", "peradaban":"civilization"};

//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (phrase.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("\\b"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"\\b", "ig"),reps[i]);
}}
return buff;
}


//test:
var phrase="Say amin peradaban !"
alert(repAll(phrase, reps));//shows: "Say amen civilization !"

skyonline
12-15-2009, 08:09 AM
//data:
var reps={ "ambung":"back-basket", "amin":"amen", "amir":"emir", "ampai":"hang", "ampai-ampai":"jellyfish", "mengampaikan":"to hang clothes out to dry", "terampai":"hung to air", "ampaian":"clothes-line", "ampu":"ingratiate", "mengampu":"to ingratiate", "pengampu":"sycophant", "ampun":"pardon", "keampunan":"forgiveness", "mengampunkan":"to forgive", "pengampun":"one who forgives", "pengampunan":"amnesty", "amuk":"amuck", "mengamuk":"attack without thinking", "pengamuk":"person who runs amuck", "anai-anai":"termite", "anak":"child", "anak angkat":"adopted child", "anak bulan":"new moon", "anak dara":"virgin", "anak merah":"newborn", "anak-beranak":"together with the children", "anak-anak":"many children", "anak-anakan":"doll", "anakanda":"son", "beranak":"to bear a child", "keanak-anakan":"childish", "peranakan":"native", "anasir":"an element", "anasir jahat":"bad social element", "ancam":"menace", "mengancam":"to threaten", "ancaman":"a threat", "terancam":"threatened", "anda":"you", "andai":"if", "andainya":"suppose", "andaian":"assumption", "berandai":"to discuss", "mengandaikan":"suppose", "andam":"beautician who does the make-up for the bride and bridegroom", "mengandam":"to make-up the bride and bridegroom", "andas":"anvil", "anduh":"suspend by means of a sling", "aneh":"bizarre", "aneka":"multifarious", "aneka ragam":"variety show", "beraneka":"having a variety of", "anekarama":"concert", "angan":"imagination", "angan-angan":"imagination", "berangan-angan":"to day-dream", "mengangan-angankan":"to wish for", "terangan-angan":"to constantly wish for", "angau":"lovesick", "anggap":"assume", "menganggap":"to assume", "anggapan":"assumption", "beranggapan":"of the opinion", "anggar":"estimate", "anggaran":"estimation", "menganggarkan":"to estimate", "anggerik":"orchid", "anggota":"limb", "beranggota":"having limbs", "anggota badan":"part of the body", "keanggotaan":"membership", "menganggotai":"to be a member of", "angguk":"nod", "anggukan":"act of nodding", "mengangguk":"to nod", "terangguk-angguk":"to keep nodding", "anggur":"grape", "menganggur":"unemployed", "penganggur":"one who is unemployed", "pengangguran":"unemployment", "angin":"wind", "berangin":"windy", "mengangin":"to winnow", "menganginkan":"to put in a windy place", "peranginan":"holiday resort", "angka":"numerical figure", "perangkaan":"statistics", "angkara":"bad", "angkasa":"space", "angkasawan":"astronaut", "angkat":"lift", "angkatan":"troops", "berangkat":"to depart", "keberangkatan":"departure", "angkuh":"haughty", "keangkuhan":"arrogance", "angkup":"callipers", "angkut":"convey", "mengangkut":"to transport", "berangkut":"to transport several times", "pengangkutan":"transportation", "angsa":"goose", "aniaya":"maltreatment", "menganiayai":"to oppress", "penganiaya":"oppressor", "penganiayaan":"oppression", "anjak":"shift position slightly", "beranjak":"to shift position slightly", "menganjakkan":"to change the position of", "anjal":"elastic", "keanjalan":"flexibility", "menganjal":"to be elastic", "anjing":"dog", "anjung":"projecting facade", "anjungan":"bridge", "anjur":"to jut out", "menganjur":"to jut out", "anjuran":"proposal", "menganjurkan":"to propose", "penganjur":"organizer", "ansur":"progress", "beransur":"to progress in stages", "ansuran":"instalment", "mengansurkan":"to pay by instalments", "antah":"rice grains left unhusked after pounding", "antan":"pestle", "antara":"among", "mengantarai":"to mediate", "perantara":"mediator", "perantaraan":"mediation", "antarabangsa":"international", "antero":"whole", "seantero":"the whole", "anti":"opposed to", "anting":"pendulum", "abai":"neglect", "mengabaikan":"to disregard", "abang":"elder brother", "abang angkat":"foster brother", "abang ipar":"brother-in-law", "abang tiri":"stepbrother", "abdi":"slave", "mengabdikan":"to enslave", "pengabdian":"slavery", "abjad":"alphabet", "abu":"ash", "mengabui":"to cheat", "abuk":"dust", "abung":"pomelo", "acah":"tease", "mengacah-acah":"to tease", "acapkali":"often", "acar":"pickle", "acara":"event", "mengacarakan":"to compere", "pengacara":"master of ceremonies", "aci":"shaft", "acu":"to direct weapon at target", "mengacukan":"to aim weapon at target", "mengacu":"to test", "acuan":"a mould", "acuh":"to care for", "mengacuhkan":"to care for", "acuh tak acuh":"to show indifference", "acum":"provoke", "mengacum":"to provoke", "acuman":"provocation", "pengacum":"instigator", "pengacuman":"agitation", "ada":"have", "adakala":"sometimes", "adakalanya":"sometimes", "adalah":"is", "adapun":"with regard to", "berada":"rich", "diadakan":"to be held", "keadaan":"condition", "mengada-ngada":"to boast", "mengada-ngadakan":"to concoct an excuse", "mengadakan":"to hold", "adab":"politeness", "beradab":"courteous", "peradaban":"civilization"};

//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (phrase.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("\\b"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"\\b", "ig"),reps[i]);
}}
return buff;
}


//test:
var phrase="Say amin peradaban !"
alert(repAll(phrase, reps));//shows: "Say amen civilization !"


how to use it..??...i test on frontpage..but is not working...can u explain how to use..

Philip M
12-15-2009, 08:15 AM
Your problem is nothing to do with how many words, but rather when one word is embedded within another (who put the age in manager? :D). The greater the number of words in your dictionary the greater the chance that this will happen.

phrase = phrase.replace(/ada/ig, "have");
peradaban...the result is perhaveban

To cause your regex to operate only on whole words you need to add the word boundary \b to each word:-

phrase = phrase.replace(/\bada\b/ig, "have");


In last month's article about beer and the many breweries in Belgium, 'rapist monks' should have read 'Trappist monks'. - Woking Review

skyonline
12-15-2009, 08:53 AM
wow!!! its work..TQ to all and Philip M.....and i have one more question....how to make in input text and result text area on this script have copy,paste,salect all and clear text button (function)..??

this is the scripts :


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

Philip M
12-15-2009, 09:17 AM
wow!!! its work..TQ to all and Philip M.....and i have one more question....how to make in result text area on this script have copy,paste,salect all and clear text button



<script type="text/javascript">
function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){
document.myForm.txtArea.focus();
PastedText = document.myForm.txtArea.createTextRange();
PastedText.execCommand("Paste");
}
</script>

<form name="myForm">
Select this text, copy it using the copy button, and paste it below.<br /><br />
<textarea id="txtArea" cols="60" rows="5">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. </textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" />
<input type="button" value="Highlight Text" onClick="javascript:this.form.txtArea.focus();this.form.txtArea.select();">
<input type="button" value = "Clear The Text" onclick = "document.getElementById('txtArea').value = '' ">

</form>



But this copy/paste code will only work in IE.

Note that there is a security issue with data stored on the clipboard, and many people choose to disable
"Allow paste operations via script" to prevent access from the internet.

rnd me
12-15-2009, 09:33 AM
how to use it..??...i test on frontpage..but is not working...can u explain how to use..

sorry about that; i had a nice explanation all written and a "databaseError" discarded my whole post with tested code.

i recovered most of the code and dumped it, as i was in a hurry.
i missed a type-o trying to hurry...

basically, i said you should break up your app into chunks; separating the content from the program code.
this makes it slimmer, and lets you optimize the code in a single place.
i gave it a big performance boost by running String.indexOf to check to see if the slow replace process and regexp construction should even be bothered with.

since indexOf is at least 20X than a pre-built regexp, this will be faster unless the text contains 950+ of the 1000 terms (back of napkin).


here is everything in a simple html wrap to demo it (tested ff3.5):


<html><script>


//data:
var reps={ "ambung":"back-basket", "amin":"amen", "amir":"emir", "ampai":"hang", "ampai-ampai":"jellyfish", "mengampaikan":"to hang clothes out to dry", "terampai":"hung to air", "ampaian":"clothes-line", "ampu":"ingratiate", "mengampu":"to ingratiate", "pengampu":"sycophant", "ampun":"pardon", "keampunan":"forgiveness", "mengampunkan":"to forgive", "pengampun":"one who forgives", "pengampunan":"amnesty", "amuk":"amuck", "mengamuk":"attack without thinking", "pengamuk":"person who runs amuck", "anai-anai":"termite", "anak":"child", "anak angkat":"adopted child", "anak bulan":"new moon", "anak dara":"virgin", "anak merah":"newborn", "anak-beranak":"together with the children", "anak-anak":"many children", "anak-anakan":"doll", "anakanda":"son", "beranak":"to bear a child", "keanak-anakan":"childish", "peranakan":"native", "anasir":"an element", "anasir jahat":"bad social element", "ancam":"menace", "mengancam":"to threaten", "ancaman":"a threat", "terancam":"threatened", "anda":"you", "andai":"if", "andainya":"suppose", "andaian":"assumption", "berandai":"to discuss", "mengandaikan":"suppose", "andam":"beautician who does the make-up for the bride and bridegroom", "mengandam":"to make-up the bride and bridegroom", "andas":"anvil", "anduh":"suspend by means of a sling", "aneh":"bizarre", "aneka":"multifarious", "aneka ragam":"variety show", "beraneka":"having a variety of", "anekarama":"concert", "angan":"imagination", "angan-angan":"imagination", "berangan-angan":"to day-dream", "mengangan-angankan":"to wish for", "terangan-angan":"to constantly wish for", "angau":"lovesick", "anggap":"assume", "menganggap":"to assume", "anggapan":"assumption", "beranggapan":"of the opinion", "anggar":"estimate", "anggaran":"estimation", "menganggarkan":"to estimate", "anggerik":"orchid", "anggota":"limb", "beranggota":"having limbs", "anggota badan":"part of the body", "keanggotaan":"membership", "menganggotai":"to be a member of", "angguk":"nod", "anggukan":"act of nodding", "mengangguk":"to nod", "terangguk-angguk":"to keep nodding", "anggur":"grape", "menganggur":"unemployed", "penganggur":"one who is unemployed", "pengangguran":"unemployment", "angin":"wind", "berangin":"windy", "mengangin":"to winnow", "menganginkan":"to put in a windy place", "peranginan":"holiday resort", "angka":"numerical figure", "perangkaan":"statistics", "angkara":"bad", "angkasa":"space", "angkasawan":"astronaut", "angkat":"lift", "angkatan":"troops", "berangkat":"to depart", "keberangkatan":"departure", "angkuh":"haughty", "keangkuhan":"arrogance", "angkup":"callipers", "angkut":"convey", "mengangkut":"to transport", "berangkut":"to transport several times", "pengangkutan":"transportation", "angsa":"goose", "aniaya":"maltreatment", "menganiayai":"to oppress", "penganiaya":"oppressor", "penganiayaan":"oppression", "anjak":"shift position slightly", "beranjak":"to shift position slightly", "menganjakkan":"to change the position of", "anjal":"elastic", "keanjalan":"flexibility", "menganjal":"to be elastic", "anjing":"dog", "anjung":"projecting facade", "anjungan":"bridge", "anjur":"to jut out", "menganjur":"to jut out", "anjuran":"proposal", "menganjurkan":"to propose", "penganjur":"organizer", "ansur":"progress", "beransur":"to progress in stages", "ansuran":"instalment", "mengansurkan":"to pay by instalments", "antah":"rice grains left unhusked after pounding", "antan":"pestle", "antara":"among", "mengantarai":"to mediate", "perantara":"mediator", "perantaraan":"mediation", "antarabangsa":"international", "antero":"whole", "seantero":"the whole", "anti":"opposed to", "anting":"pendulum", "abai":"neglect", "mengabaikan":"to disregard", "abang":"elder brother", "abang angkat":"foster brother", "abang ipar":"brother-in-law", "abang tiri":"stepbrother", "abdi":"slave", "mengabdikan":"to enslave", "pengabdian":"slavery", "abjad":"alphabet", "abu":"ash", "mengabui":"to cheat", "abuk":"dust", "abung":"pomelo", "acah":"tease", "mengacah-acah":"to tease", "acapkali":"often", "acar":"pickle", "acara":"event", "mengacarakan":"to compere", "pengacara":"master of ceremonies", "aci":"shaft", "acu":"to direct weapon at target", "mengacukan":"to aim weapon at target", "mengacu":"to test", "acuan":"a mould", "acuh":"to care for", "mengacuhkan":"to care for", "acuh tak acuh":"to show indifference", "acum":"provoke", "mengacum":"to provoke", "acuman":"provocation", "pengacum":"instigator", "pengacuman":"agitation", "ada":"have", "adakala":"sometimes", "adakalanya":"sometimes", "adalah":"is", "adapun":"with regard to", "berada":"rich", "diadakan":"to be held", "keadaan":"condition", "mengada-ngada":"to boast", "mengada-ngadakan":"to concoct an excuse", "mengadakan":"to hold", "adab":"politeness", "beradab":"courteous", "peradaban":"civilization"};

//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("\\b"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"\\b", "ig"),reps[i]);
}}
return buff;
}


function translate(){
document.getElementById('output').value = repAll(document.getElementById('input').value, reps);
}
</script>

<textarea id="input">"Say amin peradaban !"</textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>
</html>

skyonline
12-15-2009, 09:45 AM
<script type="text/javascript">
function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){
document.myForm.txtArea.focus();
PastedText = document.myForm.txtArea.createTextRange();
PastedText.execCommand("Paste");
}
</script>

<form name="myForm">
Select this text, copy it using the copy button, and paste it below.<br /><br />
<textarea id="txtArea" cols="60" rows="5">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. </textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" />
<input type="button" value="Highlight Text" onClick="javascript:this.form.txtArea.focus();this.form.txtArea.select();">
<input type="button" value = "Clear The Text" onclick = "document.getElementById('txtArea').value = '' ">

</form>



But this copy/paste code will only work in IE.

but..How to combine with this javascripts (Translator):


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

Trinithis
12-15-2009, 09:51 AM
Whoa! Lots of replies since I coded my solution:

translator.js


function Translator () {
this.rules = [];
}

(function () {
var dollar = /(\$+)/g;

function Rule (regex, replacement) {
if (typeof replacement === "function") {
var oldReplacement = replacement;
replacement = function (execResult) {
for (var i = 0; i < execResult.length; ++i) {
execResult [i] = execResult [i].replace (dollar, "$1$1");
}
return oldReplacement (execResult);
};
}
this.regex = regex;
this.replacement = replacement;
}

Translator.prototype = {
constructor: Translator

, addRule: function (regex, replacement, keepStartCase) {
if (!regex.global) {
throw new TypeError ("Translator.prototype.addTranslation: expects " + regex + " with global property set.");
}
if (keepStartCase) {
var oldReplacement = replacement;
replacement = function (execResult) {
var str = typeof oldReplacement === "function"
? oldReplacement (execResult)
: oldReplacement
;
var chr = execResult [0].charAt (0);
if (!execResult [0].length) {
return str;
}
var head = str.substr (0, 1) [chr == chr.toUpperCase () ? "toUpperCase" : "toLowerCase"] ();
var tail = str.substr (1);
return head + tail;
};
}
var rule = new Rule (regex, replacement);
this.rules.push (rule);
}

, translate: function (str) {
var translation = [];
var pos = 0;
while (pos < str.length) {
var bestExec = null;
var bestRule = null;
for (var i = 0; i < this.rules.length; ++i) {
var rule = this.rules [i];
rule.regex.lastIndex = pos;
var exec = rule.regex.exec (str);
if (exec) {
var doUpdate = !bestExec
|| (exec.index < bestExec.index)
|| (exec.index == bestExec.index && exec [0].length > bestExec [0].length)
;
if (doUpdate) {
bestExec = exec;
bestRule = rule;
}
}
}
if (!bestExec) {
return translation.join ("") + str.substr (pos);
}
else {
translation.push (str.substring (pos, bestExec.index));
pos = bestRule.regex.lastIndex;
var replacement = typeof bestRule.replacement === "function"
? bestRule.replacement (bestExec)
: bestRule.replacement
;
translation.push (bestExec [0].replace (bestRule.regex, replacement));
}
}
return translation.join ("");
}
};
}) ();


test.html


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
</head>
<body>
<div>
<script type="text/javascript" src="translator.js"></script>
<script type="text/javascript">

var t = new Translator ();

t.addRule (/ham/gi, "beef");
t.addRule (/prime/gi, "SHOULDN'T MATCH IN EXAMPLE");
t.addRule (/hamburgers/gi, "prime ribs");
t.addRule (/ribs/gi, "SHOULDN'T MATCH IN EXAMPLE");
t.addRule (/(')/g, "$1$1");
t.addRule (/(b)(oss)/g, function (execResult) {
return execResult [1].toUpperCase () + execResult [2];
});
t.addRule (/the/gi, "the", true);
t.addRule (/(\$|@)/g, function (execResult) {
return "(" + execResult [0] + "1$1)";
});

document.writeln (t.translate ("'The hamburgers are $20,' Jordan said to the boss @ outside."));

</script>
</div>
</body>
</html>

Philip M
12-15-2009, 10:12 AM
but..How to combine with this javascripts (Translator):


No idea what you mean. Simply place the script I gave you on the page. The only difference is that your textrea is named output and I have named it txtArea in my script.

Trinithis and rnd me - skyonline seems to be a beginner. The art of successful teaching is to tailor the advice to the knowledge/skill level of the student. Sophisticated graduate-level solutions are not very helpful to people struggling with basic concepts - especially if they contain errors.

rnd me
12-15-2009, 10:24 AM
tisk!


"Jordan said to the boss @ ou".replace(/(b)(oss)/g, function (execResult) {
return execResult [1].toUpperCase () + execResult [2];
}); //==="Jordan said to the Os @ ou"

is that right? it's an error in IE, but hey that's only 2/3rds of the market (sadly)... ;)
i've never seen anything but virtual perfection from Trinithis, so i feel lucky.


that the first arg to a replace function is usually junk: the whole match (a string).
i usually name my params (junk, a, b) as a result...

Philip M
12-15-2009, 10:27 AM
One more time -

Sophisticated graduate-level solutions are not very helpful to people struggling with basic concepts - especially if they contain errors.

How about giving skyonline a break, huh?

skyonline
12-15-2009, 11:07 AM
http://img697.imageshack.us/img697/4394/1215200965340pm.gif

This is the code on the picture :


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>

Philip M
12-15-2009, 01:02 PM
I am not sure exactly what you want, but here are the various scripts combined:-


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}

function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){
document.myForm.output.focus();
PastedText = document.myForm.output.createTextRange();
PastedText.execCommand("Paste");
}
</script>

<form name="myForm">

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" />
<input type="button" value="Highlight Text" onClick="javascript:this.form.output.focus();this.form.output.select();">
<input type="button" value = "Clear The Text" onclick = "document.getElementById('output').value = '' ">

</form>

skyonline
12-15-2009, 01:18 PM
tq Philip M for to make botton for "output" text area.....how about (input text area) <textarea id="input"></textarea><br />....can it put copy,paste,select all and clear botton ...

Philip M
12-15-2009, 03:03 PM
tq Philip M for to make botton for "output" text area.....how about (input text area) <textarea id="input"></textarea><br />....can it put copy,paste,select all and clear botton ...

Yes, and you ought to be able to do this yourself very easily using the scripts I have given you.

skyonline
12-15-2009, 03:30 PM
ya...i have make it...but got erro on "Copy to clipboard" button..can u cek..


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}

function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){
document.myForm.output.focus();
PastedText = document.myForm.output.createTextRange();
PastedText.execCommand("Paste");
}

function PasteFromClipboard(){
document.myForm.input.focus();
PastedText = document.myForm.input.createTextRange();
PastedText.execCommand("Paste");
}

</script>
&nbsp;<form name="myForm">

<textarea id="input"></textarea><p>
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" /><input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" /><input type="button" value="Highlight Text" onClick="javascript:this.form.input.focus();this.form.input.select();"><input type="button" value = "Clear The Text" onclick = "document.getElementById('input').value = '' "></p>
<p><br />
<input type="button" value="translate" onClick="translate();" /></p>
<p><br />
<textarea id="output"></textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" />
<input type="button" value="Highlight Text" onClick="javascript:this.form.output.focus();this.form.output.select();">
<input type="button" value = "Clear The Text" onclick = "document.getElementById('output').value = '' ">

</p>

</form>

<br />
&nbsp;&nbsp;&nbsp;

skyonline
12-15-2009, 03:33 PM
ya...i have make it...but got erro on "Copy to clipboard" button..can u cek..


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}

function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

function PasteFromClipboard(){
document.myForm.output.focus();
PastedText = document.myForm.output.createTextRange();
PastedText.execCommand("Paste");
}

function PasteFromClipboard(){
document.myForm.input.focus();
PastedText = document.myForm.input.createTextRange();
PastedText.execCommand("Paste");
}

</script>
&nbsp;<form name="myForm">

<textarea id="input"></textarea><p>
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" /><input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" /><input type="button" value="Highlight Text" onClick="javascript:this.form.input.focus();this.form.input.select();"><input type="button" value = "Clear The Text" onclick = "document.getElementById('input').value = '' "></p>
<p><br />
<input type="button" value="translate" onClick="translate();" /></p>
<p><br />
<textarea id="output"></textarea>
<br />
<input type="button" onClick="CopyToClipboard()" value="Copy to clipboard" />
<input type="button" onClick="PasteFromClipboard()" value="Paste from clipboard" />
<input type="button" value="Highlight Text" onClick="javascript:this.form.output.focus();this.form.output.select();">
<input type="button" value = "Clear The Text" onclick = "document.getElementById('output').value = '' ">

</p>

</form>

<br />
&nbsp;&nbsp;&nbsp;

Philip M
12-15-2009, 04:17 PM
You can not have two functions with the same name.
function PasteFromClipboard(){

Rename them to:-

function PasteFromClipboardOut(){
function PasteFromClipboardIn(){

What error message do you get? I explained to you that this code only works in IE.
As far as I can see it works fine (copies highlighted text to clipboard and pastes back again).

I notice that the function CopyToClipboard() fails to work for some reason when called for the first time.

I changed it to:-


function CopyToClipboard() {
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
CopiedTxt = document.selection.createRange();
CopiedTxt.execCommand("Copy");
}

and it now seems to work correctly.

skyonline
12-15-2009, 07:19 PM
function CopyToClipboard() is still can't work correctly...all scripts i have test in on IE (frontpage)

rnd me
12-15-2009, 07:22 PM
function CopyToClipboard() is still can't work correctly...all scripts i have test in on IE (frontpage)



function CopyToClipboard() {
window.clipboardData.setData('Text', document.getElementById('output').value);
}//end CB

Philip M
12-15-2009, 07:35 PM
function CopyToClipboard() {
window.clipboardData.setData('Text', document.getElementById('output').value = phrase;);
}//end CB


Sorry rnd me. That does not work for me. "Invalid argument".

skyonline - all I can say is that it works for me.

rnd me
12-15-2009, 07:45 PM
Sorry rnd me. That does not work for me. "Invalid argument".

skyonline - all I can say is that it works for me.

yeah, semi inside of parens make javascript sad...

fixed:

function CopyToClipboard() {
window.clipboardData.setData('Text', document.getElementById('output').value);
}//end CB

Philip M
12-15-2009, 07:55 PM
Ah, that's better. But the OP will need two functions:-


function CopyToClipboardOut() {
window.clipboardData.setData('Text', document.getElementById('output').value);
}

function CopyToClipboardIn() {
window.clipboardData.setData('Text', document.getElementById('input').value);
}

But this copies the whole of the textarea to the clipboard, not just the selected range.

Trinithis
12-15-2009, 08:42 PM
tisk!


"Jordan said to the boss @ ou".replace(/(b)(oss)/g, function (execResult) {
return execResult [1].toUpperCase () + execResult [2];
}); //==="Jordan said to the Os @ ou"

is that right? it's an error in IE, but hey that's only 2/3rds of the market (sadly)... ;)
i've never seen anything but virtual perfection from Trinithis, so i feel lucky.


that the first arg to a replace function is usually junk: the whole match (a string).
i usually name my params (junk, a, b) as a result...

I don't really get what that has to do with my code. I don't directly use the replace method with a function argument.

Aha! Just looked up replace (https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace) and up until now I always thought the function argument (if any) only took one argument, the matched result. I ought to change my code to act more like a normal replace function.

... and here we are:

translator.js


function Translator () {
this.rules = [];
}

(function () {
var dollar = /(\$+)/g;

function Rule (regex, replacement) {
if (typeof replacement === "function") {
var oldReplacement = replacement;
replacement = function () {
return oldReplacement.apply (null, arguments).replace (dollar, "$1$1");
};
}
this.regex = regex;
this.replacement = replacement;
}

Translator.prototype = {
constructor: Translator

, addRule: function (regex, replacement, keepStartCase) {
if (!regex.global) {
throw new TypeError ("Translator.prototype.addTranslation: expects " + regex + " with global property set.");
}
if (keepStartCase) {
var oldReplacement = replacement;
replacement = function (match) {
var rstr = typeof oldReplacement === "function"
? oldReplacement.apply (null, arguments)
: oldReplacement
;
var chr = match.charAt (0);
if (!match.length) {
return rstr;
}
var head = rstr.substr (0, 1) [chr == chr.toUpperCase () ? "toUpperCase" : "toLowerCase"] ();
var tail = rstr.substr (1);
return head + tail;
};
}
var rule = new Rule (regex, replacement);
this.rules.push (rule);
}

, translate: function (str) {
var translation = [];
var pos = 0;
while (pos < str.length) {
var bestExec = null;
var bestRule = null;
for (var i = 0; i < this.rules.length; ++i) {
var rule = this.rules ;
rule.regex.lastIndex = pos;
var exec = rule.regex.exec (str);
if (exec) {
var doUpdate = !bestExec
|| (exec.index < bestExec.index)
|| (exec.index == bestExec.index && exec [0].length > bestExec [0].length)
;
if (doUpdate) {
bestExec = exec;
bestRule = rule;
}
}
}
if (!bestExec) {
return translation.join ("") + str.substr (pos);
}
else {
translation.push (str.substring (pos, bestExec.index));
pos = bestRule.regex.lastIndex;
var replacement = typeof bestRule.replacement === "function"
? bestRule.replacement.apply (null, bestExec.concat (bestExec.index, bestExec.input))
: bestRule.replacement
;
translation.push (bestExec [0].replace (bestRule.regex, replacement));
}
}
return translation.join ("");
}
};
}) ();


test.html


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
</head>
<body>
<div>
<script type="text/javascript" src="translator.js"></script>
<script type="text/javascript">

var t = new Translator ();
t.addRule (/ham/gi, "beef");
t.addRule (/prime/gi, "SHOULDN'T MATCH IN EXAMPLE");
t.addRule (/hamburgers/gi, "prime ribs");
t.addRule (/ribs/gi, "SHOULDN'T MATCH IN EXAMPLE");
t.addRule (/(')/g, "$1$1");
t.addRule (/(b)(oss)/g, function (match, capture1, capture2) {
return capture1.toUpperCase () + capture2;
});
t.addRule (/those|SOME/gi, "the", true); // do replacement but keep initial case
t.addRule (/(\$)/g, function (match, capture1) {
return "(" + match + "1" + capture1 + ")";
});

var str = "'Those hamburgers are $20,' Jordan said to some boss outside.";

document.write (str);
document.write ("<br />");
document.write (t.translate (str));

</script>
</div>
</body>
</html>

----------------------------------

How to use:



// Could be any variable... I just chose trans.
var trans = new Translator ();

// * regex: Any regex that has the g flag set.
// * replacement: Either a string or a function.
// Behaves exactly like the replacement argument in String.prototype.replace (https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace).
// Note you can specify a function as the argument even in browsers that do not
// normally support them for replacement.
// * keepStartCase: Optional argument.
// If suppied and is true then the replaced value's first [I]character will coincide
// with the case of the first character of the matched string.
// If either first character is a character that is case independent then this does
// nothing.
trans.addRule (regex, replacement, keepStartCase);

trans.addRule (...);
...
trans.addRule (...);

var translation = trans.translate (str);


----------------------------------

Example using OP's desired code:


but..How to combine with this javascripts (Translator):


<script type="text/javascript">
function translate(phrase) {
phrase = document.getElementById('input').value;
phrase = phrase.replace(/\babadi\b/ig, "eternal");
phrase = phrase.replace(/\bkeabadian\b/ig, "immortality");
phrase = phrase.replace(/\bmengabadikan\b/ig, "perpetuate");
phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");
document.getElementById('output').value = phrase;
}
</script>

<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>



would become:


<script type="text/javascript" src="translator.js"></script>
<script type="text/javascript">
function translate () {
var phrase = document.getElementById ('input').value;
var t = new Translator ();
t.addRule (/\babadi\b/ig, "eternal");
t.addRule (/\bkeabadian\b/ig, "immortality");
t.addRule (/\bmengabadikan\b/ig, "perpetuate");
t.addRule (/\babah\b/ig, "father");
t.addRule (/\babah-abah\b/ig, "harness");
document.getElementById ('output').value = t.translate ();
}
</script>
<textarea id="input"></textarea><br />
<input type="button" value="translate" onClick="translate();" /><br />
<textarea id="output"></textarea>


That being said, if Philip M's code works for you, go for it.

rnd me
12-15-2009, 09:27 PM
One more time -

Sophisticated graduate-level solutions are not very helpful to people struggling with basic concepts - especially if they contain errors.

How about giving skyonline a break, huh?

no, give me one.

dismissive and broad cut-and-pasted "you can't do that" replies aren't very helpful either, even if the sucker buys into the denial enough to click "thanks"...

Trinithis nuggets of wisdom/experience are a much bigger rat pellet than a "thanks" click for folks like me, and picking up new "400" level techniques/patterns is the most I can get from posting here.
It's certainly more interesting than explaining to a noob why they are getting "Access Denied" errors for the 1000th time...

plus, who cares about the OP? (no offense sky)
this thread currently has 322 views, and archived threads have many more. I post more for the sake of those who google, than any one specific noob.

besides, i built a great working solution, as did others, so i think there's more than enough info posted for a self-starter to get this thing up and running.

So are we using the OP as an excuse for barely-related technical discussion? perhaps, but i'm not apologetic about it...

skyonline
12-16-2009, 12:12 AM
ok ist work...thaks to all..i really no i only copy and paste this scripts (i dont make it) why..?..i really got no idea about javascripts (only 5% understand)...and really not interesting about javascripts..i make this becuse i got free database dictionary and i got a idea to make translator in my site..and this is not for sale...and my site is free only use free domain and free hosting..i only want to help my comunity (lern english)..it also help me to improve my english...becuse evry singal word i edit and lern...sory for any prop....

skyonline
12-16-2009, 06:03 AM
i got one more question...

phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");

words that i bold is same words...

abah is = father

abah-abah is = harness

but when i tyep words abah-abah in input text area the anwser in output text area is father-father...the tru anwser is :

abah-abah = harness

how to fix it...?

Trinithis
12-16-2009, 06:36 AM
Even if you don't understand how my translator (http://www.codingforums.com/showpost.php?p=899884&postcount=29) works, I think it is simple enough to use. Look at the examples I give. It does not have the "abah-abah" -> "father-father" issue.

rnd me
12-16-2009, 06:57 AM
i got one more question...

phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");

words that i bold is same words...

abah is = father

abah-abah is = harness

but when i tyep words abah-abah in input text area the anwser in output text area is father-father...the tru anwser is :

abah-abah = harness

how to fix it...?

sorry you haven't gotten what you need.
you have been given some high value advice, but i know it can difficult to piece together chunks i didn't write...

i fixed all the issues mentioned so far (i think).

thanks for shopping at coding forums.

functionality tested in FF3,C3, IE8.
clipboard tested in IE8.




<html><script>


//data:
var reps={
"abah":"father",
"abah-abah":"harness",
"abai":"neglect",
"abang angkat":"foster brother",
"abang ipar":"brother-in-law",
"abang tiri":"stepbrother",
"abang":"elder brother",
"abdi":"slave",
"abjad":"alphabet",
"abu":"ash",
"abuk":"dust",
"abung":"pomelo",
"acah":"tease",
"acapkali":"often",
"acar":"pickle",
"acara":"event",
"aci":"shaft",
"acu":"to direct weapon at target",
"acuan":"a mould",
"acuh tak acuh":"to show indifference",
"acuh":"to care for",
"acum":"provoke",
"acuman":"provocation",
"ada":"have",
"adab":"politeness",
"adakala":"sometimes",
"adakalanya":"sometimes",
"adalah":"is",
"adapun":"with regard to",
"ambung":"back-basket",
"amin":"amen",
"amir":"emir",
"ampai":"hang",
"ampai-ampai":"jellyfish",
"ampaian":"clothes-line",
"ampu":"ingratiate",
"ampun":"pardon",
"amuk":"amuck",
"anai-anai":"termite",
"anak angkat":"adopted child",
"anak bulan":"new moon",
"anak dara":"virgin",
"anak merah":"newborn",
"anak":"child",
"anak-anak":"many children",
"anak-anakan":"doll",
"anak-beranak":"together with the children",
"anakanda":"son",
"anasir jahat":"bad social element",
"anasir":"an element",
"ancam":"menace",
"ancaman":"a threat",
"anda":"you",
"andai":"if",
"andaian":"assumption",
"andainya":"suppose",
"andam":"beautician who does the make-up for the bride and bridegroom",
"andas":"anvil",
"anduh":"suspend by means of a sling",
"aneh":"bizarre",
"aneka ragam":"variety show",
"aneka":"multifarious",
"anekarama":"concert",
"angan":"imagination",
"angan-angan":"imagination",
"angau":"lovesick",
"anggap":"assume",
"anggapan":"assumption",
"anggar":"estimate",
"anggaran":"estimation",
"anggerik":"orchid",
"anggota badan":"part of the body",
"anggota":"limb",
"angguk":"nod",
"anggukan":"act of nodding",
"anggur":"grape",
"angin":"wind",
"angka":"numerical figure",
"angkara":"bad",
"angkasa":"space",
"angkasawan":"astronaut",
"angkat":"lift",
"angkatan":"troops",
"angkuh":"haughty",
"angkup":"callipers",
"angkut":"convey",
"angsa":"goose",
"aniaya":"maltreatment",
"anjak":"shift position slightly",
"anjal":"elastic",
"anjing":"dog",
"anjung":"projecting facade",
"anjungan":"bridge",
"anjur":"to jut out",
"anjuran":"proposal",
"ansur":"progress",
"ansuran":"instalment",
"antah":"rice grains left unhusked after pounding",
"antan":"pestle",
"antara":"among",
"antarabangsa":"international",
"antero":"whole",
"anti":"opposed to",
"anting":"pendulum",
"berada":"rich",
"beradab":"courteous",
"beranak":"to bear a child",
"berandai":"to discuss",
"beraneka":"having a variety of",
"berangan-angan":"to day-dream",
"beranggapan":"of the opinion",
"beranggota":"having limbs",
"berangin":"windy",
"berangkat":"to depart",
"berangkut":"to transport several times",
"beranjak":"to shift position slightly",
"beransur":"to progress in stages",
"diadakan":"to be held",
"keadaan":"condition",
"keampunan":"forgiveness",
"keanak-anakan":"childish",
"keanggotaan":"membership",
"keangkuhan":"arrogance",
"keanjalan":"flexibility",
"keberangkatan":"departure",
"mengabaikan":"to disregard",
"mengabdikan":"to enslave",
"mengabui":"to cheat",
"mengacah-acah":"to tease",
"mengacarakan":"to compere",
"mengacu":"to test",
"mengacuhkan":"to care for",
"mengacukan":"to aim weapon at target",
"mengacum":"to provoke",
"mengada-ngada":"to boast",
"mengada-ngadakan":"to concoct an excuse",
"mengadakan":"to hold",
"mengampaikan":"to hang clothes out to dry",
"mengampu":"to ingratiate",
"mengampunkan":"to forgive",
"mengamuk":"attack without thinking",
"mengancam":"to threaten",
"mengandaikan":"suppose",
"mengandam":"to make-up the bride and bridegroom",
"mengangan-angankan":"to wish for",
"menganggap":"to assume",
"menganggarkan":"to estimate",
"menganggotai":"to be a member of",
"mengangguk":"to nod",
"menganggur":"unemployed",
"mengangin":"to winnow",
"menganginkan":"to put in a windy place",
"mengangkut":"to transport",
"menganiayai":"to oppress",
"menganjakkan":"to change the position of",
"menganjal":"to be elastic",
"menganjur":"to jut out",
"menganjurkan":"to propose",
"mengansurkan":"to pay by instalments",
"mengantarai":"to mediate",
"pengabdian":"slavery",
"pengacara":"master of ceremonies",
"pengacum":"instigator",
"pengacuman":"agitation",
"pengampu":"sycophant",
"pengampun":"one who forgives",
"pengampunan":"amnesty",
"pengamuk":"person who runs amuck",
"penganggur":"one who is unemployed",
"pengangguran":"unemployment",
"pengangkutan":"transportation",
"penganiaya":"oppressor",
"penganiayaan":"oppression",
"penganjur":"organizer",
"peradaban":"civilization",
"peranakan":"native",
"peranginan":"holiday resort",
"perangkaan":"statistics",
"perantara":"mediator",
"perantaraan":"mediation",
"seantero":"the whole",
"terampai":"hung to air",
"terancam":"threatened",
"terangan-angan":"to constantly wish for",
"terangguk-angguk":"to keep nodding"
};

//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("(\\s)"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"(\\s)", "ig"), "$1"+reps[i]+"$2");
}}
return buff;
}

function CopyToClipboardOut() {
window.clipboardData.setData('Text', document.getElementById('output').value);
}

function CopyToClipboardIn() {
window.clipboardData.setData('Text', document.getElementById('input').value);
}

function translate(){
document.getElementById('output').value = repAll(document.getElementById('input').value, reps);
}
</script>


<textarea id="input" rows="10" cols="40">
Say amin peradaban !
abah-abah
</textarea><br />


<input type="button" value="translate" onClick="translate();" />&nbsp;
<input type="button" value="clear" onClick="document.getElementById('output').value ='';" />&nbsp;
<input type="button" value="copy input" onClick="CopyToClipboardIn();" />&nbsp;
<input type="button" value="copy output" onClick="CopyToClipboardOut();" />&nbsp;
<br />
<textarea id="output" rows="10" cols="40"></textarea>
</html>

skyonline
12-16-2009, 07:05 AM
Trinithis you scripts is very hard to understand...

skyonline
12-16-2009, 07:20 AM
rnd me you scripts also can't work when i test in IE..i tyep abah and when i translate it the anwser in same "abah"...

Philip M
12-16-2009, 07:21 AM
i got one more question...

phrase = phrase.replace(/\babah\b/ig, "father");
phrase = phrase.replace(/\babah-abah\b/ig, "harness");

words that i bold is same words...

abah is = father

abah-abah is = harness

but when i tyep words abah-abah in input text area the anwser in output text area is father-father...the tru anwser is :

abah-abah = harness

how to fix it...?

Simply reverse the two entries thus:-

phrase = phrase.replace(/\babah-abah\b/ig, "harness");
phrase = phrase.replace(/\babah\b/ig, "father");

Same with

"anak":"child",
"anak-anak":"many children"

Philip M
12-16-2009, 07:41 AM
rnd me you scripts also can't work when i test in IE..i tyep abah and when i translate it the anwser in same "abah"...

Alas, the "great working solution" does not not work at all! :( The "Clear" button does not work either. :( It is hard to see how rnd me can possibly have tested this. :p

One more time: - Trinithis and rnd me - skyonline seems to be a beginner. The art of successful teaching is to tailor the advice to the knowledge/skill level of the student. Sophisticated graduate-level solutions are not very helpful to people struggling with basic concepts - especially if they contain errors.

But when the attitude is "who cares about the OP?", well, Narcissus rules.

skyonline
12-16-2009, 07:55 AM
Simply reverse the two entries thus:-

phrase = phrase.replace(/\babah-abah\b/ig, "harness");
phrase = phrase.replace(/\babah\b/ig, "father");

Same with

"anak":"child",
"anak-anak":"many children"

tq Philip M... but did u got others way to fix it,,,because if i reverse the two entries is very difficult becuse i got over 10000++ word...tq

Philip M
12-16-2009, 08:09 AM
tq Philip M... but did u got others way to fix it,,,because if i reverse the two entries is very difficult becuse i got over 10000++ word...tq

No, where one word is embedded in another at a word boundary you must place the entries in descending alphabetical order as here:-

phrase = phrase.replace(/\banak-anakan\b/ig, "doll");
phrase = phrase.replace(/\banak-anak\b/ig, "many children");
phrase = phrase.replace(/\banak\b/ig, "child");
phrase = phrase.replace(/\banak-beranak\b/ig, "togther with the children");

But surely there are not very many instances of this?

Although I admire your industry, you are really creating a dictionary rather than a translator. Don't forget the old joke: "Out of sight, out of mind" translates to "Invisible Idiot". :D

rnd me
12-16-2009, 09:16 AM
rnd me you scripts also can't work when i test in IE..i tyep abah and when i translate it the anwser in same "abah"...

grrr.sorry about that, i tested but i didn't unit test, and it looks like my manual testing missed a situation; when the word in at the very start or end of a string.

i was really working to avoid any extra processing to maximize performance, so i hated the idea of complicating the RegExp...

then it dawned on me that since the script worked everywhere but the edges, i could take away the edges by padding it with a space front and back, and then slice off that padding after the replacements were made.


if this one doesn't work somebody shoot me!


<html><script>


//data:
var reps={
"abah":"father",
"abah-abah":"harness",
"abai":"neglect",
"abang angkat":"foster brother",
"abang ipar":"brother-in-law",
"abang tiri":"stepbrother",
"abang":"elder brother",
"abdi":"slave",
"abjad":"alphabet",
"abu":"ash",
"abuk":"dust",
"abung":"pomelo",
"acah":"tease",
"acapkali":"often",
"acar":"pickle",
"acara":"event",
"aci":"shaft",
"acu":"to direct weapon at target",
"acuan":"a mould",
"acuh tak acuh":"to show indifference",
"acuh":"to care for",
"acum":"provoke",
"acuman":"provocation",
"ada":"have",
"adab":"politeness",
"adakala":"sometimes",
"adakalanya":"sometimes",
"adalah":"is",
"adapun":"with regard to",
"ambung":"back-basket",
"amin":"amen",
"amir":"emir",
"ampai":"hang",
"ampai-ampai":"jellyfish",
"ampaian":"clothes-line",
"ampu":"ingratiate",
"ampun":"pardon",
"amuk":"amuck",
"anai-anai":"termite",
"anak angkat":"adopted child",
"anak bulan":"new moon",
"anak dara":"virgin",
"anak merah":"newborn",
"anak":"child",
"anak-anak":"many children",
"anak-anakan":"doll",
"anak-beranak":"together with the children",
"anakanda":"son",
"anasir jahat":"bad social element",
"anasir":"an element",
"ancam":"menace",
"ancaman":"a threat",
"anda":"you",
"andai":"if",
"andaian":"assumption",
"andainya":"suppose",
"andam":"beautician who does the make-up for the bride and bridegroom",
"andas":"anvil",
"anduh":"suspend by means of a sling",
"aneh":"bizarre",
"aneka ragam":"variety show",
"aneka":"multifarious",
"anekarama":"concert",
"angan":"imagination",
"angan-angan":"imagination",
"angau":"lovesick",
"anggap":"assume",
"anggapan":"assumption",
"anggar":"estimate",
"anggaran":"estimation",
"anggerik":"orchid",
"anggota badan":"part of the body",
"anggota":"limb",
"angguk":"nod",
"anggukan":"act of nodding",
"anggur":"grape",
"angin":"wind",
"angka":"numerical figure",
"angkara":"bad",
"angkasa":"space",
"angkasawan":"astronaut",
"angkat":"lift",
"angkatan":"troops",
"angkuh":"haughty",
"angkup":"callipers",
"angkut":"convey",
"angsa":"goose",
"aniaya":"maltreatment",
"anjak":"shift position slightly",
"anjal":"elastic",
"anjing":"dog",
"anjung":"projecting facade",
"anjungan":"bridge",
"anjur":"to jut out",
"anjuran":"proposal",
"ansur":"progress",
"ansuran":"instalment",
"antah":"rice grains left unhusked after pounding",
"antan":"pestle",
"antara":"among",
"antarabangsa":"international",
"antero":"whole",
"anti":"opposed to",
"anting":"pendulum",
"berada":"rich",
"beradab":"courteous",
"beranak":"to bear a child",
"berandai":"to discuss",
"beraneka":"having a variety of",
"berangan-angan":"to day-dream",
"beranggapan":"of the opinion",
"beranggota":"having limbs",
"berangin":"windy",
"berangkat":"to depart",
"berangkut":"to transport several times",
"beranjak":"to shift position slightly",
"beransur":"to progress in stages",
"diadakan":"to be held",
"keadaan":"condition",
"keampunan":"forgiveness",
"keanak-anakan":"childish",
"keanggotaan":"membership",
"keangkuhan":"arrogance",
"keanjalan":"flexibility",
"keberangkatan":"departure",
"mengabaikan":"to disregard",
"mengabdikan":"to enslave",
"mengabui":"to cheat",
"mengacah-acah":"to tease",
"mengacarakan":"to compere",
"mengacu":"to test",
"mengacuhkan":"to care for",
"mengacukan":"to aim weapon at target",
"mengacum":"to provoke",
"mengada-ngada":"to boast",
"mengada-ngadakan":"to concoct an excuse",
"mengadakan":"to hold",
"mengampaikan":"to hang clothes out to dry",
"mengampu":"to ingratiate",
"mengampunkan":"to forgive",
"mengamuk":"attack without thinking",
"mengancam":"to threaten",
"mengandaikan":"suppose",
"mengandam":"to make-up the bride and bridegroom",
"mengangan-angankan":"to wish for",
"menganggap":"to assume",
"menganggarkan":"to estimate",
"menganggotai":"to be a member of",
"mengangguk":"to nod",
"menganggur":"unemployed",
"mengangin":"to winnow",
"menganginkan":"to put in a windy place",
"mengangkut":"to transport",
"menganiayai":"to oppress",
"menganjakkan":"to change the position of",
"menganjal":"to be elastic",
"menganjur":"to jut out",
"menganjurkan":"to propose",
"mengansurkan":"to pay by instalments",
"mengantarai":"to mediate",
"pengabdian":"slavery",
"pengacara":"master of ceremonies",
"pengacum":"instigator",
"pengacuman":"agitation",
"pengampu":"sycophant",
"pengampun":"one who forgives",
"pengampunan":"amnesty",
"pengamuk":"person who runs amuck",
"penganggur":"one who is unemployed",
"pengangguran":"unemployment",
"pengangkutan":"transportation",
"penganiaya":"oppressor",
"penganiayaan":"oppression",
"penganjur":"organizer",
"peradaban":"civilization",
"peranakan":"native",
"peranginan":"holiday resort",
"perangkaan":"statistics",
"perantara":"mediator",
"perantaraan":"mediation",
"seantero":"the whole",
"terampai":"hung to air",
"terancam":"threatened",
"terangan-angan":"to constantly wish for",
"terangguk-angguk":"to keep nodding"
};

//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("(\\s)"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"(\\s)", "ig"), "$1"+reps[i]+"$2");
}}
return buff;
}

function CopyToClipboardOut() {
window.clipboardData.setData('Text', document.getElementById('output').value);
}

function CopyToClipboardIn() {
window.clipboardData.setData('Text', document.getElementById('input').value);
}



function translate(){
document.getElementById('output').value = repAll(" "+document.getElementById('input').value+" ", reps).slice(1,-1);
}
</script>

<textarea id="input" rows="10" cols="40">"Say amin peradaban !"
abah-abah

abah</textarea><br />
<input type="button" value="translate" onClick="translate();" />&nbsp;
<input type="button" value="clear" onClick="document.getElementById('output').value ='';" />&nbsp;
<input type="button" value="copy input" onClick="CopyToClipboardIn();" />&nbsp;
<input type="button" value="copy output" onClick="CopyToClipboardOut();" />&nbsp;
<br />
<textarea id="output" rows="10" cols="40"></textarea>
</html>

test yields:

"Say amen civilization !"
harness
father

despite the past bugs, i honestly think this (or something like it) is your best bet going forward.

skyonline
12-16-2009, 09:46 AM
grrr.sorry about that, i tested but i didn't unit test, and it looks like my manual testing missed a situation; when the word in at the very start or end of a string.

i was really working to avoid any extra processing to maximize performance, so i hated the idea of complicating the RegExp...

then it dawned on me that since the script worked everywhere but the edges, i could take away the edges by padding it with a space front and back, and then slice off that padding after the replacements were made.


if this one doesn't work somebody shoot me!


<html><script>


//data:
var reps={
"abah":"father",
"abah-abah":"harness",
"abai":"neglect",
"abang angkat":"foster brother",
"abang ipar":"brother-in-law",
"abang tiri":"stepbrother",
"abang":"elder brother",
"abdi":"slave",
"abjad":"alphabet",
"abu":"ash",
"abuk":"dust",
"abung":"pomelo",
"acah":"tease",
"acapkali":"often",
"acar":"pickle",
"acara":"event",
"aci":"shaft",
"acu":"to direct weapon at target",
"acuan":"a mould",
"acuh tak acuh":"to show indifference",
"acuh":"to care for",
"acum":"provoke",
"acuman":"provocation",
"ada":"have",
"adab":"politeness",
"adakala":"sometimes",
"adakalanya":"sometimes",
"adalah":"is",
"adapun":"with regard to",
"ambung":"back-basket",
"amin":"amen",
"amir":"emir",
"ampai":"hang",
"ampai-ampai":"jellyfish",
"ampaian":"clothes-line",
"ampu":"ingratiate",
"ampun":"pardon",
"amuk":"amuck",
"anai-anai":"termite",
"anak angkat":"adopted child",
"anak bulan":"new moon",
"anak dara":"virgin",
"anak merah":"newborn",
"anak":"child",
"anak-anak":"many children",
"anak-anakan":"doll",
"anak-beranak":"together with the children",
"anakanda":"son",
"anasir jahat":"bad social element",
"anasir":"an element",
"ancam":"menace",
"ancaman":"a threat",
"anda":"you",
"andai":"if",
"andaian":"assumption",
"andainya":"suppose",
"andam":"beautician who does the make-up for the bride and bridegroom",
"andas":"anvil",
"anduh":"suspend by means of a sling",
"aneh":"bizarre",
"aneka ragam":"variety show",
"aneka":"multifarious",
"anekarama":"concert",
"angan":"imagination",
"angan-angan":"imagination",
"angau":"lovesick",
"anggap":"assume",
"anggapan":"assumption",
"anggar":"estimate",
"anggaran":"estimation",
"anggerik":"orchid",
"anggota badan":"part of the body",
"anggota":"limb",
"angguk":"nod",
"anggukan":"act of nodding",
"anggur":"grape",
"angin":"wind",
"angka":"numerical figure",
"angkara":"bad",
"angkasa":"space",
"angkasawan":"astronaut",
"angkat":"lift",
"angkatan":"troops",
"angkuh":"haughty",
"angkup":"callipers",
"angkut":"convey",
"angsa":"goose",
"aniaya":"maltreatment",
"anjak":"shift position slightly",
"anjal":"elastic",
"anjing":"dog",
"anjung":"projecting facade",
"anjungan":"bridge",
"anjur":"to jut out",
"anjuran":"proposal",
"ansur":"progress",
"ansuran":"instalment",
"antah":"rice grains left unhusked after pounding",
"antan":"pestle",
"antara":"among",
"antarabangsa":"international",
"antero":"whole",
"anti":"opposed to",
"anting":"pendulum",
"berada":"rich",
"beradab":"courteous",
"beranak":"to bear a child",
"berandai":"to discuss",
"beraneka":"having a variety of",
"berangan-angan":"to day-dream",
"beranggapan":"of the opinion",
"beranggota":"having limbs",
"berangin":"windy",
"berangkat":"to depart",
"berangkut":"to transport several times",
"beranjak":"to shift position slightly",
"beransur":"to progress in stages",
"diadakan":"to be held",
"keadaan":"condition",
"keampunan":"forgiveness",
"keanak-anakan":"childish",
"keanggotaan":"membership",
"keangkuhan":"arrogance",
"keanjalan":"flexibility",
"keberangkatan":"departure",
"mengabaikan":"to disregard",
"mengabdikan":"to enslave",
"mengabui":"to cheat",
"mengacah-acah":"to tease",
"mengacarakan":"to compere",
"mengacu":"to test",
"mengacuhkan":"to care for",
"mengacukan":"to aim weapon at target",
"mengacum":"to provoke",
"mengada-ngada":"to boast",
"mengada-ngadakan":"to concoct an excuse",
"mengadakan":"to hold",
"mengampaikan":"to hang clothes out to dry",
"mengampu":"to ingratiate",
"mengampunkan":"to forgive",
"mengamuk":"attack without thinking",
"mengancam":"to threaten",
"mengandaikan":"suppose",
"mengandam":"to make-up the bride and bridegroom",
"mengangan-angankan":"to wish for",
"menganggap":"to assume",
"menganggarkan":"to estimate",
"menganggotai":"to be a member of",
"mengangguk":"to nod",
"menganggur":"unemployed",
"mengangin":"to winnow",
"menganginkan":"to put in a windy place",
"mengangkut":"to transport",
"menganiayai":"to oppress",
"menganjakkan":"to change the position of",
"menganjal":"to be elastic",
"menganjur":"to jut out",
"menganjurkan":"to propose",
"mengansurkan":"to pay by instalments",
"mengantarai":"to mediate",
"pengabdian":"slavery",
"pengacara":"master of ceremonies",
"pengacum":"instigator",
"pengacuman":"agitation",
"pengampu":"sycophant",
"pengampun":"one who forgives",
"pengampunan":"amnesty",
"pengamuk":"person who runs amuck",
"penganggur":"one who is unemployed",
"pengangguran":"unemployment",
"pengangkutan":"transportation",
"penganiaya":"oppressor",
"penganiayaan":"oppression",
"penganjur":"organizer",
"peradaban":"civilization",
"peranakan":"native",
"peranginan":"holiday resort",
"perangkaan":"statistics",
"perantara":"mediator",
"perantaraan":"mediation",
"seantero":"the whole",
"terampai":"hung to air",
"terancam":"threatened",
"terangan-angan":"to constantly wish for",
"terangguk-angguk":"to keep nodding"
};

//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("(\\s)"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"(\\s)", "ig"), "$1"+reps[i]+"$2");
}}
return buff;
}

function CopyToClipboardOut() {
window.clipboardData.setData('Text', document.getElementById('output').value);
}

function CopyToClipboardIn() {
window.clipboardData.setData('Text', document.getElementById('input').value);
}



function translate(){
document.getElementById('output').value = repAll(" "+document.getElementById('input').value+" ", reps).slice(1,-1);
}
</script>

<textarea id="input" rows="10" cols="40">"Say amin peradaban !"
abah-abah

abah</textarea><br />
<input type="button" value="translate" onClick="translate();" />&nbsp;
<input type="button" value="clear" onClick="document.getElementById('output').value ='';" />&nbsp;
<input type="button" value="copy input" onClick="CopyToClipboardIn();" />&nbsp;
<input type="button" value="copy output" onClick="CopyToClipboardOut();" />&nbsp;
<br />
<textarea id="output" rows="10" cols="40"></textarea>
</html>

test yields:

"Say amen civilization !"
harness
father

despite the past bugs, i honestly think this (or something like it) is your best bet going forward.

TQ rnd me..1 more request...can put button sellect all and paste..

rnd me
12-16-2009, 09:52 AM
Alas, the "great working solution" does not not work at all! :( The "Clear" button does not work either. :( It is hard to see how rnd me can possibly have tested this. :p

One more time: - Trinithis and rnd me - skyonline seems to be a beginner. The art of successful teaching is to tailor the advice to the knowledge/skill level of the student. Sophisticated graduate-level solutions are not very helpful to people struggling with basic concepts - especially if they contain errors.

But when the attitude is "who cares about the OP?", well, Narcissus rules.

yes, how convenient for you. ;^)

we're all here to help each other, we all want to be "right", and none of us are perfect.


"who cares about the OP?" was largely sarcasm, which i often forget doesn't translate to HTML. My point was that you shouldn't fault me for trying (repeatedly) to help the guy, even if i erred and had a side-conversation along the way. when a superior like Trinithis has something to say, I'm all ears.

I offer a 100% money-back guarantee on all my posts, and i wouldn't have spent a my time creating two demos from scratch if i didn't care about the OP's problem.


I think there is a language barrier here, normal when describing complicated things like applications. i do have to say, i'm still not 100% sure what this is for, and I was WAY off on my first stab.


i don't think my solution is all that complicated, and it's certainly more scalable and maintainable than "you must place the entries in descending alphabetical order", which is not a requirement of my version...


when i realized the OP needed "ready to demo" code, i wrapped it up.
i don't think it gets any easier than cut and paste <HTML> demos.
that a pretty good break if you ask me!

going forward, i'll slow down and try to be more careful about pre-testing.

rnd me
12-16-2009, 10:14 AM
TQ rnd me..1 more request...can put button sellect all and paste..

sure, i can do that.

replace everything after "//engine:" from my last post with this:


//engine:
function repAll(str, reps){
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("(\\s)"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"(\\s)", "ig"), "$1"+reps[i]+"$2");
}}
return buff;
}

function CB(s) {
if (s && window.clipboardData) {
window.clipboardData.setData("Text", s);
} else {
return window.clipboardData.getData("Text");
}
}
//end CB

function translate(){
document.getElementById('output').value = repAll(" "+document.getElementById('input').value+" ", reps).slice(1,-1);
}
</script>
<input type="button" value="select" onClick="document.getElementById('input').select()" />&nbsp;
<input type="button" value="copy" onClick="CB(document.getElementById('input').value)" />&nbsp;
<input type="button" value="paste" onClick="document.getElementById('input').value=CB()" />&nbsp;
<br />
<textarea id="input" rows="10" cols="60">"Say amin peradaban !"
abah-abah
abah</textarea><br />

<input type="button" value="translate" onClick="translate();" />&nbsp;
<input type="button" value="clear" onClick="document.getElementById('output').value =''; document.getElementById('input').value ='';" />&nbsp;

<br />
<textarea id="output" rows="10" cols="60"></textarea><br />

<input type="button" value="select" onClick="document.getElementById('output').select()" />&nbsp;
<input type="button" value="copy" onClick="CB(document.getElementById('output').value)" />&nbsp;
<input type="button" value="paste" onClick="document.getElementById('output').value=CB()" /><br />

</html>

(thoroughly) tested in IE8.

Philip M
12-16-2009, 10:46 AM
skyonline -

If there are 10000+ words in your dictionary then you will need to devise a strtaegy to reduce the time taken (number of comparisons/replaces). You may care to consider this as a first step along that road:-


<textarea id="input"></textarea><br>
<input type="button" value="translate" onClick="go();" /><br>

<script type = "text/javascript">

var phrase; // must be a global variable
function go() {
phrase = document.getElementById("input").value.toLowerCase();
phrase = phrase.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
phrase = phrase.replace(/\s{2,}/g," "); // replace multiple spaces with one space
phrase = phrase.replace(/[^a-z\-\s]/gi, ""); // strip all but a-z and hyphen space
var firstchar = phrase.substr(0,1); // extract the first character

if (firstchar == "a") {translatea(phrase)}
if (firstchar == "b") {translateb(phrase)}
if (firstchar == "c") {translatec(phrase)}
// and so on, to
if (firstchar == "z") {translatez(phrase)}
else {
alert ("Invalid input");
return false;
}
}

function translatea(phrase){
// translate all words/phrases beginning with a
document.getElementById('output').value = phrase;
}

function translateb(phrase){
// translate all words/phrases beginning with b
document.getElementById('output').value = phrase;
}

function translateb(phrase){
// translate all words/phrases beginning with c
document.getElementById('output').value = phrase;
}

</script>

rnd me's script has the same problem if there are a very large number of entries, and will need adjustment to "maximise performance".


"I offer a 100% money-back guarantee on all my posts, and i wouldn't have spent a my time creating two demos from scratch if i didn't care about the OP's problem." Well, there is another possible explanation why you might want to do that, and the guarantee turned out to be somewhat, er, tarnished on two occasions. Even if the sucker (in your revealing expression) buys into the response enough to click "thanks"...
I have to say that for myself I am entirely sure what the OP is trying to do - he is trying to make a Malay - English dictionary.

skyonline
12-16-2009, 12:03 PM
to philip M : I say to use scripts from rnd me how I to reduce the time taken (number of comparisons/replaces).what need i have to do..?

to rnd me : thank you (only this woods i can say)

Philip M
12-16-2009, 12:21 PM
to philip M : I say to use scripts from rnd me how I to reduce the time taken (number of comparisons/replaces).what need i have to do..?

to rnd me : thank you (only this woods i can say)

Suggest you ask rnd me. I have had enough of this topic, and I am not planning to waste any more of my time.

skyonline
12-16-2009, 12:36 PM
oh..ok..if i use your scripts...how to use this :


<textarea id="input"></textarea><br>
<input type="button" value="translate" onClick="go();" /><br>

<script type = "text/javascript">

var phrase; // must be a global variable
function go() {
phrase = document.getElementById("input").value.toLowerCase();
phrase = phrase.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
phrase = phrase.replace(/\s{2,}/g," "); // replace multiple spaces with one space
phrase = phrase.replace(/[^a-z\-\s]/gi, ""); // strip all but a-z and hyphen space
var firstchar = phrase.substr(0,1); // extract the first character

if (firstchar == "a") {translatea(phrase)}
if (firstchar == "b") {translateb(phrase)}
if (firstchar == "c") {translatec(phrase)}
// and so on, to
if (firstchar == "z") {translatez(phrase)}
else {
alert ("Invalid input");
return false;
}
}

function translatea(phrase){
// translate all words/phrases beginning with a
document.getElementById('output').value = phrase;
}

function translateb(phrase){
// translate all words/phrases beginning with b
document.getElementById('output').value = phrase;
}

function translateb(phrase){
// translate all words/phrases beginning with c
document.getElementById('output').value = phrase;
}

</script>

rnd me
12-16-2009, 01:17 PM
If there are 10000+ words in your dictionary then you will need to devise a strtaegy to reduce the time taken (number of comparisons/replaces). You may care to consider this as a first step along that road:-

...

rnd me's script has the same problem if there are a very large number of entries, and will need adjustment to "maximise performance".

Well, I was done with this, but slandering my code's performance pulled me back in...

I designed my code from ground up to be as quick as possible!

I agree 100% that you want to call String.replace() as little as possible.
that's exactly why my code checks for the raw existence of a potential replacement using "".indexOf() before attempting a replacement.

i don't have String.replace() on my "back of napkin" benchmark (http://danml.com/bench/), but i do have String.match(), which takes at least as much CPU work as String.replace()...

According to the bench, String.indexOf() is 5-10 X faster than String.match(), which helps a lot in terms of overall performance.


PhillipM: don't throw stone if you live in a glass house!
does this code work? :


var phrase; // must be a global variable
function go() {
phrase = document.getElementById("input").value.toLowerCase();
phrase = phrase.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
phrase = phrase.replace(/\s{2,}/g," "); // replace multiple spaces with one space
phrase = phrase.replace(/[^a-z\-\s]/gi, ""); // strip all but a-z and hyphen space
var firstchar = phrase.substr(0,1); // extract the first character

if (firstchar == "a") {translatea(phrase)}
if (firstchar == "b") {translateb(phrase)}
if (firstchar == "c") {translatec(phrase)}
// and so on, to
if (firstchar == "z") {translatez(phrase)}
else {
alert ("Invalid input");
return false;
}
}

function translatea(phrase){
// translate all words/phrases beginning with a
document.getElementById('output').value = phrase;
}
...


it seems to me like it only checks the textbox value for matches that coincide with the first word. but what about the second and third words?

i assumed from the <textarea> that the app was supposed to handle more than one word at once. if there was a loop or something i could see the code working, but i don't think the above will.

plus, wouldn't this code remove all capitalization, punctuation, line breaks, parens, and other meaningful semantics? i don't know anything about the Malay language, but I can't imagine that being desirable!

what would phillip's code do to html text, "fred@john.bobroberts.com", or "bob paid $100,000.00 for his house?" ?

consider other performance factors:
each RegExp instance is an object, has to be parsed and instantiated.
global variables are slower than locals, function calls have overhead, and the by-value passing of strings forces un-needed duplication.

by comparison, my code instantiates only strings, keeps the property lookups local inside repAll, and doesn't have any repetitive function overhead, and doesn't need to duplicate the string between replacing each match.

i think that all adds up to faster performance, but if Philip wants to code a benchmark and race them, i'd accept the challenge anytime.

if there will be no non-alphabetic character besides spaces in the input, i can make my code a lot faster by taming my RegExp builder. I tested the code as-is, and it does itself (~5kb) in 60ms in firefox3 on a cheap 3-year vista desktop.

if anyone needs it faster, they can download google chrome for free.
chrome is 30-100X faster than IE, and doesn't interrupt long-running scripts as quickly, making it a great javascript processor for CPU-heavy applications.

-------

skyonline:

if it's of any use, with a tiny addition, my base code can do one word instantly. copy the code below.

put the button next to the other buttons, and paste the translateAWord function after the translate() function to add this feature:


function translateAWord(){
document.getElementById('output').value = reps[document.getElementById('input').value.toLocaleLowerCase()];
}


the button for it:

<input type="button" value="translate single" onClick="translateAWord();" />&nbsp;

Philip M
12-16-2009, 03:51 PM
it seems to me like it only checks the textbox value for matches that coincide with the first word. but what about the second and third words?

plus, wouldn't this code remove all capitalization, punctuation, line breaks, parens, and other meaningful semantics? i don't know anything about the Malay language, but I can't imagine that being desirable!

what would phillip's code do to html text, "fred@john.bobroberts.com", or "bob paid $100,000.00 for his house?" ?


As I understand it the objective is that the user enters a single Malay word, phrase or self-contained expression such as "acuh tak acuh", (but not a complete sentence or paragraph) and the result is the translation of that into English, assuming of course that the word is in the database. Example:- "andam":"beautician who does the make-up for the bride and bridegroom". In other words, a dictionary. Surely that is clear from the example code which skyonline posted? Words in dictionaries do not normally include punctuation or line breaks. Also, dictionaries are arranged alphabetically in lower case (excepting proper names) in accordance with the first letter of the word, phrase or expression. The Malay language uses the Roman alphabet.

A translation tool which would translate whole sentences or paragraphs is a completely different thing, and far beyond the scope of this thread. Of course your script will translate (a collection of individual) multiple words at once, but that does not result in a meaningful sentence. "abdi abjad abu" results in the enigmatic "slave alphabet ash". I agree that the use of a textarea rather than a textbox gives a misleading impression.

"fred@john.bobroberts.com" is not a Malay word, phrase or expression. It is not a dictionary entry in English either. Neither is "Bob paid $100,000.00 for his house."

So yes, my code works. I always test anything that I post here, as of course even a misplaced ; can cause problems, as you may have noticed. But I do not claim that it is the most compact or most efficient that can be devised. But it does have the merit of being comprehensible.

Normally I would care as little about performance as you do about the OP. But as I say, with 10000+ entries it makes sense to take steps to reduce the number of comparisons required.

However, I am weary of this topic. I have done my best to help skyonline (whose question initially was about the problem of embedded words) but I am not interested in prolonging arguments, so that is the last I have to say.

skyonline
12-17-2009, 01:14 AM
thanks to all.i have made Translate text is successfully
based on your help..all goodwill i will remember ..
especially to Philip M and RnD me .. sorry if there is one and wrong.
is because I really do not know about the javascript language even basic
code.10000 ++ TQ... in malay : terima kasih : )... peace..

rnd me
12-17-2009, 03:49 AM
As I understand it the objective is that the user enters a single Malay word, phrase or self-contained expression such as "acuh tak acuh", (but not a complete sentence or paragraph) and the result is the translation of that into English, assuming of course that the word is in the database. Example:- "andam":"beautician who does the make-up for the bride and bridegroom". In other words, a dictionary. Surely that is clear from the example code which skyonline posted? Words in dictionaries do not normally include punctuation or line breaks. Also, dictionaries are arranged alphabetically in lower case (excepting proper names) in accordance with the first letter of the word, phrase or expression. The Malay language uses the Roman alphabet.

it wasn't clear to me, but this seems logical. i appreciate your willingness to share your insight.

Figuring that there won't be anything but words and compounds makes my job a lot easier. I optimized my code to handle only words and compounds.
I kept the old one, just in case, and added a new button for the fast version.
In terms of performance, right now the "translate phrase" is 10-20x faster than "translate anything".
If there were more definitions, "translate phrase" will be even faster-er.

"translate phrase" doesn't use a RegExp and it only tries as many replacements as there are words, which is very efficient. If you have 10,000 definitions, use "translate phrase" if possible. It does discard capital letters, but that is probably worth it. I figured out a way to keep the whitespace in firefox, but in IE "translate phrase" will give back a space-separated list of replacements.

it never hurts to have too-much functionality on a simple application.

skyonline:
don't worry about all the tech talk, here is the updated code, the best version yet:

<html>
<head>
<title>Malay Translator</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="dandavis" />
</head>
<script>

//data:
var reps={
"abah":"father",
"abah-abah":"harness",
"abai":"neglect",
"abang angkat":"foster brother",
"abang ipar":"brother-in-law",
"abang tiri":"stepbrother",
"abang":"elder brother",
"abdi":"slave",
"abjad":"alphabet",
"abu":"ash",
"abuk":"dust",
"abung":"pomelo",
"acah":"tease",
"acapkali":"often",
"acar":"pickle",
"acara":"event",
"aci":"shaft",
"acu":"to direct weapon at target",
"acuan":"a mould",
"acuh tak acuh":"to show indifference",
"acuh":"to care for",
"acum":"provoke",
"acuman":"provocation",
"ada":"have",
"adab":"politeness",
"adakala":"sometimes",
"adakalanya":"sometimes",
"adalah":"is",
"adapun":"with regard to",
"ambung":"back-basket",
"amin":"amen",
"amir":"emir",
"ampai":"hang",
"ampai-ampai":"jellyfish",
"ampaian":"clothes-line",
"ampu":"ingratiate",
"ampun":"pardon",
"amuk":"amuck",
"anai-anai":"termite",
"anak angkat":"adopted child",
"anak bulan":"new moon",
"anak dara":"virgin",
"anak merah":"newborn",
"anak":"child",
"anak-anak":"many children",
"anak-anakan":"doll",
"anak-beranak":"together with the children",
"anakanda":"son",
"anasir jahat":"bad social element",
"anasir":"an element",
"ancam":"menace",
"ancaman":"a threat",
"anda":"you",
"andai":"if",
"andaian":"assumption",
"andainya":"suppose",
"andam":"beautician who does the make-up for the bride and bridegroom",
"andas":"anvil",
"anduh":"suspend by means of a sling",
"aneh":"bizarre",
"aneka ragam":"variety show",
"aneka":"multifarious",
"anekarama":"concert",
"angan":"imagination",
"angan-angan":"imagination",
"angau":"lovesick",
"anggap":"assume",
"anggapan":"assumption",
"anggar":"estimate",
"anggaran":"estimation",
"anggerik":"orchid",
"anggota badan":"part of the body",
"anggota":"limb",
"angguk":"nod",
"anggukan":"act of nodding",
"anggur":"grape",
"angin":"wind",
"angka":"numerical figure",
"angkara":"bad",
"angkasa":"space",
"angkasawan":"astronaut",
"angkat":"lift",
"angkatan":"troops",
"angkuh":"haughty",
"angkup":"callipers",
"angkut":"convey",
"angsa":"goose",
"aniaya":"maltreatment",
"anjak":"shift position slightly",
"anjal":"elastic",
"anjing":"dog",
"anjung":"projecting facade",
"anjungan":"bridge",
"anjur":"to jut out",
"anjuran":"proposal",
"ansur":"progress",
"ansuran":"instalment",
"antah":"rice grains left unhusked after pounding",
"antan":"pestle",
"antara":"among",
"antarabangsa":"international",
"antero":"whole",
"anti":"opposed to",
"anting":"pendulum",
"berada":"rich",
"beradab":"courteous",
"beranak":"to bear a child",
"berandai":"to discuss",
"beraneka":"having a variety of",
"berangan-angan":"to day-dream",
"beranggapan":"of the opinion",
"beranggota":"having limbs",
"berangin":"windy",
"berangkat":"to depart",
"berangkut":"to transport several times",
"beranjak":"to shift position slightly",
"beransur":"to progress in stages",
"diadakan":"to be held",
"keadaan":"condition",
"keampunan":"forgiveness",
"keanak-anakan":"childish",
"keanggotaan":"membership",
"keangkuhan":"arrogance",
"keanjalan":"flexibility",
"keberangkatan":"departure",
"mengabaikan":"to disregard",
"mengabdikan":"to enslave",
"mengabui":"to cheat",
"mengacah-acah":"to tease",
"mengacarakan":"to compere",
"mengacu":"to test",
"mengacuhkan":"to care for",
"mengacukan":"to aim weapon at target",
"mengacum":"to provoke",
"mengada-ngada":"to boast",
"mengada-ngadakan":"to concoct an excuse",
"mengadakan":"to hold",
"mengampaikan":"to hang clothes out to dry",
"mengampu":"to ingratiate",
"mengampunkan":"to forgive",
"mengamuk":"attack without thinking",
"mengancam":"to threaten",
"mengandaikan":"suppose",
"mengandam":"to make-up the bride and bridegroom",
"mengangan-angankan":"to wish for",
"menganggap":"to assume",
"menganggarkan":"to estimate",
"menganggotai":"to be a member of",
"mengangguk":"to nod",
"menganggur":"unemployed",
"mengangin":"to winnow",
"menganginkan":"to put in a windy place",
"mengangkut":"to transport",
"menganiayai":"to oppress",
"menganjakkan":"to change the position of",
"menganjal":"to be elastic",
"menganjur":"to jut out",
"menganjurkan":"to propose",
"mengansurkan":"to pay by instalments",
"mengantarai":"to mediate",
"pengabdian":"slavery",
"pengacara":"master of ceremonies",
"pengacum":"instigator",
"pengacuman":"agitation",
"pengampu":"sycophant",
"pengampun":"one who forgives",
"pengampunan":"amnesty",
"pengamuk":"person who runs amuck",
"penganggur":"one who is unemployed",
"pengangguran":"unemployment",
"pengangkutan":"transportation",
"penganiaya":"oppressor",
"penganiayaan":"oppression",
"penganjur":"organizer",
"peradaban":"civilization",
"peranakan":"native",
"peranginan":"holiday resort",
"perangkaan":"statistics",
"perantara":"mediator",
"perantaraan":"mediation",
"seantero":"the whole",
"terampai":"hung to air",
"terancam":"threatened",
"terangan-angan":"to constantly wish for",
"terangguk-angguk":"to keep nodding"
};

//engines:
function repAll(str, reps){//handles any type of text and punctuation.
var buff=String(str);
for(i in reps){if(reps.hasOwnProperty(i) && (buff.indexOf(i)!==-1)){
buff=buff.replace(new RegExp("(\\s)"+i.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')+"(\\s)", "ig"), "$1"+reps[i]+"$2");
}}
return buff;
}


function repAllFast(str, reps){ //handles only space-separated words and compunds.
var buff=String(str).toLocaleLowerCase().replace(/[^\w\s\-]/g,"").split(/(\s+)/g);
for(var i=0, mx=buff.length;i<mx;i++){
buff[i]=reps[buff[i]]||buff[i]||" ";
}
return buff.join("\v"==="v"?" ":"");
}


function translate(){
Out.value = repAll(" "+In.value+" ", reps).slice(1,-1);
}

function translateAWord(){
Out.value = reps[In.value.toLocaleLowerCase()]||In.value;
}

function translateFast(){
Out.value = repAllFast(In.value, reps);
}

window.onload=function(){window.Out=document.getElementById('output'); window.In=document.getElementById('input');}

function CB(s) {
if (s && window.clipboardData) {
window.clipboardData.setData("Text", s);
} else {
return window.clipboardData.getData("Text");
}
}
//end CB

</script>
<input type="button" value="select" onclick="In.select()" />&nbsp;
<input type="button" value="copy" onclick="CB(In.value)" />&nbsp;
<input type="button" value="paste" onclick="In.value=CB()" />&nbsp;

<br />
<textarea id="input" rows="10" cols="60">"Say amin peradaban !"
abah-abah
abah</textarea>
<br />

<input type="button" value="translate phrase" onclick="translateFast();" />&nbsp;
<input type="button" value="translate anything" onclick="translate();" />&nbsp;
<input type="button" value="translate word" onclick="translateAWord();" />&nbsp;
<input type="button" value="clear" onclick="Out.value =''; In.value ='';" />&nbsp;

<br />
<textarea id="output" rows="10" cols="60"></textarea><br />

<input type="button" value="select" onclick="Out.select()" />&nbsp;
<input type="button" value="copy" onclick="CB(Out.value)" />&nbsp;
<input type="button" value="paste" onclick="Out.value=CB()" /><br />

</html>

thanks for the challenge skyonline, i had fun building the app!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum