public static string ToTraditional(string chinese) { var lexicon = ChineseLexicon.Current; if (lexicon is null) { return(Converter.Convert(chinese, Direction.SimplifiedToTraditional)); } var words = ChineseTokenizer.SplitWords(chinese, ChineseType.Simplified); var sb = new StringBuilder(); foreach (var word in words) { if (word.Length == 1) { sb.Append(Converter.Convert(word, Direction.SimplifiedToTraditional)); } else { sb.Append(lexicon.Words.First(x => x.Simplified == word).Traditional); } } return(sb.ToString()); }
public static string ToTraditional(string chinese) { var lexicon = ChineseLexicon.Current ?? ChineseLexicon.Default; var words = ChineseTokenizer.SplitWords(chinese, ChineseType.Simplified); var sb = new StringBuilder(); foreach (var word in words) { var result = lexicon.Words.FirstOrDefault(x => x.Simplified == word)?.Traditional ?? word; sb.Append(result); } return(sb.ToString()); }
/// <summary> /// 转换指定字符串到简体中文。 /// </summary> /// <param name="chinese"></param> /// <returns></returns> public static string ToSimplified(string chinese) { var scope = LexiconScope.Current ?? LexiconScope.Default; var lexicon = scope.Lexicon; var words = ChineseTokenizer.SplitWords(ChineseTypes.Simplified, chinese); var sb = new StringBuilder(); foreach (var word in words) { var result = lexicon.Words.FirstOrDefault(x => x.Traditional == word)?.Simplified ?? word; sb.Append(result); } return(sb.ToString()); }
/// <summary> /// 获取指定类型字符串的拼音 /// </summary> /// <param name="chineseTypes"></param> /// <param name="chinese"></param> /// <param name="format"></param> /// <returns></returns> public static string GetString(ChineseTypes chineseTypes, string chinese, PinyinFormat format = PinyinFormat.Default) { var scope = LexiconScope.Current ?? LexiconScope.Default; var lexicon = scope.Lexicon; IEnumerable <int> GetDefaultSteps() { foreach (var ch in chinese) { yield return(1); } } var steps = lexicon is null?GetDefaultSteps() : ChineseTokenizer.SplitWords(chineseTypes, chinese).Select(x => x.Length); if (!chinese.IsNullOrWhiteSpace()) { var sb = new StringBuilder(); var insertSpace = false; var ptext = 0; foreach (var step in steps) { var word = chinese.Substring(ptext, step); try { string pinyin = null; if (pinyin is null && chineseTypes.HasFlag(ChineseTypes.Simplified)) { pinyin = lexicon.Words.FirstOrDefault(x => x.Simplified == word)?.SimplifiedPinyin; } if (pinyin is null && chineseTypes.HasFlag(ChineseTypes.Traditional)) { pinyin = lexicon.Words.FirstOrDefault(x => x.Traditional == word)?.TraditionalPinyin; } if (pinyin is null && chineseTypes.HasFlag(ChineseTypes.Simplified)) { pinyin = Builtin.ChineseChars.FirstOrDefault(x => x.Char == word[0])?.SimplifiedPinyin; } if (pinyin is null && chineseTypes.HasFlag(ChineseTypes.Traditional)) { pinyin = Builtin.ChineseChars.FirstOrDefault(x => x.Char == word[0])?.TraditionalPinyin; } if (pinyin is null) { throw new ArgumentException($"未能匹配文字({word})。"); } if (format != PinyinFormat.InitialConsonant) { if (insertSpace) { sb.Append(" "); } } switch (format) { case PinyinFormat.Default: sb.Append(pinyin); break; case PinyinFormat.WithoutTone: sb.Append(GetPinyinWithoutTone(pinyin)); break; case PinyinFormat.Phonetic: sb.Append(GetPhoneticSymbol(pinyin)); break; case PinyinFormat.InitialConsonant: sb.Append(pinyin.First()); break; } insertSpace = true; } catch { sb.Append(word); insertSpace = false; } ptext += step; } return(sb.ToString()); } return(chinese); }
public static string GetString(string chinese, PinyinFormat format = PinyinFormat.Default, ChineseType chineseType = ChineseType.Simplified) { var lexicon = ChineseLexicon.Current; IEnumerable <int> GetDefaultSteps() { foreach (var ch in chinese) { yield return(1); } } var steps = lexicon is null?GetDefaultSteps() : ChineseTokenizer.SplitWords(chinese, chineseType).Select(x => x.Length); if (!chinese.IsNullOrWhiteSpace()) { var sb = new StringBuilder(); var insertSpace = false; var ptext = 0; foreach (var step in steps) { var word = chinese.Substring(ptext, step); try { string pinyin; if (word.Length == 1) { var chineseChar = new ChineseChar(word[0]); pinyin = chineseChar.Pinyins[0].ToString().ToLower(); } else { var chineseWord = chineseType == ChineseType.Traditional ? lexicon.Words.First(x => x.Traditional == word) : lexicon.Words.First(x => x.Simplified == word); pinyin = chineseWord.Pinyin; } if (format != PinyinFormat.Code) { if (insertSpace) { sb.Append(" "); } } switch (format) { case PinyinFormat.Default: sb.Append(pinyin); break; case PinyinFormat.WithoutTone: sb.Append(GetPinyinWithoutTone(pinyin)); break; case PinyinFormat.Phonetic: sb.Append(GetPhoneticSymbol(pinyin)); break; case PinyinFormat.Code: sb.Append(pinyin.First()); break; } insertSpace = true; } catch { sb.Append(word); insertSpace = false; } ptext += step; } return(sb.ToString()); } return(chinese); }