How can I convert Cyrillic string into English in c#

前端 未结 10 957
醉酒成梦
醉酒成梦 2020-12-31 21:05

Is it posible to convert Cyrillic string to English(Latin) in c#? For example I need to convert \"Петролеум\" in \"Petroleum\". Plus I forgot to mention that if I have Cyril

相关标签:
10条回答
  • 2020-12-31 21:33

    You can of course map the letters to the latin transcription, but you won't get an english word out of it in most cases. E.g. Российская Федерация transcribes to Rossiyskaya Federatsiya. wikipedia offers an overview of the mapping. You are probably looking for a translation service, google probably offers an api for that.

    0 讨论(0)
  • 2020-12-31 21:33

    Why do you want to do this? Changing characters one-for-one generally doesn't even produce a reasonable transliteration, much less a translation. You may find this post to be of interest.

    0 讨论(0)
  • 2020-12-31 21:40

    You are searching for a way of translitterating russian words written in cirillic (in some encodings, e.g. even a Latin encoding, since iso 8859-5 aka Latin-5 is for cyrillic) into latin alphabet (with accents)?

    I don't know if .NET has something to transliterate, but I dare say it (as many other good frameworks) hasn't. This wikipedian link could give you some ideas to implement translitteration, but it is not the only way and remember tha cyrillic writing systems is not used by russian only and the way you apply translitteration may vary on the language that use the writing system. E.g. see the same for bulgarian. May this link (always from wp) can be also interesting if you want to program the translitterator by yourself.

    0 讨论(0)
  • 2020-12-31 21:41

    You can use text.Replace(pair.Key, pair.Value) function.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Transliter
    {
        public partial class Form1 : Form
        {
            Dictionary<string, string> words = new Dictionary<string, string>();
    
            public Form1()
            {
                InitializeComponent();
                words.Add("а", "a");
                words.Add("б", "b");
                words.Add("в", "v");
                words.Add("г", "g");
                words.Add("д", "d");
                words.Add("е", "e");
                words.Add("ё", "yo");
                words.Add("ж", "zh");
                words.Add("з", "z");
                words.Add("и", "i");
                words.Add("й", "j");
                words.Add("к", "k");
                words.Add("л", "l");
                words.Add("м", "m");
                words.Add("н", "n");
                words.Add("о", "o");
                words.Add("п", "p");
                words.Add("р", "r");
                words.Add("с", "s");
                words.Add("т", "t");
                words.Add("у", "u");
                words.Add("ф", "f");
                words.Add("х", "h");
                words.Add("ц", "c");
                words.Add("ч", "ch");
                words.Add("ш", "sh");
                words.Add("щ", "sch");
                words.Add("ъ", "j");
                words.Add("ы", "i");
                words.Add("ь", "j");
                words.Add("э", "e");
                words.Add("ю", "yu");
                words.Add("я", "ya");
                words.Add("А", "A");
                words.Add("Б", "B");
                words.Add("В", "V");
                words.Add("Г", "G");
                words.Add("Д", "D");
                words.Add("Е", "E");
                words.Add("Ё", "Yo");
                words.Add("Ж", "Zh");
                words.Add("З", "Z");
                words.Add("И", "I");
                words.Add("Й", "J");
                words.Add("К", "K");
                words.Add("Л", "L");
                words.Add("М", "M");
                words.Add("Н", "N");
                words.Add("О", "O");
                words.Add("П", "P");
                words.Add("Р", "R");
                words.Add("С", "S");
                words.Add("Т", "T");
                words.Add("У", "U");
                words.Add("Ф", "F");
                words.Add("Х", "H");
                words.Add("Ц", "C");
                words.Add("Ч", "Ch");
                words.Add("Ш", "Sh");
                words.Add("Щ", "Sch");
                words.Add("Ъ", "J");
                words.Add("Ы", "I");
                words.Add("Ь", "J");
                words.Add("Э", "E");
                words.Add("Ю", "Yu");
                words.Add("Я", "Ya");
        }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string source = textBox1.Text;
                foreach (KeyValuePair<string, string> pair in words)
                {
                    source = source.Replace(pair.Key, pair.Value);
                }
                textBox2.Text = source;
            }
        }
    }
    

    If you change

    cryllic to latin:

    text.Replace(pair.Key, pair.Value); 
    

    latin to cryllic

    source.Replace(pair.Value,pair.Key);
    
    0 讨论(0)
  • 2020-12-31 21:45

    This method is very fast:

    static string[] CyrilicToLatinL = 
      "a,b,v,g,d,e,zh,z,i,j,k,l,m,n,o,p,r,s,t,u,f,kh,c,ch,sh,sch,j,y,j,e,yu,ya".Split(',');
    static string[] CyrilicToLatinU = 
      "A,B,V,G,D,E,Zh,Z,I,J,K,L,M,N,O,P,R,S,T,U,F,Kh,C,Ch,Sh,Sch,J,Y,J,E,Yu,Ya".Split(',');
    
    public static string CyrilicToLatin(string s)
    {
      var sb = new StringBuilder((int)(s.Length * 1.5));
      foreach (char c in s)
      {
        if (c >= '\x430' && c <= '\x44f') sb.Append(CyrilicToLatinL[c - '\x430']);
        else if (c >= '\x410' && c <= '\x42f') sb.Append(CyrilicToLatinU[c - '\x410']);
        else if (c == '\x401') sb.Append("Yo");
        else if (c == '\x451') sb.Append("yo");
        else sb.Append(c);
      }
      return sb.ToString();
    }
    
    0 讨论(0)
  • 2020-12-31 21:47

    Use a Dictionary with russian and english words as a lookup table. It'll be a lot of typing to build it, but it's full proof.

    0 讨论(0)
提交回复
热议问题