Converting “Bizarre” Chars in String to Roman Char

2019-04-13 19:54发布

I need to be able to convert user input to [a-z] roman characters ONLY (not case sensitive). So, there are only 26 characters that I am interested in.

However, the user can type in any "form" of those characters that they wish. The Spanish "n", the French "e", and the German "u" can all have accents from the user input (which are removed by the program).

I've gotten pretty close with these two extension methods:

    public static string LettersOnly(this string Instring)
    {
        char[] aChar = Instring.ToCharArray();
        int intCount = 0;
        string strTemp = "";

        for (intCount = 0; intCount <= Instring.Length - 1; intCount++)
        {
            if (char.IsLetter(aChar[intCount]) )
            {
                strTemp += aChar[intCount];
            }
        }

        return strTemp;
    }

    public static string RemoveAccentMarks(this string s)
    {
        string normalizedString = s.Normalize(NormalizationForm.FormD);
        StringBuilder sb = new StringBuilder();

        char c;
        for (int i = 0; i <= normalizedString.Length - 1; i++)
        {
            c = normalizedString[i];
            if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.NonSpacingMark)
            {
                sb.Append(c);
            }
        }

        return sb.ToString();
    }

Here is an example test:

string input = "Àlièñ451";
input = input.LettersOnly().RemoveAccentMarks().ToLower();
console.WriteLine(input);

Result: "alien" (as expected)

This works for 99.9% of the cases. However, a few characters seem to pass all of the checks.

For instance, "ß" (a German double-s, I think). This is considered by .Net to be a letter. This is not considered by the function above to have any accent marks... but it STILL isn't in the range of a-z, like I need it to be. Ideally, I could convert this to a "B" or an "ss" (whichever is appropriate), but I need to convert it to SOMETHING in the range of a-z.

Another example, the dipthong ("æ"). Again, .Net considers this a "letter". The function above doesn't see any accent, but again, it isn't in the roman 26 character alphabet. In this case, I need to convert to the two letters "ae" (I think).

Is there an easy way to convert ANY worldwide input to the closest roman alphabet equivalent? It is expected that this probably won't be a perfectly clean translation, but I need to trust that the inputs at FlipScript.com are ONLY getting the characters a-z... and nothing else.

Any and all help appreciated.

1条回答
Emotional °昔
2楼-- · 2019-04-13 20:44

If I were you, I'd create a Dictionary which would contain the mappings from foreign letters to Roman letters. I'd use this for two reasons:

  1. It will make understanding what you want to do easier to someone who is reading your code.
  2. There are a small, finite, number of these special letters so you don't need to worry about maintenance of the data structure.

I'd put the mappings into an xml file then load them into the data structure at run-time. That way, you do not need to modify any code which uses the characters, you only need to specify the mappings themselves.

查看更多
登录 后发表回答