如何音译c#.net 4.0? [英] How to transliterate on c# .net 4.0 ?

查看:66
本文介绍了如何音译c#.net 4.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手。这些是我的代码:

I am new to programming. These are my codes:

public string ThanglishToTamilList(char[] characters, int length) {
        var dict1 = new Dictionary<string, string>();

        dict1.Add("a", "\u0B85"); //
        dict1.Add("aa", "\u0B86"); //
        dict1.Add("A", "\u0B86"); //
        dict1.Add("i", "\u0B87"); //
        dict1.Add("ee", "\u0B88"); //
        dict1.Add("I", "\u0B88"); //
        dict1.Add("u", "\u0B89"); //
        ...



        List<String> list = new List<String>();
        string[] array;
        var valueOfDictOne = "";

        for (int i = 0; i < length; i++)
        {
            try
            {
                valueOfDictOne = dict1[characters[i].ToString()];
                list.Add(valueOfDictOne);

            }
            catch
            {
                list.Add(characters[i].ToString());
            }
        }

        array = list.ToArray();
        string result = string.Join("", array);
        return result;
    }



功能参数详情:



char [] characters:字符数组(textbox.text.ToCharArray())



int length:数组的长度。 (没有我们在文本框中输入的字符)



我的预期输出应为:



如果用户输入 - >输出应为அ。



同样:



a - > அ



aa - > ஆ



A - > ஆ...



请注意aa& A代表相同ஆ



我的问题:此代码仅替换一个字符(a - >அ),这样可以正常工作。



但是如果输入aa,则输出为அஅ



aa - > அஅ



但我需要正确的输出



aa - > ஆ



我为此添加了一些代码行。但这不起作用:


function Parameter details:

char[] characters : Array of characters (textbox.text.ToCharArray())

int length : length of the array. (no of characters we typed in the text box)

My expected output should be:

If the user types a -> Output should be அ.

Likewise:

a -> அ

aa -> ஆ

A -> ஆ ...

note that aa & A represent same ஆ

My Problem: This code only replace one charecter (a -> அ), This works fine.

But if we type aa the output is அஅ

aa -> அஅ

But I need the correct output as

aa -> ஆ

I have added some lines of codes for this. but this did not work:

...
       for (int i = 0; i < length; i++)
       {
           try
           {

               if (String.Equals(characters[i], "a") && !(String.Equals(characters[i], "aa")))
               {

                   //MessageBox.Show("a");

                   valueOfDictOne = dict1[characters[i].ToString()];
                   list.Add(valueOfDictOne);
               }
               else if (String.Equals(characters[i], "aa"))
               {
                   //MessageBox.Show("aa");

                   valueOfDictOne = dict1[characters[i].ToString()];
                   list.Add(valueOfDictOne);
               }

           }
           catch
           {
               list.Add(characters[i].ToString());
           }
       }




Please help me to correct this code or please provide any easy alternative ways to transliterate.

Thank you.

推荐答案

问题是您的代码非常简单:您找到第一个匹配和它一起去吧。这真的不行。您不需要通过字符串单独处理每个字符,而是需要处理有效替换列表,这意味着您还需要在列表中引入一个订单。例如,在开始查看a的任何示例之前,您需要在字符串中处理aa的所有示例,否则后者将始终优先。



例如,你需要做的是:

The problem is that your code is very simplistic: you find the first match and go with it. which really won''t work. Instead of processing each character individually though the string, you need to process the list of valid replacements instead, and that means that you need to introduce an order to your list as well. For example, you need to process all the examples of "aa" in your string before you start looking at any examples of "a", or the later will always get precedence.

For example, what you need to do is along the lines of:
string s = characters.ToSting();
s = s.Replace("aa", "\u0B86"");
s = s.Replace("a", "\u0B85");





我认为你需要坐下来重新思考,因为字典不是一个好的选择 - 值的顺序取决于你插入它们的顺序,你需要控制命令的顺序。





BTW:依靠try catch进行正常处理是一种非常糟糕的做法 - 处理时间非常昂贵在尝试代码之前检查问题。



I think you need to sit down and rethink, because a dictionary is not a good choice for this - the order of values is determined by the order in which you insert them, and you need control over the ordering of your commands.


BTW: It is a very bad practice to rely on try catch for normal processing - it is very expensive in processing time compared to checking for problems before you try the code.


请考虑我在过去的答案中建议的替代方案:输入印地语和英语的问题 [ ^ ]。



您怎么看?问题是:泰米尔语有超过60个字符。我解释了它是如何解决的。



-SA
Please consider the alternative I suggested in my past answer: Problem in typing in Hindi and english[^].

What do you think? The problem is: Tamil has over 60 characters. I explained how it could be worked around.

—SA


这篇关于如何音译c#.net 4.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