none
Converting Hindi Text to English Text

    Question

  • Hi,

    Can someone guide me in converting hindi text to english text using c#.  The application will be windows based.  When I googled it says it will be possible using google/translate.  However I am not aware of how to use that in my application.

    Any ideas?

    Regards,
    Karthizen
    • Edited by Karthizen Friday, August 28, 2009 5:05 AM No signature
    Friday, August 28, 2009 5:05 AM

Answers

  • Hi Karthizen,

    Based on my understanding, you want to directly convert a hindi character to an english character(characters). You said that we can build a map to do that. Yes, this is a good idea. We can define a static dictionary to store the mapping and then use it to transliterate. This is the code snippet:
    A static class provide some transliterate methods:
        public static class CharacterTransliterate
        {
            //The dictionary used to store the character map:
            //'hindi_character -> english_character' pairs
            private static Dictionary<char, string> _transDic = null;
    
            //Get the character map.
            public static Dictionary<char, string> TransDic
            {
                get
                {
                    if (_transDic == null)
                    {
                        _transDic = new Dictionary<char, string>();
                        //Add some pairs.
                        _transDic.Add('अ', "A");
                        _transDic.Add('आ', "Aa");
                        _transDic.Add('इ', "I");
                        _transDic.Add('ई', "Ee");
                        //Not finished.
                        //Add other 'hindi_character -> english_character' pair here
                    }
                    return _transDic;
                }
            }
            //Convert a hindi character to english character.
            public static string GetEnglishCharacter(char hindiCharacter)
            {
                return TransDic[hindiCharacter];
            }
    
            //Convert a hindi character to english character.
            public static string GetEnglishString(string hindiString)
            {
                //Traverse the characters and convert each character, then connect them to a string.
                StringBuilder builder = new StringBuilder();
                foreach (char character in hindiString)
                {
                    if (TransDic.ContainsKey(character))
                        builder.Append(TransDic[character]);
                    else
                        builder.Append(character);
                }
                return builder.ToString();
            }
        }
    


    A example shows how to use the static class:
            private void button3_Click(object sender, EventArgs e)
            {
                //Convert hindi string to a english string.
                this.richTextBox1.Text = CharacterTransliterate.GetEnglishString(this.richTextBox1.Text);
            }
    You can get more information about hindi-english mapping from these links:
    http://www.xs4all.nl/~wjsn/hindi.htm 
    http://unicode.org/charts/PDF/U0900.pdf 
    http://www.alanwood.net/unicode/devanagari.html

    Let me know if this helps.
    Aland Li 
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Karthizen Wednesday, September 02, 2009 5:58 AM
    Monday, August 31, 2009 12:27 PM

