Ejemplo n.º 1
0
        private string ConvertToKatakana(String toKatakana)
        {
            JapaneseKanaConverter converter = new JapaneseKanaConverter();
            String converted = "";

            for (int i = 0; i < toKatakana.Length; i++)
            {
                if (JapaneseKanaClassifier.IsKatakana(Convert.ToString(toKatakana[i])))
                {
                    converted += toKatakana[i];
                }
                else if (JapaneseKanaClassifier.IsHiragana(Convert.ToString(toKatakana[i])))
                {
                    converted += converter.HiraganaToKatakana(Convert.ToString(toKatakana[i]));
                }
                else
                {
                    Console.WriteLine(toKatakana + " " + toKatakana[i] + " " + i);
                    if (JapaneseKanaClassifier.IsKanji(Convert.ToString(toKatakana[i])))
                    {
                        throw new FormatException("Kanji Error");
                    }
                    else
                    {
                        converted += toKatakana[i]; //Latin Characters
                    }
                }
            }
            return(converted);
        }
        public static string KanaToRomaji(String kana)
        {
            if (JapaneseKanaClassifier.ContainsKanji(kana))
            {
                throw new FormatException("String contains kanji");
            }
            JapaneseKanaConverter converter = new JapaneseKanaConverter();

            if (JapaneseKanaClassifier.IsHiragana(kana))
            {
                kana = converter.HiraganaToKatakana(kana);
            }
            String converted = "";

            for (int i = 0; i < kana.Length; i++)
            {
                if (JapaneseKanaClassifier.IsHiragana(kana[i]))
                {
                    converted += converter.HiraganaToKatakana(Convert.ToString(kana[i]));
                }
                else
                {
                    converted += kana[i];
                }
            }
            kana = converted;

            Dictionary <string, string> dictionary = new Dictionary <string, string>();
            Dictionary <string, string> digraph    = new Dictionary <string, string>();

            for (int i = 0; i < romajiKatakanaPairs.Length; i += 2)
            {
                dictionary.Add(romajiKatakanaPairs[i], romajiKatakanaPairs[i + 1]);
            }

            for (int i = 0; i < romajiKatakanaDigraphs.Length; i += 2)
            {
                digraph.Add(romajiKatakanaDigraphs[i], romajiKatakanaDigraphs[i + 1]);
            }


            String temp     = "";
            String modified = "";

            for (int i = 0; i < kana.Length; i++)
            {
                temp = "";
                if (((i + 1) < kana.Length) && dictionary.ContainsKey(Convert.ToString(kana[i])))
                {
                    temp = kana[i] + "" + kana[i + 1];
                    if (digraph.ContainsKey(temp))
                    {
                        modified += digraph[temp];
                        i++;
                    }
                    else if (dictionary.ContainsKey(Convert.ToString(kana[i])))
                    {
                        modified += dictionary[Convert.ToString(kana[i])];
                    }
                }
                else if (dictionary.ContainsKey(Convert.ToString(kana[i])))
                {
                    modified += dictionary[Convert.ToString(kana[i])];
                }
                else if (kana[i] == 'っ' || kana[i] == 'ッ')
                {
                    string buffer = "";
                    if (((i + 1) < kana.Length) && dictionary.ContainsKey(Convert.ToString(kana[i + 1])) && JapaneseKanaClassifier.IsJapaneseScript(Convert.ToString(kana[i + 1])))
                    {
                        buffer   += dictionary[Convert.ToString(kana[i + 1])];
                        modified += buffer[0];
                    }
                    else if ((i + 1) == kana.Length || (((i + 1) < kana.Length) && dictionary.ContainsKey(Convert.ToString(kana[i + 1])) && !JapaneseKanaClassifier.IsJapaneseScript(Convert.ToString(kana[i + 1]))))
                    {
                        modified += "-";
                    }
                }
                else if (kana[i] == 'ー')
                {
                    modified += modified[modified.Length - 1];
                }
                else
                {
                    modified += kana[i];
                }
            }
            return(modified);
        }