예제 #1
0
        public static string ToRomaji(string input, WanaKanaOptions?options = null)
        {
            if (input is null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            options ??= new WanaKanaOptions();

            if (input.Length == 0)
            {
                return(string.Empty);
            }

            // just throw away the substring index information and just concatenate all the kana
            var romajiTokens = SplitIntoRomaji(input, options);
            var builder      = new StringBuilder();

            foreach (var token in romajiTokens)
            {
                var(start, end, romaji) = token;
                var makeUppercase = options.UppercaseKatakana && KatakanaChecker.IsKatakana(input.Substring(start, end - start));
                builder.Append(makeUppercase ? romaji?.ToUpperInvariant() ?? string.Empty : romaji);
            }
            return(builder.ToString());
        }
예제 #2
0
        public static string KatakanaToHiragana(string input, bool isDestinationRomaji = false)
        {
            if (input is null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            char?previousKana = null;
            var  builder      = new StringBuilder();

            for (int index = 0; index < input.Length; index++)
            {
                var character = input[index];

                // Short circuit to avoid incorrect codeshift for 'ー' and '・'
                if (SpecialCharacterChecker.IsSlashDot(character) || IsInitialLongDash(character, index) || IsKanaAsSymbol(character))
                {
                    builder.Append(character);
                    // Transform long vowels: 'オー' to 'おう'
                }
                else if (previousKana != null && IsInnerLongDash(character, index))
                {
                    // Transform previousKana back to romaji, and slice off the vowel
                    var romaji = RomajiConverter.ToRomaji(previousKana.ToString()).Last();
                    // However, ensure 'オー' => 'おお' => 'oo' if this is a transform on the way to romaji
                    if (KatakanaChecker.IsKatakana(input[index - 1]) && romaji == 'o' && isDestinationRomaji)
                    {
                        builder.Append('お');
                    }
                    else
                    {
                        builder.Append(_longVowels[romaji]);
                    }
                }
                else if (!SpecialCharacterChecker.IsLongDash(character) && KatakanaChecker.IsKatakana(character))
                {
                    // Shift charcode.
                    var hiraganaCode = (character - CharacterRanges.KatakanaStart) + CharacterRanges.HiraganaStart;
                    var hiragana     = (char)hiraganaCode;
                    previousKana = hiragana;
                    builder.Append(hiragana);
                }
                else
                {
                    // Pass non katakana chars through
                    previousKana = null;
                    builder.Append(character);
                }
            }
            return(builder.ToString());
        }
예제 #3
0
 /// <summary>
 /// Test if input is katakana.
 /// </summary>
 /// <param name="input">Input.</param>
 /// <returns>A value indicating whether input is katakana.</returns>
 public static bool IsKatakana(string input) => KatakanaChecker.IsKatakana(input);
예제 #4
0
 private static string GetTokenType(char character, bool compact = false)
 {
     if (compact)
     {
         if (IsJaNumber(character))
         {
             return(TokenTypes.Other);
         }
         if (IsEnNumber(character))
         {
             return(TokenTypes.Other);
         }
         if (IsEnSpace(character))
         {
             return(TokenTypes.English);
         }
         if (SpecialCharacterChecker.IsEnglishPunctuation(character))
         {
             return(TokenTypes.Other);
         }
         if (IsJaSpace(character))
         {
             return(TokenTypes.Japanese);
         }
         if (SpecialCharacterChecker.IsJapanesePunctuation(character))
         {
             return(TokenTypes.Other);
         }
         if (JapaneseChecker.IsJapanese(character))
         {
             return(TokenTypes.Japanese);
         }
         if (RomajiChecker.IsRomaji(character))
         {
             return(TokenTypes.English);
         }
     }
     else
     {
         if (IsJaSpace(character))
         {
             return(TokenTypes.Space);
         }
         if (IsEnSpace(character))
         {
             return(TokenTypes.Space);
         }
         if (IsJaNumber(character))
         {
             return(TokenTypes.JapaneseNumber);
         }
         if (IsEnNumber(character))
         {
             return(TokenTypes.EnglishNumber);
         }
         if (SpecialCharacterChecker.IsEnglishPunctuation(character))
         {
             return(TokenTypes.EnglishPunctuation);
         }
         if (SpecialCharacterChecker.IsJapanesePunctuation(character))
         {
             return(TokenTypes.JapanesePunctuation);
         }
         if (KanjiChecker.IsKanji(character))
         {
             return(TokenTypes.Kanji);
         }
         if (HiraganaChecker.IsHiragana(character))
         {
             return(TokenTypes.Hiragana);
         }
         if (KatakanaChecker.IsKatakana(character))
         {
             return(TokenTypes.Katakana);
         }
         if (JapaneseChecker.IsJapanese(character))
         {
             return(TokenTypes.Japanese);
         }
         if (RomajiChecker.IsRomaji(character))
         {
             return(TokenTypes.English);
         }
     }
     return(TokenTypes.Other);
 }
예제 #5
0
 /// <summary>
 /// Test if input is katakana.
 /// </summary>
 /// <param name="input">Input.</param>
 /// <returns>A value indicating whether input is katakana.</returns>
 public static bool IsKatakana(char input) => KatakanaChecker.IsKatakana(input);