All replies

  • this will probably get you started.

    Friday, August 28, 2009 6:51 AM
  • Hi Ray,

    Thank you for your reply.  Let me have a look at it & update you on this.

    Regards,
    Karthik

    Friday, August 28, 2009 6:56 AM
  • Well,  I need to transliterate and not translate the text typed in Hindi.  The link given by Ray will be useful for translation.

    Is there any APIs available for transliteration? 


    Regards,
    Karthik

    Friday, August 28, 2009 7:30 AM
  • I have asked one of my friends, he said for each combination of Hindi letter, we need to create a Hashmap with respective combination of English letters. And then to create a function which will read the complete Hindi text and refer to the hashmap and then transliterate one letter at a time.

    Well, how to create a hashmap?

    Friday, August 28, 2009 12:02 PM
  • Hi Karthizen,

    Based on my understanding, you want to directly convert a hindi character to an english character(characters). You said that we can build a map to do that. Yes, this is a good idea. We can define a static dictionary to store the mapping and then use it to transliterate. This is the code snippet:
    A static class provide some transliterate methods:
        public static class CharacterTransliterate
        {
            //The dictionary used to store the character map:
            //'hindi_character -> english_character' pairs
            private static Dictionary<char, string> _transDic = null;
    
            //Get the character map.
            public static Dictionary<char, string> TransDic
            {
                get
                {
                    if (_transDic == null)
                    {
                        _transDic = new Dictionary<char, string>();
                        //Add some pairs.
                        _transDic.Add('अ', "A");
                        _transDic.Add('आ', "Aa");
                        _transDic.Add('इ', "I");
                        _transDic.Add('ई', "Ee");
                        //Not finished.
                        //Add other 'hindi_character -> english_character' pair here
                    }
                    return _transDic;
                }
            }
            //Convert a hindi character to english character.
            public static string GetEnglishCharacter(char hindiCharacter)
            {
                return TransDic[hindiCharacter];
            }
    
            //Convert a hindi character to english character.
            public static string GetEnglishString(string hindiString)
            {
                //Traverse the characters and convert each character, then connect them to a string.
                StringBuilder builder = new StringBuilder();
                foreach (char character in hindiString)
                {
                    if (TransDic.ContainsKey(character))
                        builder.Append(TransDic[character]);
                    else
                        builder.Append(character);
                }
                return builder.ToString();
            }
        }
    


    A example shows how to use the static class:
            private void button3_Click(object sender, EventArgs e)
            {
                //Convert hindi string to a english string.
                this.richTextBox1.Text = CharacterTransliterate.GetEnglishString(this.richTextBox1.Text);
            }
    You can get more information about hindi-english mapping from these links:
    http://www.xs4all.nl/~wjsn/hindi.htm 
    http://unicode.org/charts/PDF/U0900.pdf 
    http://www.alanwood.net/unicode/devanagari.html

    Let me know if this helps.
    Aland Li 
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Karthizen Wednesday, September 02, 2009 5:58 AM
    Monday, August 31, 2009 12:27 PM
  • Hi Anand Li,

    Thats works very well.  Let me add the other character set & Update on further outcome.

    Thank you so much,

    Regards,
    karthik
    Wednesday, September 02, 2009 5:59 AM
  • Hi Karthizen,

    You are welcome.

    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Wednesday, September 02, 2009 6:00 AM
  • please edit this code so that i can use the same to convert english to hindi
    Monday, August 30, 2010 6:39 AM
  • how i convert english to hindi
    Monday, August 30, 2010 7:10 AM
  • Hi Karthizen,

    Based on my understanding, you want to directly convert a hindi character to an english character(characters). You said that we can build a map to do that. Yes, this is a good idea. We can define a static dictionary to store the mapping and then use it to transliterate. This is the code snippet:
    A static class provide some transliterate methods:
     public static class CharacterTransliterate
    {
    //The dictionary used to store the character map:
    //'hindi_character -> english_character' pairs
    private static Dictionary<char, string> _transDic = null;

    //Get the character map.
    public static Dictionary<char, string> TransDic
    {
    get
    {
    if (_transDic == null)
    {
    _transDic = new Dictionary<char, string>();
    //Add some pairs.
    _transDic.Add('अ', "A");
    _transDic.Add('आ', "Aa");
    _transDic.Add('इ', "I");
    _transDic.Add('ई', "Ee");
    //Not finished.
    //Add other 'hindi_character -> english_character' pair here
    }
    return _transDic;
    }
    }
    //Convert a hindi character to english character.
    public static string GetEnglishCharacter(char hindiCharacter)
    {
    return TransDic[hindiCharacter];
    }

    //Convert a hindi character to english character.
    public static string GetEnglishString(string hindiString)
    {
    //Traverse the characters and convert each character, then connect them to a string.
    StringBuilder builder = new StringBuilder();
    foreach (char character in hindiString)
    {
    if (TransDic.ContainsKey(character))
    builder.Append(TransDic[character]);
    else
    builder.Append(character);
    }
    return builder.ToString();
    }
    }



    A example shows how to use the static class:
      private void button3_Click(object sender, EventArgs e)
    {
    //Convert hindi string to a english string.
    this.richTextBox1.Text = CharacterTransliterate.GetEnglishString(this.richTextBox1.Text);
    }
    You can get more information about hindi-english mapping from these links:
    http://www.xs4all.nl/~wjsn/hindi.htm 
    http://unicode.org/charts/PDF/U0900.pdf 
    http://www.alanwood.net/unicode/devanagari.html

    Let me know if this helps.
    Aland Li 
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

    hey anand li,

    you code was quite a help for me,can you please explain how would you add special hindi characters to your language mapping and append them while you covert the word/sentence ....

    thanks...

    help would be appreciated

    this is my code:

    public class Class1
        {
            Dictionary<string, string> _convert = new Dictionary<string, string>();
            public Class1()
            {
                _convert.Add("a", "\u0904");
                _convert.Add("A", "\u0905");
                _convert.Add("aa", "\u0906");
                _convert.Add("i", "\u0907");
                _convert.Add("ii", "\u0908");
                _convert.Add("u", "\u0909");
                _convert.Add("uu", "\u090A");
                _convert.Add("r", "\u090B");
                _convert.Add("L", "\u090C");
                _convert.Add("E", "\u090D");
                _convert.Add("e", "\u090E");
                //_convert.Add("E", "\u090F");
                _convert.Add("ai", "\u0910");
                //_convert.Add("O", "\u0911");
                _convert.Add("o", "\u0912");
                //_convert.Add("O", "\u0913");
                _convert.Add("au", "\u0914");
                _convert.Add("ka", "\u0915");
                _convert.Add("kh", "\u0916");
                _convert.Add("ga", "\u0917");
                _convert.Add("gha", "\u0918");
                _convert.Add("nga", "\u0919");
                _convert.Add("ca", "\u091A");
                _convert.Add("cha", "\u091B");
                _convert.Add("ja", "\u091C");
                _convert.Add("jha", "\u091D");
                _convert.Add("nya", "\u091E");
                _convert.Add("tta", "\u091F");
                _convert.Add("ttha", "\u0920");
                _convert.Add("dda", "\u0921");
                _convert.Add("ddha", "\u0922");
                _convert.Add("nna", "\u0923");
                _convert.Add("ta", "\u0924");
                _convert.Add("tha", "\u0925");
                _convert.Add("da", "\u0926");
                _convert.Add("dha", "\u0927");
                _convert.Add("na", "\u0928");
                _convert.Add("nnna", "\u0921");
                _convert.Add("pa", "\u092A");
                _convert.Add("pha", "\u092B");
                _convert.Add("ba", "\u092C");
                _convert.Add("bha", "\u092D");
                _convert.Add("ma", "\u092E");
                _convert.Add("ya", "\u092F");
                _convert.Add("ra", "\u0930");
                _convert.Add("rra", "\u0931");
                _convert.Add("la", "\u0932");
                _convert.Add("lla", "\u0933");
                _convert.Add("llla", "\u0934");
                _convert.Add("va", "\u0935");
                _convert.Add("sha", "\u0936");
                _convert.Add("ssa", "\u0937");
                _convert.Add("sa", "\u0938");
                _convert.Add("ha", "\u0939");
                _convert.Add("AA", "\u093E");
                _convert.Add("I", "\u093F");
                _convert.Add("II", "\u0940");
                _convert.Add("U", "\u0941");
                _convert.Add("UU", "\u0942");
                _convert.Add("R", "\u0943");
                _convert.Add("RR", "\u0944");
                _convert.Add("AI", "\u0945");
                _convert.Add("0", "\u0966");
                _convert.Add("1", "\u0967");
                _convert.Add("2", "\u0968");
                _convert.Add("3", "\u0969");
                _convert.Add("4", "\u096A");
                _convert.Add("5", "\u096B");
                _convert.Add("6", "\u096C");
                _convert.Add("7", "\u096D");
                _convert.Add("8", "\u096E");
                _convert.Add("9", "\u096F");
           
            }
           

            public string dic(string a)
            {
                         
                    foreach (KeyValuePair<string, string> kvp in _convert)
                {
                    if (String.Compare(kvp.Key,a)==0)
                    {
                        return kvp.Value;
                    }
                   
                }
                return " ";
            }




        }

    //Calling

    public partial class Form1 : Form
        {
           
           
           
                Class1 x = new Class1();
               
           
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.textBox1.AcceptsReturn = true;
                this.textBox1.AcceptsTab = true;
                this.textBox1.Multiline = true;
                this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;



                label1.Text = textBox1.Text.Length.ToString() ;
               
               

                    this.textBox2.Text = x.dic(textBox1.Text);
               
               
            }


    • Edited by simranjeet.87 Monday, March 21, 2011 5:34 AM edit with name reference
    • Proposed as answer by simranjeet.87 Monday, January 02, 2012 9:57 AM
    Monday, March 21, 2011 5:33 AM