public string TestCaseType(char ch, HanyuPinyinVCharType vcharType) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.CaseType = HanyuPinyinCaseType.UPPERCASE; format.VCharType = vcharType; return PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]; }
/// <summary> /// Convert Hanyu pinyin to given format /// </summary> /// <param name="pinyin">The given Hanyu pinyin string </param> /// <param name="outputFormat">The given format</param> /// <returns>The Hanyu pinyin with given format</returns> internal static string FormatHanyuPinyin( string pinyin, HanyuPinyinOutputFormat outputFormat) { if (outputFormat == null) throw new ArgumentNullException("The parameter outputFormat could not be null."); if (outputFormat.ToneType == HanyuPinyinToneType.WITH_TONE_MARK && (outputFormat.VCharType == HanyuPinyinVCharType.WITH_U_AND_COLON || outputFormat.VCharType == HanyuPinyinVCharType.WITH_V)) throw new InvalidHanyuPinyinFormatException("Tone marks cannot be added to v or u:"); string result = pinyin.ToLower(); if (outputFormat.ToneType == HanyuPinyinToneType.WITHOUT_TONE) result = Regex.Replace(pinyin, "[0-9]", ""); else if (outputFormat.ToneType == HanyuPinyinToneType.WITH_TONE_MARK) result = ConvertToneNumber2ToneMark(result.Replace("u:", "v")); if (outputFormat.VCharType == HanyuPinyinVCharType.WITH_V) result = result.Replace("u:", "v"); else if (outputFormat.VCharType == HanyuPinyinVCharType.WITH_U_UNICODE) result = result.Replace("u:", "ü"); if (outputFormat.CaseType == HanyuPinyinCaseType.UPPERCASE) result = result.ToUpper(); return result; }
/// <summary> /// Convert Hanyu pinyin to given format /// </summary> /// <param name="pinyin">The given Hanyu pinyin string </param> /// <param name="outputFormat">The given format</param> /// <returns>The Hanyu pinyin with given format</returns> internal static string FormatHanyuPinyin( string pinyin, HanyuPinyinOutputFormat outputFormat) { if (outputFormat == null) { throw new ArgumentNullException("The parameter outputFormat could not be null."); } if (outputFormat.ToneType == HanyuPinyinToneType.WITH_TONE_MARK && (outputFormat.VCharType == HanyuPinyinVCharType.WITH_U_AND_COLON || outputFormat.VCharType == HanyuPinyinVCharType.WITH_V)) { throw new InvalidHanyuPinyinFormatException("Tone marks cannot be added to v or u:"); } string result = pinyin.ToLower(); if (outputFormat.ToneType == HanyuPinyinToneType.WITHOUT_TONE) { result = Regex.Replace(pinyin, "[0-9]", ""); } else if (outputFormat.ToneType == HanyuPinyinToneType.WITH_TONE_MARK) { result = ConvertToneNumber2ToneMark(result.Replace("u:", "v")); } if (outputFormat.VCharType == HanyuPinyinVCharType.WITH_V) { result = result.Replace("u:", "v"); } else if (outputFormat.VCharType == HanyuPinyinVCharType.WITH_U_UNICODE) { result = result.Replace("u:", "ü"); } if (outputFormat.CaseType == HanyuPinyinCaseType.UPPERCASE) { result = result.ToUpper(); } return(result); }
/* /// <summary> /// 获取输入字符 /// </summary> /// <returns>System.String.</returns> private string GetInputText() { }*/ /// <summary> /// 获取字符串的拼音表示 /// </summary> /// <param name="title"> /// The title. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> private static string GetPinyinString(string title) { var builder = new StringBuilder(); var format = new HanyuPinyinOutputFormat { ToneType = HanyuPinyinToneType.WITH_TONE_MARK }; for (var i = 0; i < title.Length; i++) { var hz = title[i]; var pinyins = PinyinHelper.ToHanyuPinyinStringArray(hz, format); if (pinyins == null || pinyins.Length <= 0) { builder.Append(hz); } else { builder.Append(pinyins[0]); } } return builder.ToString(); }
/// <summary> /// 转换字符串 /// </summary> /// <param name="str"></param> /// <returns>无间隔的字符串全拼</returns> public static string ToFullLetter(string str) { var format = new HanyuPinyinOutputFormat(); StringBuilder sb = new StringBuilder(); foreach (char ch in str) { sb.Append(GetFirstPinyinStringArray(ch, format)); } return sb.ToString(); }
/// <summary> /// 只从拼音库中读取第一个拼音 /// 也就是忽略多音字 /// </summary> private static string GetFirstPinyinStringArray(char ch,HanyuPinyinOutputFormat format) { const string comma = ","; string code = String.Format("{0:X}", (int)ch).ToUpper(); if (dict.ContainsKey(code)) { var py = dict[code]; var index = py.IndexOf(comma); if (index > 0) { py = py.Substring(0,index); } return PinyinFormatter.FormatHanyuPinyin(py, format); } return null; }
private static string[] GetFomattedHanyuPinyinStringArray( char ch, HanyuPinyinOutputFormat format) { string[] unformattedArr = GetUnformattedHanyuPinyinStringArray(ch); if (null != unformattedArr) { for (int i = 0; i < unformattedArr.Length; i++) { unformattedArr[i] = PinyinFormatter.FormatHanyuPinyin(unformattedArr[i], format); } } return unformattedArr; }
/// <summary> /// Get all Hanyu pinyin of a single Chinese character (both /// Simplified Chinese and Traditional Chinese). /// </summary> /// <param name="ch">The given Chinese character</param> /// <param name="format">The given output format</param> /// <returns>A string array contains all Hanyu pinyin presentations; return /// null for non-Chinese character.</returns> public static string[] ToHanyuPinyinStringArray( char ch, HanyuPinyinOutputFormat format) { return GetFomattedHanyuPinyinStringArray(ch, format); }
/// <summary> /// 转换字符串 /// </summary> /// <param name="str"></param> /// <returns>字符串的首字母</returns> public static string ToFirstLetter(string str) { var format = new HanyuPinyinOutputFormat(); StringBuilder sb = new StringBuilder(); foreach (char ch in str) { var s = GetFirstPinyinStringArray(ch, format); if (!string.IsNullOrEmpty(s)) { sb.Append(s.Substring(0,1)); } } return sb.ToString(); }
public string[] TestCharWithMultiplePronouciations( char ch, HanyuPinyinToneType toneType, HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.ToneType = toneType; format.VCharType = vcharType; format.CaseType = caseType; return PinyinHelper.ToHanyuPinyinStringArray(ch, format); }
public string TestWithToneNumber( char ch, HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.ToneType = HanyuPinyinToneType.WITH_TONE_NUMBER; format.VCharType = vcharType; format.CaseType = caseType; return PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]; }
public void TestToneMarkWithUAndColon(char ch, HanyuPinyinVCharType vcharType) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.ToneType = HanyuPinyinToneType.WITH_TONE_MARK; format.VCharType = vcharType; PinyinHelper.ToHanyuPinyinStringArray(ch, format); }
public string TestToneMark(char ch) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.ToneType = HanyuPinyinToneType.WITH_TONE_MARK; format.VCharType = HanyuPinyinVCharType.WITH_U_UNICODE; return PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]; }