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 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Transliteration from English to Kannada

    I have 2 Arrays: english[]={"a", "aa", "i", "ii", . . .}, kannada[]={"\u0043", "\u0044", "\u0045", "\u0046", . . .} and a Textarea to type. Whenever I type the english[x] in the Textarea the corresponding kannada[x] must appear in the textarea. That is all!
    I need a JAVASCRIPT program to transliterate from English to Kannada.
    Any alternative solution welcome!
    Here is a code snippet I use. It tranliterates only the first character. I need the whole Text.

    <HTML>
    <HEAD>
    <TITLE>Kannada Transliteration</TITLE>
    <p style="font-family:Arial;font-size:20pt;color:red;">
    <SCRIPT language="JavaScript">
    <!--//
    function myFunction(){
    var x=document.getElementById("str");

    if(x.value=="a") (x.value = "\u0043");
    if(x.value=="A") x.value = "\u0044";
    if(x.value=="i") x.value = "\u0045";
    if(x.value=="I") x.value = "\u0046";
    if(x.value=="u") x.value = "\u0047";
    if(x.value=="U") x.value = "\u0048";

    }

    //-->
    </SCRIPT>
    </HEAD>
    <BODY>
    Enter Some Text: <BR><BR>
    <textarea name="tarea" rows="5"
    cols="16" id="str"
    style="font-family:BRH JHK01;font-
    size:24pt;color:blue;"
    onkeyup="myFunction()"></textarea>
    </BODY>
    </HTML>

    Thanks in advance.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    I have the idea that this is homework. You need to use regular expressions. No arrays required.

    Code:
    
    <script type = "text/javascript">
    
    function myFunction() {
    
    var x=document.getElementById("str").value;
    x = x.replace(/a$/,"\u0043");   // $ means the character at the end of the string.  So if you change a to b, b will not 
    // change again when another b is entered which you want to change to c.
    x = x.replace(/A$/,"\u0044");
    x = x.replace(/i$/, "\u0045");
    x = x.replace(/I$/, "\u0046");
    x = x.replace(/u$/, "\u0047");
    x = x.replace(/U$/, "\u0048");
    document.getElementById("str").value = x;
    }
    
    </script>
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9). The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    The Unicode Kannada characters may be found at http://en.wikipedia.org/wiki/Kannada_language, but I doubt if there is a one-for-one correspondence so as to permit simple translation/substitution.



    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    Last edited by Philip M; 08-21-2014 at 09:23 AM. Reason: Typo

    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    But this whole scheme falls apart because of the DOUBLE English letters that transliterate into a single Kannada character. "aa" and "ii" in the example given.
    (If you substitute for "a" first then you won't be able to find "aa".)

    So a better way might be to retain the English letter version in a hidden textarea or even JavaScript variable and then do the transliteration but attacking the double letters *first*.

    One problem in all this: What about edits performed by the user? Example: The user uses the mouse to highlight 3 Kannada *words* and then hits the DELETE key. What will you do then? Or, in Philip's method, say the user moves the cursor to the middle of an existing Kannada word and then types "aa". Nothing will change because Philip's code only changes characters at the end of the text.

    This is *NOT* an easy task. If it is homework, then you need to read carefully the ENTIRE assignment to make sure you can do everything required.
    Last edited by Old Pedant; 08-21-2014 at 08:42 PM.
    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.

  • #4
    New Coder
    Join Date
    Aug 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Transliteration from English to Kannada

    Hi Old Pedant,
    Double character convention like "aa", "ii", "ee"" is possible in Kannada transliteration.
    Please visit Online Kannada Converter | English Kannada Translation | Kannada Conversion
    Try typing some characters.
    =mbasanna

  • #5
    New Coder
    Join Date
    Aug 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Mr Peter.
    The program works fine, but for a SINGLE CHARACTER.
    How can it be modified to replace a WORD?

    Please visit: Online Kannada Converter | English Kannada Translation | Kannada Conversion
    Type your name, place, etc.
    See how it changes to Kannada.
    I need a similar one!
    Thanks again,
    =mbasanna.

  • #6
    New Coder
    Join Date
    Aug 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Mr Phillip.
    The program works fine, but for a SINGLE CHARACTER.
    How can it be modified to replace a WORD?

    Please visit: Online Kannada Converter | English Kannada Translation | Kannada Conversion
    Type your name, place, etc.
    See how it changes to Kannada.
    I need a similar one!
    Thanks again,
    =mbasanna.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    As Old Pedant says, "This is *NOT* an easy task". In principle you need to split the text into an array of words (delimited by a space) and then tranliterate the characters in each word starting with the characters which translate into the double letters aa, ii etc. As I know nothing about Kannada I am not able to offer further assistance.

    Philip M becomes ಫಿಲಿಪ್ ಮ್ in Kannada.

    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.

  • #8
    New Coder
    Join Date
    Aug 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mr Philip,
    It is not a difficult task as you expect.
    It is extremely simle and possible.
    But I do not know how to put it.
    Here is a code snippet that may help you.

    <center>
    <!DOCTYPE html>
    <html>
    <body>
    <p>Click the button <BR>
    to replace<BR>
    "aa" with "\u0c86":

    <p id="demo">aa </p>

    <button onclick="myFunction()">Click Me</button>
    <script>
    function myFunction() {
    var str = document.getElementById("demo").innerHTML;
    var res = str.replace("aa", "\u0C86");

    document.getElementById("demo").innerHTML = res;
    }
    </script>

    </body>
    </html>

    Please check it and get me appropriate code.
    Thank you
    = mbasanna

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You seem very unclear as to what is is you want. You asked for translate letters, then whole words, then back to letters.
    And your syntax is wrong - var res = str.replace("aa", "\u0C86");
    should be var res = str.replace(/aa/g, "\u0C86");

    Glad to know that you think this is extremely simple and possible. Good luck! Over and out.

    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.

  • #10
    New Coder
    Join Date
    Aug 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK! Mr Philip
    I am a learner and hobbyist.
    I needed a program to transliterate TEXT.
    · Here, I have improved your Program.
    · It contains of 2 types of statements.
    · Slashed (//) for Single char and Unslashed for multiple chars.
    · If I use Slashed statements the Unslashed do not work.
    · If I use only Unslashed they do work.
    · How can I make both of them work?
    The program is given below:
    <HTML>
    <HEAD>
    <TITLE>Kannada Transliteration</TITLE>
    <p style="font-family:Arial;font-size:20pt;color:red;">
    <script type = "text/javascript">

    function myFunction(){

    var x=document.getElementById("str").value;

    //x = x.replace(/a/,"\u0043");
    x = x.replace(/aa/,"\u0044");
    //x = x.replace(/i/, "\u0045");
    x = x.replace(/ii/, "\u0046");
    //x = x.replace(/u/, "\u0047");
    x = x.replace(/uu/, "\u0048");

    //x = x.replace(/k/,"\u0050\u00ef");
    x = x.replace(/ka/,"\u0050\u00C0");

    x = x.replace(/kSha/, "\u0050\u00ef\u00b5\u00c0");

    document.getElementById("str").value = x;
    }
    </script>
    </HEAD>
    <BODY>

    Enter Some Text: <BR><BR>
    <textarea name="tarea" rows="5" cols="16" id="str"
    style="font-family:BRH JHK01;font-size:24pt;color:blue;"
    onkeyup="myFunction()"></textarea>
    </BODY>
    </HTML>

    With Thanks
    =mbasanna

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    I *TOLD* you what was wrong with that!

    You *MUST* do the LONGER sequences FIRST!

    Code:
    function myFunction()
    {
        var x=document.getElementById("str").value;
        // 4 letters first (or 5 or 6 if any exist) 
        x = x.replace(/kSha/g, "\u0050\u00ef\u00b5\u00c0");
        // 3 letters if any
        // 2 letters
        x = x.replace(/ka/g,"\u0050\u00C0");
        x = x.replace(/uu/g, "\u0048");
        x = x.replace(/ii/g, "\u0046");
        x = x.replace(/aa/g,"\u0044");
        // finally single letters
        x = x.replace(/a/g,"\u0043"); 
        x = x.replace(/i/g, "\u0045");
        x = x.replace(/u/g, "\u0047");
        x = x.replace(/k/g,"\u0050\u00ef"); 
    
        document.getElementById("str").value = x;
    }
    Notice that all the regular expressions end with /g.
    That means "global" so that *ALL* occurrences of the pattern will be replaced, not just the first one.
    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.


  •  

    Posting Permissions

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