Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with custom translator

    I am attempting to write a simple JS code for a translator I'm making. It is based on a language a friend has made up. My problem is I want to be able to translate from English to the gibberish and vice versa.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
    var wordPairs = [
    ["no", "il"],
    ["yes", "kra"],
    ["true", "k-kra"],
    ["action", "likt"],
    ["to be", "k"],
    ["to not be", "il’k"],
    ["to eat", "kuklu"],
    ["to fix", "aik"],
    ["to have", "kuru"],
    ["to jump", "likt-kikin-ik’haska"],
    ["to move", "likt"],
    ["to own", "kuru"],
    ["to see", "lkl-kasa-ik’kikn-ak"],
    ["to use", "k"],
    ["to use", "il’k"],
    ["to watch", "lkl-kasa-ik'kikin-ak"],
    
    ];
    function checkText(str){
    for(i=0; i < wordPairs.length; i++){
    str = str.replace(wordPairs[i][0],wordPairs[i][1]);
    }
    document.getElementById('reults').innerHTML = str;
    }
    window.onload=function(){
    document.getElementById('txtIn').onchange=function(){
    checkText(this.value);
    }
    }
    </script>
    </head>
    <body>
    <input type="text" id="txtIn" />
    <div id="reults"></div> 
    </body>
    </html>
    I got the original code from some people here; however, I cannot get it to reverse as well. Thank you in advance for any help.

  • #2
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi Kagi77

    something like this?:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
    var wordPairs = [
    ["no", "il"],
    ["yes", "kra"],
    ["true", "k-kra"],
    ["action", "likt"],
    ["to be", "k"],
    ["to not be", "il’k"],
    ["to eat", "kuklu"],
    ["to fix", "aik"],
    ["to have", "kuru"],
    ["to jump", "likt-kikin-ik’haska"],
    ["to move", "likt"],
    ["to own", "kuru"],
    ["to see", "lkl-kasa-ik’kikn-ak"],
    ["to use", "k"],
    ["to use", "il’k"],
    ["to watch", "lkl-kasa-ik'kikin-ak"],
    
    ];
    
    var textInput;
    var str;
    var direction;
    
    function checkText(str)
    {
    	str = document.getElementById('txtIn').value.toLowerCase();
    	direction = document.getElementById('directionSelect').selectedIndex;
    	if(direction==0)
    	{
    		for(i=0; i < wordPairs.length; i++)
    		{
    			str = str.replace(wordPairs[i][0],wordPairs[i][1]);
    		}
    	}
    	else
    	{
    		for(i=0; i < wordPairs.length; i++)
    		{
    			str = str.replace(wordPairs[i][1],wordPairs[i][0]);
    		}
    	}
    	
    	document.getElementById('result').innerHTML = str;
    	
    }
    
    
    window.onload=function()
    {
    	document.getElementById('convertBtn').onclick=checkText;
    }
    </script>
    </head>
    <body>
    <input type="text" id="txtIn" />
    <button id="convertBtn">Convert</button>
    <select id="directionSelect">
    <option>English to Giberish</option>
    <option>Giberish to English</option>
    </select>
    <div id="result"></div> 
    </body>
    </html>
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • Users who have thanked thesam101 for this post:

    Kagi77 (11-10-2011)

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yes that is what I was looking for thank you! There is a problem with translating where it is picking up other words. If you type in k-kra on gibberish to english it gives you to-be, yes when it should be true. I know it was something to do with the fact it isn't looking for exact matches (which is what I want). How do I do that?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Easy to fix: rearrange the ordering of your word pairs so that the longer gibberish words come first.

    So that k-kra is found before either k or kra alone.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Kagi77 (11-10-2011)

  • #5
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    See this is why I come here. I would have never thought to do that! Thank you so much!

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Here's a completely different approach.

    It's not useful/better for this situation, but it shows the kinds of things you *could* consider doing if you go further.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
    var eToG = {
        "no" : "il",
        "yes" : "kra",
        "true" : "k-kra",
        "action" : "likt",
        "to be" : "k",
        "not be" : "il’k",
        "to eat" : "kuklu",
        "to fix" : "aik",
        "to have" : "kuru",
        "to jump" : "likt-kikin-ik’haska",
        "to move" : "likt",
        "to own" : "kuru",
        "to see" : "lkl-kasa-ik’kikn-ak",
        "to use" : "nil’k",
        "to watch" : "lkl-kasa-ik'kikin-ak"
    }
    
    var gToE = [];
    for ( var word in eToG )
    {
        gToE[ eToG[word] ] = word;
    }
    
    function translate(text,table)
    {
        var where = document.getElementById("results");
        where.innerHTML = "";
        var words = text.value.split(" ");
        var w = 0;
        while ( w < words.length )
        {
            var word = words[w++];
            var xl = table[word];
            if ( xl == null && w < words.length ) 
            { 
                word += " " + words[w++];
                xl = table[word];
                if ( xl == null ) --w;
            }
            where.innerHTML += " " + ( xl == null ? "??" : xl );
        }
    }
    
    function setup( )
    {
        var form  = document.forms[0];
        var xlate = document.getElementById("results");
        form.english.onchange = function() { translate(this,eToG); }
        form.gibberish.onchange = function() { translate(this,gToE); }
    }
    
    window.onload = setup;
    </script>
    </head>
    <body>
    <form>
        English: <input type="text" name="english" /><br/>
        Gibberish: <input type="text" name="gibberish" /><br/>
        
        Translation: <div id="results"></div> 
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey I borrowed this code from you! (thank you by the way!) could someone explain to me why it's making up it's own words for me? i have "retwa" in there for "water", but its putting is wstayer and I can't figure out why... here's my code, i apologize for the amount of words, but i think it might be playing into this.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Ideenian Translator</title>
    <p><H2><center><font color="#8306CC" face="Freestyle Script" size="20">
    Ideenian Translator</center></H2></p>
    </br>
    
    <p><font color="D9270F" face="Gabriola" size="7">
    <center>
    <script type="text/javascript">
    var wordPairs = [
    
    ["a", "sa"],
    ["agree", "disenro"],
    ["air", "anim"],
    ["all", "lal"],
    ["allow", "wolit"],
    ["also", "aka"],
    ["am", "wa"],
    ["and", "nec"],
    ["angry", "grapy"],
    ["animal", "nanan"],
    ["answer", "quoner"],
    ["apple", "plepa"],
    ["are", "sonar"],
    ["aside", "dina"],
    ["ask ", "peto"],
    ["at", "tay"],
    ["ate", "te"],
    ["away", "yawan"],
    ["band", "nabna"],
    ["bath", "thab"],
    ["bathe", "thabay"],
    ["be ", "eb"],
    ["become", "motus"],
    ["bed", "cubal"],
    ["bee ", "eeb"],
    ["begin", "incepto"],
    ["believe", "putov"],
    ["belt", "blehteh"],
    ["best", "bonta"],
    ["book", "bok"],
    ["borrow", "rotumu"],
    ["box", "blan"],
    ["break", "erf"],
    ["breath", "santin"],
    ["bring", "rinto"],
    ["brother", "soha"],
    ["but", "et"],
    ["buy", "chay"],
    ["can", "nac"],
    ["carry", "ray"],
    ["change ", "jetio"],
    ["cold", "doona"],
    ["color", "roloc"],
    ["come", "mec"],
    ["come", "meco"],
    ["cool", "looin"],
    ["correct", "rotsek"],
    ["crazy", "zycra"],
    ["creature", "turan"],
    ["creepy", "peecyr"],
    ["crystal ", "katyr"],
    ["day", "yad"],
    ["death", "thead"],
    ["do", "odi"],
    ["dress", "dreeah"],
    ["dry", "dren"],
    ["earth", "lethra"],
    ["eat", "ta"],
    ["egg", "geh"],
    ["eight", "tet"],
    ["end", "nedn"],
    ["evil", "mwaha"],
    ["father", "dira"],
    ["feel", "tago"],
    ["fine", "nifa"],
    ["fire", "rifelin"],
    ["fish", "shif"],
    ["five", "vif"],
    ["fix", "ify"],
    ["flower", "reflow"],
    ["food", "odof"],
    ["for", "ga"],
    ["four", "rof"],
    ["fresh", "shefeh"],
    ["Friday", "dirfy"],
    ["friend ", "nolaf"],
    ["from", "ab"],
    ["go", "ogg"],
    ["good", "dool"],
    ["goodbye", "tasen/tas/en"],
    ["grass", "snaf"],
    ["happy", "peeha"],
    ["hat", "tah"],
    ["hate", "tahee"],
    ["have", "vah"],
    ["he", "eh"],
    ["heal", "leah"],
    ["hear", "reah"],
    ["hello", "tasai/tas/ai"],
    ["here", "reh"],
    ["his", "sih"],
    ["hot", "toen"],
    ["house", "sohs"],
    ["how", "woni"],
    ["hungry ", "nuhryg"],
    ["I", "ni"],
    ["in", "nis"],
    ["is", "si"],
    ["it", "jina"],
    ["job", "obij"],
    ["just", "stuj"],
    ["know", "peco"],
    ["lay", "yali"],
    ["leaf", "flekin"],
    ["life", "tiva"],
    ["light", "gith"],
    ["like", "keyan"],
    ["little", "tili"],
    ["look", "okol"],
    ["loopy", "pyloo"],
    ["lot", "tolin"],
    ["love", "lolam"],
    ["Me", "mah"],
    ["Monday", "donmy"],
    ["moon", "noom"],
    ["morning", "minorg"],
    ["mother", "diha"],
    ["Myself", "glof"],
    ["nature", "renta"],
    ["my", "ni"],
    ["night", "tinta"],
    ["nine", "nin"],
    ["no", "na"],
    ["not", "dah"],
    ["of", "fon"],
    ["ok", "kona"],
    ["on", "la"],
    ["one", "na"],
    ["or", "vel"],
    ["our", "ro"],
    ["out", "tu"],
    ["pants", "saytah"],
    ["plant", "tanlep"],
    ["power", "repo"],
    ["pretty", "typra"],
    ["pull", "teef"],
    ["pyrimid", "primy"],
    ["right", "nogra"],
    ["royalty", "lortai"],
    ["sad", "dashinga"],
    ["same", "nill"],
    ["Saturday", "dusty"],
    ["say", "oros"],
    ["school", "skol"],
    ["seven", "vensa"],
    ["she", "esh"],
    ["shirt", "showana"],
    ["shorts", "showta"],
    ["sick", "keena"],
    ["silly", "silin"],
    ["sister", "soma"],
    ["six", "ix"],
    ["skirt", "skowana"],
    ["smell", "lesh"],
    ["so", "nas"],
    ["song ", "sara"],
    ["sorry", "trislus/tris"],
    ["start", "sar"],
    ["sun", "nus"],
    ["Sunday", "dunsy"],
    ["thankful", "nemus"],
    ["thankyou", "nemoy/nem"],
    ["the", "eth"],
    ["there", "rethe"],
    ["this", "ith"],
    ["three", "ret"],
    ["through", "ruh"],
    ["throw", "wot"],
    ["Thursday", "dusthy"],
    ["to", "ot/oto"],
    ["toward", " ervas"],
    ["true ", "resai"],
    ["Tuesday", "desty"],
    ["two", "twon"],
    ["until", "ad"],
    ["up", "puin"],
    ["very", "revy"],
    ["warm", "mar"],
    ["was", "aws"],
    ["Water", "retwa"],
    ["Wednesday", "denwy"],
    ["wet", "tweh"],
    ["when", "lin"],
    ["with", "nai"],
    ["word", "darin"],
    ["would", "delow"],
    ["write", "aiter"],
    ["yes", "weys"],
    ["you", "oy"],
    ["your", "ory"],
    ["zero", "roz"],
    ["call", "vor"],
    
    
    
    ];
    
    var textInput;
    var str;
    var direction;
    
    function checkText(str)
    {
    	str = document.getElementById('txtIn').value.toLowerCase();
    	direction = document.getElementById('directionSelect').selectedIndex;
    	if(direction==0)
    	{
    		for(i=0; i < wordPairs.length; i++)
    		{
    			str = str.replace(wordPairs[i][0],wordPairs[i][1]);
    		}
    	}
    	else
    	{
    		for(i=0; i < wordPairs.length; i++)
    		{
    			str = str.replace(wordPairs[i][1],wordPairs[i][0]);
    		}
    	}
    	
    	document.getElementById('result').innerHTML = str;
    	
    }
    
    
    window.onload=function()
    {
    	document.getElementById('convertBtn').onclick=checkText;
    }
    </script>
    <body background="Magic Forest.jpg">
    <input type="text" id="txtIn" />
    <button id="convertBtn">Translate</button>
    <select id="directionSelect">
    <option>English to Ideenian</option>
    <option>Ideenian to English</option>
    </select>
    <div id="result"></div> 
    </center></p>
    </body>
    </html>

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You are using replace incorrectly. If you replace (part of) the value of str then that new value of str becomes the text to replace on the next iteration of the loop. water > wsater > wstayer. Do it like this (you want to find/replace whole words only):-

    Code:
    function checkText() {
    var str = document.getElementById('txtIn').value.toLowerCase();
    var direction = document.getElementById('directionSelect').selectedIndex;
    var newstr = "Not Found";
    
    if (direction==0)  {	
    for(i=0; i < wordPairs.length; i++) {
    if (str == wordPairs[i][0].toLowerCase()) {
    newstr = wordPairs[i][1];
    }
    }
    }
    
    else {	
    for(i=0; i < wordPairs.length; i++) {
    if (str == wordPairs[i][1].toLowerCase()) {
    newstr = wordPairs[i][0];
    }
    }
    }
    	
    document.getElementById('result').innerHTML = newstr;
    	
    }
    This translates just a single word (but only the noun singular, not the plural e.g. eggs). If you want to translate whole sentences you will need to use something like Old Pedant's script. But like your script that only translates the infinitive of the verb. If "to see" = "lkl-kasa-ik’kikn-ak", what is "I saw" or "I have seen"?


    BTW, ask and bee (and quite a few others) return "Not Found". I will leave you to work out why.



    Roger Boyes probes Vienna Boys’ Choir - Times OnLine
    Last edited by Philip M; 12-26-2012 at 12:30 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •