public string GetChineseSpell() { string _pinyin = ""; if (_strText == null || _strText.Trim() == "") { return _strText; } else { char[] chars = _strText.ToCharArray(); foreach (char c in chars) { if (ChineseChar.IsValidChar(c)) { ChineseChar cn = new ChineseChar(c); if (cn.IsPolyphone) { _IsPolyphone = true; } ReadOnlyCollection<string> roc = cn.Pinyins; if (roc.Count > 0) { string first = roc[0].ToString().Substring(0, 1).ToLower(); _pinyin = _pinyin + first; } // label1.Text = roc[0].ToString(); } else { _pinyin = _pinyin + c.ToString(); } } } return _pinyin; }
public static List<string> GetShortPinyin(string name) { List<List<char>> allPY = new List<List<char>>(); foreach (var item in name) { if (!ChineseChar.IsValidChar(item)) { continue; } ChineseChar cc = new ChineseChar(item); //取出多音字中所有第一个字母 var fchars = cc.Pinyins .Where(x => x != null) .Select(x => x.FirstOrDefault()) .Distinct() .Where(x => x != null) .ToList(); //将此集合加入到 allPY allPY.Add(fchars); } //遍历 allPY ,生成可能组成的简拼集合 List<string> result = new List<string>(); CreateCASE(allPY, ref result); return result.Distinct().ToList(); }
private void Button_Click(object sender, RoutedEventArgs e) //获取拼音 { string text = this.TB.Text.Trim(); if (text.Length == 0) { return; } try { //for(int i = 0; i < text.Length; i++) //{ //} char one_char = text.ToCharArray()[0]; int ch_int = (int)one_char; string str_char_int = string.Format("{0}", ch_int); if (ch_int > 127) { ChineseChar chineseChar = new ChineseChar(one_char); IReadOnlyCollection <string> pinyin = chineseChar.Pinyins; string pin_str = "\n "; foreach (string pin in pinyin) { pin_str += pin + "\r\n "; } this.OUT.Text = pin_str; } } catch (Exception e1) { MessageBox.Show("出现错误" + e1.ToString()); } }
/// <summary> /// 中文转拼音 /// </summary> /// <param name="chainessStr"></param> /// <param name="isUppper">是否大写</param> /// <returns></returns> public static string ChineseConverterToSpell(this string chainessStr, bool isUppper = false) { //英文 var returnSpell = new StringBuilder(); foreach (var obj in chainessStr) { try { var chineseChar = new ChineseChar(obj); var returnSpellChar = chineseChar.Pinyins[0]; //TODO 这里获取第一个拼音,但有可能是多音字的情况 var item = returnSpellChar.Substring(0, returnSpellChar.Length - 1); if (!isUppper) { returnSpell.Append(item.Substring(0, 1).ToUpper() + item.Substring(1).ToLower()); } else { returnSpell.Append(item); } } catch { returnSpell.Append(obj.ToString()); } } return(returnSpell.ToString()); }
public static List <string> GetShortPinyin(string name) { List <List <char> > allPY = new List <List <char> >(); foreach (var item in name) { if (!ChineseChar.IsValidChar(item)) { continue; } ChineseChar cc = new ChineseChar(item); //取出多音字中所有第一个字母 var fchars = cc.Pinyins .Where(x => x != null) .Select(x => x.FirstOrDefault()) .Distinct() .Where(x => x != null) .ToList(); //将此集合加入到 allPY allPY.Add(fchars); } //遍历 allPY ,生成可能组成的简拼集合 List <string> result = new List <string>(); CreateCASE(allPY, ref result); return(result.Distinct().ToList()); }
private static Dictionary <int, List <string> > GetTotalPingYinDictionary(string text) { var chs = text.ToCharArray(); //记录每个汉字的全拼 Dictionary <int, List <string> > totalPingYinList = new Dictionary <int, List <string> >(); for (int i = 0; i < chs.Length; i++) { var pinyinList = new List <string>(); //是否是有效的汉字 if (ChineseChar.IsValidChar(chs[i])) { ChineseChar cc = new ChineseChar(chs[i]); pinyinList = cc.Pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).ToList(); } else { pinyinList.Add(chs[i].ToString()); } //去除声调,转小写 pinyinList = pinyinList.ConvertAll(p => Regex.Replace(p, @"\d", "").ToLower()); //去重 pinyinList = pinyinList.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList(); if (pinyinList.Any()) { totalPingYinList[i] = pinyinList; } } return(totalPingYinList); }
private string GetPinYin(char cKey) { try { ChineseChar objChar = new ChineseChar(cKey); Hashtable ht = new Hashtable(); string sRslt = ""; foreach (string s in objChar.Pinyins) { if (s == null) { continue; } string sPy = s.Substring(0, s.Length - 1).ToLower(); if (ht[sPy] == null) { ht[sPy] = 1; sRslt += sPy + " "; } } return(sRslt.TrimEnd()); } catch { return(""); } }
public static string PinyinFull(this string value) { if (string.IsNullOrEmpty(value)) { return(string.Empty); } var sb = new StringBuilder(); foreach (var ch in value) { try { if (ChineseChar.IsValidChar(ch)) { var py = new ChineseChar(ch); sb.Append(py.Pinyins[0][0]).Append(py.Pinyins[0].Substring(1, py.Pinyins[0].Length - 2).ToLower()); } else { sb.Append(ch); } } catch { sb.Append(ch); } } return(sb.ToString().TrimEnd()); }
/// <summary> /// 汉字转化为拼音首字母 /// </summary> /// <param name="str">汉字</param> /// <returns>首字母</returns> public static string GetFirstPinYin(string str) { if (str == null) { return(null); } if (str.Trim().Length <= 0) { return(string.Empty); } char[] list = new char[str.Length]; for (int i = 0; i < str.Length; i++) { char obj = str[i]; if (ChineseChar.IsValidChar(obj)) { ChineseChar chineseChar = new ChineseChar(obj); list[i] = chineseChar.Pinyins[0][0]; } else { list[i] = obj; } } return(new string(list)); }
/// <summary> /// 汉字转化为拼音 /// </summary> /// <param name="str">汉字</param> /// <returns>全拼</returns> public static string GetPinyin(string str) { if (string.IsNullOrEmpty(str)) { return(null); } var list = new List <List <string> >(); foreach (char obj in str) { var chars = new List <string>(); list.Add(chars); if (!ChineseChar.IsValidChar(obj)) { chars.Add(obj.ToString()); continue; } ChineseChar chineseChar = new ChineseChar(obj); foreach (var item in chineseChar.Pinyins.Where(a => a != null)) { var pinyin = item.Substring(0, item.Length - 1); if (chars.Contains(pinyin)) { continue; } chars.Add(pinyin); } } return(string.Join(',', CombineString(list[0], list.GetRange(1, list.Count - 1))).ToLower()); }
public static string convert(string senstr) { string ss = StopWordsRegex.Replace(senstr, ""); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ss.Length; i++) { if (ChineseChar.IsValidChar(ss[i])) { ChineseChar cc = new ChineseChar(ss[i]); if (ChineseChar.IsValidChar(ss[i]) && cc.PinyinCount > 0) { sb.Append(cc.Pinyins[0].Substring(0, cc.Pinyins[0].Length - 1)); sb.Append(" "); } else { sb.Append(ss[i]); } } else { sb.Append(ss[i]); } } return(sb.ToString()); }
/// <summary> /// 获取汉字首字母 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string GetCnFirstChart(this string str) { var ret = ""; foreach (var c in str) { if ((int)c <= 127) { if ((int)c < 65) { continue; } else if ((int)c <= 122 && (int)c >= 65) { ret += c.ToString().ToUpper(); } else { continue; } } else { ChineseChar chineseChar = new ChineseChar(c); var pinyins = chineseChar.Pinyins; String firstPinyin = pinyins[0].Substring(0, 1); ret += firstPinyin; } } return(ret); }
/// <summary> /// 输入汉字返回拼音 首字母大写 /// 输出是否包含多音字 /// </summary> /// <param name="txt"></param> /// <returns></returns> public static string GetPinyin(string txt, out bool polyphone) { polyphone = false; char[] ch = txt.ToArray(); StringBuilder pinyinStr = new StringBuilder(); foreach (char c in ch) { if (ChineseChar.IsValidChar(c)) { ChineseChar chineseChar = new ChineseChar(c); ReadOnlyCollection <string> pinyin = chineseChar.Pinyins; List <string> py = pinyin.Where(n => !string.IsNullOrEmpty(n)).ToList(); List <string> py1 = py.Select(n => n.Substring(0, n.Length - 1)).Distinct().ToList(); if (py1.Count() > 1) { polyphone = true; } pinyinStr.Append(pinyin[0].Substring(0, 1)); pinyinStr.Append(pinyin[0].Substring(1, pinyin[0].Length - 2).ToLower()); } else { pinyinStr.Append(c.ToString()); } } return(pinyinStr.ToString()); }
/// <summary> /// 获取汉字拼音 /// </summary> /// <param name="strText"></param> /// <returns></returns> public static string GetChineseSpell(string strText) { string py = ""; var builder = new StringBuilder(); strText.ToCharArray().ForEach((p) => { if (!ChineseChar.IsValidChar(p)) { builder.Append(p.ToString()); //不是有效的汉字,原字输出 } else { var chineseChar = new ChineseChar(p); if (chineseChar.PinyinCount > 0) { py = chineseChar.Pinyins[0].ToString(); builder.Append(py.Substring(0, py.Length - 1)); } else { builder.Append(p.ToString()); } } }); return(builder.ToString()); }
private void SetAppName(string name) { string appName = string.Empty; int count = 0; int num = 0; while (count < 6) { if (num >= name.Length) { break; } if (ChineseChar.IsValidChar(name[num])) { count += 2; } else { count += 1; } num++; } if (count > 6) { appName = name.Substring(0, num) + "..."; } else { appName = name; } this.TileContainer.Title = appName; this.GridToolTip.Text = System.IO.Path.GetFileName(this.ShowName); }
/// <summary> /// 将字符串转换成拼音 /// </summary> /// <param name="chineseStr">拼音字符串</param> /// <param name="includeTone">是否包含音调</param> /// <returns></returns> public static string ConvertToPinYin(string chineseStr) { if (chineseStr == null) { throw new ArgumentNullException(nameof(chineseStr)); } var charArray = chineseStr.ToCharArray(); var sb = new StringBuilder(); foreach (var c in charArray) { if (!IsValidChar(c)) { sb.Append(c); continue; } var chineseChar = new ChineseChar(c); var pyColl = chineseChar.Pinyins; if (pyColl.Length == 0) { continue; } var first = pyColl[0]; sb.Append(first.Remove(first.Length - 1)); } return(sb.ToString()); }
/// <summary> /// 汉字转化为拼音 /// </summary> /// <param name="str">汉字</param> /// <param name="str">转化的拼音是否需要用空格隔开</param> /// <returns>全拼</returns> public static string GetPinYin(string str, bool withBlank) { if (str == null) { return(null); } if (str.Trim().Length <= 0) { return(string.Empty); } string[] list = new string[str.Length]; for (int i = 0; i < str.Length; i++) { char obj = str[i]; if (ChineseChar.IsValidChar(obj)) { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0]; list[i] = t.Substring(0, t.Length - 1); } else { list[i] = obj.ToString(); } } return(string.Join(withBlank ? " " : "", list)); }
/// <summary> /// 获取词组所有可能的拼音组合 /// </summary> /// <param name="source">要获取拼音的文字</param> /// <param name="singleWordSpliter">单个字之间的分隔符</param> /// <param name="wordTermSpliter">词字之间的分隔符</param> /// <returns></returns> public static string GetPinYinCombination(string source, string singleWordSpliter = "", string wordTermSpliter = " ") { if (string.IsNullOrWhiteSpace(source)) { return(string.Empty); } var result = new List <List <string> >(); foreach (var c in source) { if (ChineseChar.IsValidChar(c)) { var pinYins = GetPinYinWithTone(c) .Where(x => x != null) .Select(RemoveTone) .Distinct() .Select(UpperFirstChar) .ToList(); result.Add(pinYins); } else { result.Add(new List <string> { UpperChar(c).ToString() }); } } return(string.Join(wordTermSpliter, Combinate(result, singleWordSpliter))); }
/// <summary> /// 获取首字母拼音 /// </summary> /// <param name="name"></param> /// <returns></returns> public static char GetPYChar(string name) { var c = name.First(); if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) { return(c); } else { try { ChineseChar cc = new ChineseChar(c); if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0) { return(cc.Pinyins[0][0]); } } catch (Exception ex) { return(c); } return(c); } }
public static string GetFulltPY(string str) { char[] chas = str.ToCharArray(); string strpy = ""; foreach (char cha in chas) { if (ChineseChar.IsValidChar(cha)) { ChineseChar china = new ChineseChar(cha); if (china.Pinyins.Count > 0) { for (int i = 0; i < china.Pinyins[0].Length - 1; i++) { if (i == 0) { strpy += china.Pinyins[0][0].ToString().ToUpper(); } else { strpy += china.Pinyins[0][i].ToString().ToLower(); } } } } } return(strpy); }
/// <summary> /// 字符串的全拼 /// </summary> /// <param name="str_InputText"></param> /// <returns></returns> public static string P_GetAllPinYin(string str_InputText) { string str_Result = ""; if (!string.IsNullOrEmpty(str_InputText) && !string.IsNullOrEmpty(str_InputText.Trim())) { foreach (char c in str_InputText.Trim()) { try { if ((c > 64 && c < 91) || (c > 96 && c < 122) || (c > 47 && c < 58)) { str_Result += c; } else { ChineseChar chineseChar = new ChineseChar(c); str_Result += chineseChar.Pinyins[0].Substring(0, chineseChar.Pinyins[0].Length - 1); } } catch { str_Result += c; } } } return(str_Result); }
//private String getInputText() //{ //} private void PinYinBtn_Click(object sender, RoutedEventArgs e) { String chinese = inputText.Text.Trim(); if (chinese == null || chinese == "") { MessageBox.Show("请输入汉字"); } else { //清除原有的拼音 ouputList.Items.Clear(); foreach (char c in chinese) { if (ChineseChar.IsValidChar(c)) { ChineseChar chChar = new ChineseChar(c); ReadOnlyCollection <string> pinyins = chChar.Pinyins; for (int i = 0; i < pinyins.Count; i++) { if (pinyins[i] != null && pinyins[i] != "") { ouputList.Items.Add(pinyins[i]); } } } //换字的时候换行 ouputList.Items.Add(null); } } }
public static string GetPyFromName(string userName) { StringBuilder sbPy = new StringBuilder(); //1.获取用户输入的文字 string user_input = userName.Trim(); //1.1循环遍历字符串中的每个字符 for (int i = 0; i < user_input.Length; i++) { if (ChineseChar.IsValidChar(user_input[i])) { //2.创建一个拼音计算类的对象 ChineseChar cnChar = new ChineseChar(user_input[i]); if (cnChar.PinyinCount > 0) { sbPy.Append(cnChar.Pinyins[0].Substring(0, cnChar.Pinyins[0].Length - 1).ToLower()); //sbPy.Append(cnChar.Pinyins[0].Substring(0, 1)); //拼音首字母 } } else { //sbPy.Append(user_input[i]); } } return(sbPy.ToString()); }
/// <summary> /// 获取字符串全拼 /// </summary> /// <param name="str">要转换的字符串,无法避免多音字</param> /// <returns></returns> public static string GetPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); var pinyins = chineseChar.Pinyins; foreach (var pinyin in pinyins) { if (pinyin != null) { r += pinyin.Substring(0, pinyin.Length - 1); break; } } } catch { r = "转换出现错误"; } } return(r); }
public static string ConvertToPY(string source) { char[] arrString = source.ToCharArray(); string pyFinal = ""; if (arrString.Length > 0) { for (int i = 0; i < arrString.Length; i++) { try { //获取某个中文字符的拼音 //先判断是否是英文字母或者数字 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9]+$"); if (reg1.IsMatch(arrString[i].ToString())) { pyFinal += arrString[i].ToString().ToLower(); continue; } ChineseChar chn = new ChineseChar(arrString[i]); //取开头字母 string py = chn.Pinyins[0].Substring(0, 1); pyFinal += py; } catch (Exception ex) { //如果不是简体中文,则忽略 } } } return(pyFinal); }
private static char[] GetFirstWordByCh(char singleChinese) { ChineseChar ch; List <char> list = new List <char>(); if (singleChinese < '\x007f') { list.Add(char.ToUpper(singleChinese)); return(list.ToArray()); } try { ch = new ChineseChar(singleChinese); } catch { return(null); } if (ch.get_PinyinCount() < 1) { return(null); } ReadOnlyCollection <string> onlys = ch.get_Pinyins(); for (int i = 0; i < ch.get_PinyinCount(); i++) { if (!list.Contains(onlys[i][0])) { list.Add(onlys[i][0]); } } return(list.ToArray()); }
public static string ToPinYin(this string zhongwen) { if (string.IsNullOrWhiteSpace(zhongwen)) { return(null); } string result = string.Empty; foreach (char item in zhongwen) { try { ChineseChar cc = new ChineseChar(item); if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0) { string temp = cc.Pinyins[0].ToString(); result += temp.Substring(0, temp.Length - 1); } } catch (Exception) { result += item.ToString(); } } return(result); }
private void btn1_Click_1(object sender, RoutedEventArgs e) { if (!checkInput()) { return; } listBox.Items.Clear(); char one_char = textBox.Text.Trim().ToCharArray()[0]; int ch_int = (int)one_char; string str_char_int = string.Format("{0}", ch_int); if (ch_int > 127) { ChineseChar chineseChar = new ChineseChar(one_char); System.Collections.ObjectModel.ReadOnlyCollection <string> pinyin = chineseChar.Pinyins; string pin_str = ""; foreach (string pin in pinyin) { listBox.Items.Add(pin); pin_str += pin + "\r\n"; } } }
/// <summary> /// 获取该字符串的拼音首字母 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string GetFirstPY(string str) { if (!string.IsNullOrEmpty(str)) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); if (chineseChar.Pinyins.Count > 0) { string t = chineseChar.Pinyins[0].ToString(); if (t.Length > 0) { r += t.Substring(0, 1); } } } catch { r += obj.ToString(); } } return(r); } return(string.Empty); }
private static void getallpinyin(ArrayList pinyinlist, StringBuilder py, string sen, int index) { if (index == sen.Length) { pinyinlist.Add(py.ToString()); return; } if (ChineseChar.IsValidChar(sen[index])) { ChineseChar cc = new ChineseChar(sen[index]); string[] pinyins = getpinyins(cc); for (int i = 0; i < pinyins.Length; i++) { int len = py.Length; py.Append(pinyins[i]); py.Append(" "); getallpinyin(pinyinlist, py, sen, index + 1); py.Length = len; } } else { int len = py.Length; py.Append(sen[index]); getallpinyin(pinyinlist, py, sen, index + 1); py.Length = len; } }
/// <summary> /// 获取汉字的拼音 /// </summary> /// <param name="targetValue">目标汉字</param> /// <param name="pinYinOnly">只要拼音</param> /// <returns>拼音</returns> public static string GetPinyin(string targetValue, bool pinYinOnly = true) { string returnValue = string.Empty; if (!string.IsNullOrEmpty(targetValue)) { foreach (Char c in targetValue) { if (ChineseChar.IsValidChar(c)) { ChineseChar chineseChar = new ChineseChar(c); // 汉字的所有拼音拼写 ReadOnlyCollection <string> pinyins = chineseChar.Pinyins; returnValue += pinyins[0].Substring(0, pinyins[0].Length - 1).ToLower(); } else { if (!pinYinOnly) { returnValue += c; } } } } return(returnValue); }
private void ListBoxWord_MouseDoubleClick(object sender, MouseEventArgs e) { if (!string.IsNullOrWhiteSpace(textBoxLyrics.Text)) { var myLyricsChars = textBoxLyrics.Text.Replace("\n", "").Replace("\r", "").Replace(" ", "").Replace("|", "").ToCharArray(); string myLyricsWordStr = myLyricsChars[listBoxWord.SelectedIndex].ToString(); textBoxTone.Enabled = true; if (new ChineseChar(Convert.ToChar(myLyricsWordStr)).IsPolyphone) { listBoxTone.Enabled = true; var pinyinListR = new ChineseChar(Convert.ToChar(myLyricsWordStr)).Pinyins; var pinyinList = new List <string>(pinyinListR); RemoveNullElement(pinyinList); listBoxTone.Items.Clear(); listBoxTone.Items.AddRange(items: pinyinList.ToArray()); } else { MessageBox.Show(@"这不是一个多音字。"); } } }
private string ConvertToPinYin(string input) { StringBuilder sb = new StringBuilder(); foreach (var item in input) { string s= new ChineseChar(item).Pinyins[0]; sb.Append(s.Substring(0,s.Length-1)); } return sb.ToString(); }
/// <summary> /// 获取单个汉字的首写拼音字母 /// </summary> /// <param name="cn"></param> /// <returns></returns> public static string GetSpell(char cn) { var res = cn.ToString(); if (Regex.IsMatch(res, @"[\u4E00-\u9FA5]")) { ChineseChar china = new ChineseChar(cn); res = china.Pinyins.First(); } return res; }
/// <summary> /// 获取字符的拼音,没有音标 /// </summary> /// <param name="ch">传入的字符</param> /// <returns>返回的字符的拼音</returns> public static string GetPinYinChar(char ch) { string res = ""; if (ChineseChar.IsValidChar(ch)) { ChineseChar cc = new ChineseChar(ch); res = cc.Pinyins[0].ToString().Substring(0, cc.Pinyins[0].Length - 1); } else { res = ch.ToString(); } return res; }
/// <summary> /// 根据字符串获得拼音 /// </summary> /// <param name="input"></param> /// <returns></returns> private string GetPinYin(string input) { StringBuilder resultSb = new StringBuilder(); foreach (var item in input) { //判断是否是汉字 if (ChineseChar.IsValidChar(item)) { ChineseChar c = new ChineseChar(item); //一般情况下回输出拼音加一个数字,下面把这个数字去掉 resultSb.Append(c.Pinyins[0].Substring(0,c.Pinyins[0].Length-1)+" "); } } return resultSb.ToString().ToLower(); }
/// <summary> /// �~�r��Ƭ����� /// </summary> /// <param name=��str ��> �~�r</param> /// <returns> ����</returns> public static string GetPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, t.Length - 1); } catch { r += obj.ToString(); } } return r; }
private static List<string> GetHeadsOfWord(char word) { var chineseChar = new ChineseChar(word); var heads = new List<string>(); foreach (var pinyin in chineseChar.Pinyins) { if (pinyin == null) { continue; } var head = pinyin.Substring(0, 1); heads.Add(head); } return heads.Distinct().ToList(); }
/// <summary> /// 拼音转换 /// </summary> /// <param name="str"></param> /// <param name="simple"></param> /// <returns></returns> public static string ToSpell(this string str, bool simple = false) { if (string.IsNullOrEmpty(str)) return ""; string fullPath = AppConfig.BasePath + AppConfig.GetConfig("pinypath"); Dictionary<string, string[]> dict = FileHelper.ReadFileSplit(fullPath, "|"); if (dict != null && dict.Count > 0) { foreach (string key in dict.Keys) { string[] value = dict[key]; if (str.Contains(key)) { if (simple) { str = value.Length > 1 ? str.Replace(key, value[1]) : str.Replace(key, value[0].Substring(0, 1)); } else { str = str.Replace(key, value[0]); } } } } List<string> res = new List<string>(); foreach (char c in str) { if (ChineseChar.IsValidChar(c)) { string piny = new ChineseChar(c).Pinyins[0]; piny = piny.Substring(0, piny.Length - 1); res.Add(piny.ToLower()); } else { res.Add(c.ToString()); } } return simple ? string.Join("", res.Select(r => r.Substring(0, 1))) : string.Join("", res); }
public static string[] ToPinyins(char ch, PinyinStyle style = PinyinStyle.ToneNumber) { if (ChineseChar.IsValidChar(ch)) { var cc = new ChineseChar(ch); switch (style) { case PinyinStyle.ToneNumber: return cc.Pinyins.Take(cc.PinyinCount).ToArray(); case PinyinStyle.NoTone: return cc.Pinyins.Take(cc.PinyinCount) .Select(py => py.Substring(0, py.Length - 1)) .Distinct().ToArray(); case PinyinStyle.Acronym: return cc.Pinyins.Take(cc.PinyinCount) .Select(py => py.Substring(0, 1)) .Distinct().ToArray(); } } return null; }
public static string GetPinyin(string str, List<miniZhDicts> zhlists) { var zh = zhlists.FirstOrDefault(n => n.word == str.TrimEnd().TrimStart()); if (zh != null) { return zh.pinyin.ToLower(); } else { string r = string.Empty; foreach (char obj in str) { try { var chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToLower(); r += t + "@"; } catch { r += obj.ToString().ToLower() + " "; } } var yinbiaostr = ""; if (!string.IsNullOrEmpty(r)) { var xsb = Regex.Split(r, "@"); foreach (var s in xsb) { if (!string.IsNullOrEmpty(s.TrimEnd().TrimStart())) { yinbiaostr = yinbiaostr + Getx(s) + " "; } } } return yinbiaostr.ToLower(); } }
/*END CUSTOM*/ // GET: /Customer/ public ViewResult Index(int? page) { int currentPageIndex = page.HasValue ? page.Value - 1 : 0; var customers = db.Customers.OrderBy(c => c.Name).ToList(); List<CustomerWithPinYinName> customer_PinYin = new List<CustomerWithPinYinName>(); if (Session["LoginUser"] != null) { User user = (User)Session["LoginUser"]; customers = customers.Where(x => x.UserID == user.UserID).ToList(); foreach (Customer cus in customers) { CustomerWithPinYinName cusPY = new CustomerWithPinYinName(); cusPY.Address = cus.Address; cusPY.City = cus.City; cusPY.CompanyNumber = cus.CompanyNumber; cusPY.ContactPerson = cus.ContactPerson; cusPY.CP = cus.CP; cusPY.CustomerID = cus.CustomerID; cusPY.Email = cus.Email; cusPY.Fax = cus.Fax; cusPY.Invoices = cus.Invoices; cusPY.Name = cus.Name; cusPY.Notes = cus.Notes; cusPY.Phone1 = cus.Phone1; cusPY.Phone2 = cus.Phone2; cusPY.User = cus.User; cusPY.UserID = cus.UserID; string r = string.Empty; foreach (char obj in cusPY.Name) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } cusPY.PinYin = r; customer_PinYin.Add(cusPY); } } return View(customer_PinYin.ToPagedList(currentPageIndex, defaultPageSize)); }
public ViewResultBase Search(string q, int? page) { q = q.ToUpper(); List<Customer> customers = db.Customers.OrderBy(c => c.Name).ToList(); List<CustomerWithPinYinName> customer_PinYin = new List<CustomerWithPinYinName>(); if (Session["LoginUser"] != null) { foreach (Customer cus in customers) { CustomerWithPinYinName cusPY = new CustomerWithPinYinName(); cusPY.Address = cus.Address; cusPY.City = cus.City; cusPY.CompanyNumber = cus.CompanyNumber; cusPY.ContactPerson = cus.ContactPerson; cusPY.CP = cus.CP; cusPY.CustomerID = cus.CustomerID; cusPY.Email = cus.Email; cusPY.Fax = cus.Fax; //cusPY.Invoices = cus.Invoices; cusPY.Name = cus.Name; cusPY.Notes = cus.Notes; cusPY.Phone1 = cus.Phone1; cusPY.Phone2 = cus.Phone2; cusPY.User = cus.User; cusPY.UserID = cus.UserID; string r = string.Empty; foreach (char obj in cusPY.Name) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } cusPY.PinYin = r; customer_PinYin.Add(cusPY); } if (q.Length == 1)//alphabetical search, first letter { ViewBag.LetraAlfabetica = q; customer_PinYin = customer_PinYin.Where(c => c.PinYin.StartsWith(q)).ToList(); } else if (q.Length > 1) { //normal search customer_PinYin = customer_PinYin.Where(c => c.PinYin.IndexOf(q) > -1).ToList(); } } int currentPageIndex = page.HasValue ? page.Value - 1 : 0; var customersListPaged = customer_PinYin.OrderBy(i => i.Name).ToPagedList(currentPageIndex, defaultPageSize); if (Request.IsAjaxRequest()) return PartialView("Index", customersListPaged); else return View("Index", customersListPaged); }
private void DoFormatEdit(ref string T1) { int i; for (i = 1; i <= 5; i++) { if (((RadioButton)(FindName("Format" + i.ToString()))).IsChecked.Value) { T1 = f.TransformStr(Text1, i); break; } } if (i > 5 && ((RadioButton)FindName("Pinyin")).IsChecked.Value) { string temp = string.Empty; foreach (char c in Text1) { if (Regex.IsMatch(c.ToString(), @"[\u4e00-\u9fbb]")) { ChineseChar ch = new ChineseChar(c); string s = ch.Pinyins[0]; s = s.ToLower(); s = s.Substring(0, s.Length - 1); temp += s + ' '; } else { temp += c; } } T1 = temp; } }
private void window_KeyDown(object sender, KeyEventArgs e) { if (!isInputMode) { if (e.Key == Key.Tab) btnSkip_Click(null, null); return; } switch (e.Key) { case Key.Tab: btnSkip_Click(null, null); return; case Key.Back: if (txtInputBuffer.Text != "") txtInputBuffer.Text = txtInputBuffer.Text.Remove(txtInputBuffer.Text.Length - 1); break; case Key.A: txtInputBuffer.Text += 'a'; break; case Key.B: txtInputBuffer.Text += 'b'; break; case Key.C: txtInputBuffer.Text += 'c'; break; case Key.D: txtInputBuffer.Text += 'd'; break; case Key.E: txtInputBuffer.Text += 'e'; break; case Key.F: txtInputBuffer.Text += 'f'; break; case Key.G: txtInputBuffer.Text += 'g'; break; case Key.H: txtInputBuffer.Text += 'h'; break; case Key.I: txtInputBuffer.Text += 'i'; break; case Key.J: txtInputBuffer.Text += 'j'; break; case Key.K: txtInputBuffer.Text += 'k'; break; case Key.L: txtInputBuffer.Text += 'l'; break; case Key.M: txtInputBuffer.Text += 'm'; break; case Key.N: txtInputBuffer.Text += 'n'; break; case Key.O: txtInputBuffer.Text += 'o'; break; case Key.P: txtInputBuffer.Text += 'p'; break; case Key.Q: txtInputBuffer.Text += 'q'; break; case Key.R: txtInputBuffer.Text += 'r'; break; case Key.S: txtInputBuffer.Text += 's'; break; case Key.T: txtInputBuffer.Text += 't'; break; case Key.U: txtInputBuffer.Text += 'u'; break; case Key.V: txtInputBuffer.Text += 'v'; break; case Key.W: txtInputBuffer.Text += 'w'; break; case Key.X: txtInputBuffer.Text += 'x'; break; case Key.Y: txtInputBuffer.Text += 'y'; break; case Key.Z: txtInputBuffer.Text += 'z'; break; } // 试着比对文本框内容 string content = txtInputBuffer.Text; if (content == "") return; if (isPinyinMode) { try { char kanji = currentWord.Kanji[currentKanjiIndex], hanzi; if (kanji == '々') kanji = currentWord.Kanji[currentKanjiIndex - 1]; if (kanji2Hanzi.TryGetValue(kanji, out hanzi)) kanji = hanzi; ChineseChar chr = new ChineseChar(kanji); foreach (string pinyin in chr.Pinyins) if (pinyin != null && content == pinyin.Substring(0, pinyin.Length - 1).ToLower()) { // 增加一个汉字到大字行 txtKanji.Text += currentWord.Kanji[currentKanjiIndex]; // 清空缓冲区 txtInputBuffer.Clear(); if (++currentKanjiIndex == currentWord.Kanji.Length) { SuccessCount++; currentWord.Kana2KanjiSuccessCount++; PushLog("完成了词 " + currentWord.Kanji + " 的汉字输入"); isInputMode = false; } return; } } catch (NotSupportedException) { // 如果词语是混合词 string kana = KanaConverter.RomajiToHiragana(content); // 防止误输入拗音 if (currentWord.Kanji.IndexOf(kana, currentKanjiIndex) == currentKanjiIndex && (kana.Length == 3 || // 促音 + 拗音,相等就一定可以输入 (currentWord.Kanji[currentKanjiIndex] == 'っ' && kana.Length == 2 && currentWord.Kanji.IndexOfAny(new[] { 'ゃ', 'ゅ', 'ょ' }, currentKanjiIndex + 2) != currentKanjiIndex + 2) || // 促音非拗音,第三个字不是拗音即可 (currentWord.Kana[currentKanaIndex] != 'っ' && kana.Length == 2) || // 非促音拗音 (currentWord.Kanji[currentKanjiIndex] != 'っ' && kana.Length == 1 && currentWord.Kanji.IndexOfAny(new[] { 'ゃ', 'ゅ', 'ょ' }, currentKanjiIndex + 1) != currentKanjiIndex + 1) // 非促音非拗音,第二个字不是拗音即可 )) { // 增加假名到大字行 txtKanji.Text += kana; // 清空缓冲区 txtInputBuffer.Clear(); if ((currentKanjiIndex += kana.Length) == currentWord.Kanji.Length) { SuccessCount++; currentWord.Kana2KanjiSuccessCount++; PushLog("完成了词 " + currentWord.Kanji + " 的汉字输入"); isInputMode = false; return; } } } } else { string kana = KanaConverter.RomajiToHiragana(content); // 防止误输入拗音 if (currentWord.Kana.IndexOf(kana, currentKanaIndex) == currentKanaIndex && (kana.Length == 3 || // 促音 + 拗音,相等就一定可以输入 (currentWord.Kana[currentKanaIndex] == 'っ' && kana.Length == 2 && currentWord.Kana.IndexOfAny(new[] { 'ゃ', 'ゅ', 'ょ' }, currentKanaIndex + 2) != currentKanaIndex + 2) || // 促音非拗音,第三个字不是拗音即可 (currentWord.Kana[currentKanaIndex] != 'っ' && kana.Length == 2) || // 非促音拗音 (currentWord.Kana[currentKanaIndex] != 'っ' && kana.Length == 1 && currentWord.Kana.IndexOfAny(new[] { 'ゃ', 'ゅ', 'ょ' }, currentKanaIndex + 1) != currentKanaIndex + 1) // 非促音非拗音,第二个字不是拗音即可 )) { // 增加假名到小字行 txtAnnotation.Text += kana; // 清空缓冲区 txtInputBuffer.Clear(); if ((currentKanaIndex += kana.Length) == currentWord.Kana.Length) { SuccessCount++; currentWord.Kanji2KanaSuccessCount++; PushLog("完成了词 " + currentWord.Kanji + " 的假名输入"); isInputMode = false; return; } } } }
/// <summary> /// 汉字转化为拼音首字母 /// </summary> /// <param name="str">汉字</param> /// <returns>首字母</returns> private string GetFirstPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } return r; }
/// <summary> /// 获取汉字的拼音首字母 /// </summary> /// <param name="targetValue">目标汉字</param> /// <returns>拼音</returns> public static string GetFirstPinyin(string targetValue) { string returnValue = string.Empty; if (!string.IsNullOrEmpty(targetValue)) { foreach (Char c in targetValue) { if (ChineseChar.IsValidChar(c)) { ChineseChar chineseChar = new ChineseChar(c); // 汉字的所有拼音拼写 ReadOnlyCollection<string> pinyins = chineseChar.Pinyins; returnValue += pinyins[0].Substring(0, 1).ToUpper(); } } } return returnValue; }
private static List<string> GetSpellsOfWord(char word) { var chineseChar = new ChineseChar(word); var spells = new List<string>(); foreach (var pinyin in chineseChar.Pinyins) { if (pinyin == null) { continue; } var spell = pinyin.Substring(0, 1) + pinyin.Substring(1, pinyin.Length - 2).ToLower(); spells.Add(spell); } return spells.Distinct().ToList(); }
/// <summary> /// 获取 可用的拼音。 /// (不要最后的 1,2,3,4 声) /// </summary> /// <param name="charData"></param> /// <returns></returns> private List<string> GetPinYins(char charData) { List<string> resultList = new List<string>(); ChineseChar chineseChar = new ChineseChar(charData); foreach (string pin in chineseChar.Pinyins) { if (String.IsNullOrEmpty(pin)) { // 忽略空白. continue; } // 结果 去掉最后一个 声调的数字. string result = pin.Substring(0, pin.Length - 1).ToLower(); if (!resultList.Contains(result)) { resultList.Add(result); } } return resultList; }
/// <summary> /// 汉字转化为拼音 /// </summary> /// <param name="str">汉字</param> /// <returns>全拼</returns> public static string GetPinyin(string str) { var r = string.Empty; foreach (var obj in str) { try { var chineseChar = new ChineseChar(obj); var t = chineseChar.Pinyins[0]; r += t.Substring(0, t.Length - 1); } catch (Exception) { r += obj.ToString(); } } return r; }
/// <summary> /// 获取中文全拼 /// </summary> public static string GetQuanPin(string characters, bool isUppercase = false) { if (characters.Length != 0) { StringBuilder fullSpellBuild = new StringBuilder(); for (int i = 0; i < characters.Length; i++) { //判断是否是中文 bool itemFlag = ChineseChar.IsValidChar(characters[i]); if (itemFlag) { ChineseChar chineseChar = new ChineseChar(characters[i]); foreach (string value in chineseChar.Pinyins) { if (!string.IsNullOrEmpty(value)) { fullSpellBuild.Append(value.Remove(value.Length - 1, 1)); break; } } } else { fullSpellBuild.Append(characters[i]); } } if (isUppercase) return fullSpellBuild.ToString().ToUpper(); return fullSpellBuild.ToString().ToLower(); } return ""; }
public ViewResultBase Search(string q, int? page) { q = q.ToUpper(); List<CoffeeInvoice.Models.ViewModel.ProductViewModel> productsVM = new List<Models.ViewModel.ProductViewModel>(); if (Session["LoginUser"] != null) { User user = (User)Session["LoginUser"]; var products = db.Products.Where(x => x.UserID == user.UserID).OrderBy(x => x.ProductID).ToList(); foreach (var pro in products) { CoffeeInvoice.Models.ViewModel.ProductViewModel proVM = new Models.ViewModel.ProductViewModel(); proVM.ProductID = pro.ProductID; proVM.ProductName = pro.ProductName; string r = string.Empty; foreach (char obj in proVM.ProductName) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } if (q.Length == 1)//alphabetical search, first letter { ViewBag.LetraAlfabetica = q; if (r.StartsWith(q) || proVM.ProductName.ToUpper().StartsWith(q)) { proVM.Provider = pro.Provider; proVM.ProviderID = pro.ProviderID; proVM.Price = pro.Price; proVM.CNYSellPrice = pro.CNYSellPrice; proVM.CNYPrice = pro.Price * AUDCNYRate; proVM.UserID = pro.UserID; productsVM.Add(proVM); } } else if (q.Length > 1) { //normal search if (r.IndexOf(q) > -1 || proVM.ProductName.ToUpper().IndexOf(q) >-1) { proVM.Provider = pro.Provider; proVM.ProviderID = pro.ProviderID; proVM.Price = pro.Price; proVM.CNYSellPrice = pro.CNYSellPrice; proVM.CNYPrice = pro.Price * AUDCNYRate; proVM.UserID = pro.UserID; productsVM.Add(proVM); } } } } int currentPageIndex = page.HasValue ? page.Value - 1 : 0; var productsListPaged = productsVM.ToPagedList(currentPageIndex, defaultPageSize); if (Request.IsAjaxRequest()) { return PartialView("Index", productsListPaged); } else return View("Index",productsListPaged); }
static void Main(string[] args) { foreach(var a in args) { Console.Write(a); } string inFileName=""; string outFileName=""; string option = null; string help="\n"; help += " ---------------------------------zhCn2PinYin--------------------------------\n"; help += " --- zhCn2PinYin.exe Version 0.0.1 ---\n"; help += " --- Author Kean (c) 2015-08-10 ---\n"; help += " --- Microsoft Visual Studio International Pack 1.0 ---\n"; help += " --- usage: [name.exe] [inFileName] [outFileName] [tone] [noPinyin] ---\n"; help += " --- eg: zhCn2PinYin.exe in.txt out.txt tone ---\n"; help += " --- eg: zhCn2PinYin.exe in.txt out.txt noPinyin ---\n"; help += " --- eg: zhCn2PinYin.exe in.txt out.txt ---\n"; help += " --- Warning: Much Datas to Convert Will Taken Much Time!( :) <= 1MB) ---\n"; help += " ---------------------------------zhCn2PinYin--------------------------------\n"; Console.Write(help); if(args.Length>0) { outFileName = "Hanzi2PinYin.txt"; if(args.Length>=1) { inFileName=args[0]; } if(args.Length>=2) { outFileName = args[1]; } if(args.Length>=3) { option = args[2].ToLower(); } if(args.Length>3) { Console.Write("\nLook Up To Help!!!!!!!!!!\n"); } } else { Console.Write("In File Name:"); inFileName = Console.ReadLine(); Console.Write("Out File Name:"); outFileName = Console.ReadLine(); Console.Write("Output Option: Tone or NoPinYin or AnyKey else?"); option = Console.ReadLine().ToLower(); } FileStream outStream = new FileStream(outFileName, FileMode.Create); StreamWriter cout = new StreamWriter(outStream); StreamReader cin; try { cin = new StreamReader(inFileName, Encoding.Default); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine("Press any Key to end."); Console.Read(); return ; } String line; while ((line = cin.ReadLine()) != null) { if (line.Length < 1) continue; string simple=ChineseConverter.Convert(line,ChineseConversionDirection.TraditionalToSimplified); int cnt = -1; foreach (var ch in simple) { ++cnt; if (!ChineseChar.IsValidChar(ch)) continue; if(line[cnt]!=ch) { cout.Write(":" + line[cnt].ToString()+","); } cout.Write(ch.ToString()); if (option == "nopinyin") { cout.Write("\r\n"); continue; } try { ChineseChar chineseChar = new ChineseChar(ch); var pinyins = chineseChar.Pinyins; //因为一个汉字可能有多个读音,pinyins是一个集合 String currOne = null; String lastOne = null; foreach (var pinyin in pinyins) //下面的方法只是简单的获得了集合中第一个非空元素 { if (pinyin != null) { if (option == "tone")//输出音调 { currOne = pinyin.Substring(0, pinyin.Length); cout.Write("," + currOne.ToLower()); } else if(option!="nopinyin")//不用音调 { lastOne = currOne; currOne = pinyin.Substring(0, pinyin.Length - 1);//拼音的最后一个字符是音调 if(lastOne!=currOne) cout.Write("," + currOne.ToLower()); } } } cout.Write("\r\n"); } catch (Exception ) { ; } } } cin.Close(); cout.Flush(); //清空缓冲区 cout.Close(); //关闭流 outStream.Close(); if (args.Length > 0) return; Console.WriteLine("Press Any Key to End."); Console.Read(); }
/// <summary> /// 获取拼音字符串 /// </summary> /// <param name="wordStr">汉字数组</param> /// <param name="HeadOrAll">true为Head,false为All</param> /// <returns></returns> private string GetSpellStr(string wordStr, bool HeadOrAll) { if (string.IsNullOrEmpty(wordStr)) { return string.Empty; } // 匹配中文字符 Regex regexChinese = new Regex("^[\u4e00-\u9fa5]$"); string result = string.Empty; string[] tmpArr; char[] arrayChars = wordStr.ToArray<char>(); foreach (char item in arrayChars) { if (regexChinese.IsMatch(item.ToString())) { ChineseChar data = new ChineseChar(item); if (data.PinyinCount > 0) { if (!string.IsNullOrEmpty(result)) { tmpArr = result.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); result = string.Empty; for (int i = 0; i < tmpArr.Length; i++) { for (int j = 0; j < data.PinyinCount; j++) { string pin = data.Pinyins[j]; if (!string.IsNullOrEmpty(pin)) { if (HeadOrAll) { result += tmpArr[i] + pin[0] + ","; } else { result += tmpArr[i] + pin.Substring(0, pin.Length - 1) + ","; } } } } tmpArr = result.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string finallyresult = string.Empty; List<string> tempList = new List<string>(); foreach (string temp in tmpArr) { if (tempList.Contains(temp) == false) { tempList.Add(temp); finallyresult += temp + ","; } } if (finallyresult.EndsWith(",")) { result = finallyresult.Substring(0, finallyresult.Length - 1); } } else { for (int i = 0; i < data.PinyinCount; i++) { string pin = data.Pinyins[i]; if (!string.IsNullOrEmpty(pin)) { if (HeadOrAll) { if (!result.Contains(pin[0])) { result += pin[0] + ","; } } else { if (!result.Contains(pin.Substring(0, pin.Length - 1))) { result += pin.Substring(0, pin.Length - 1) + ","; } } } } if (result.EndsWith(",")) { result = result.Substring(0, result.Length - 1); } } } } else { result += item; } } return result; }
/// <summary> /// 获取汉字的拼音 /// </summary> /// <param name="targetValue">目标汉字</param> /// <param name="pinYinOnly">只要拼音</param> /// <returns>拼音</returns> public static string GetPinyin(string targetValue, bool pinYinOnly = true) { string returnValue = string.Empty; if (!string.IsNullOrEmpty(targetValue)) { foreach (Char c in targetValue) { if (ChineseChar.IsValidChar(c)) { ChineseChar chineseChar = new ChineseChar(c); // 汉字的所有拼音拼写 ReadOnlyCollection<string> pinyins = chineseChar.Pinyins; returnValue += pinyins[0].Substring(0, pinyins[0].Length - 1).ToLower(); } else { if (!pinYinOnly) { returnValue += c; } } } } return returnValue; }
/// <summary> /// 使用 Microsoft Visual Studio International Pack v1.0 中的 /// Simplified Chinese Pin-Yin Conversion Library 来处理 汉语拼音. /// </summary> /// <param name="iChar"></param> public void Test(char iChar) { ChineseChar chineseChar = new ChineseChar(iChar); Console.WriteLine("{0} 是否是多音字:{1} ", iChar, chineseChar.IsPolyphone); ReadOnlyCollection<string> pinyin = chineseChar.Pinyins; foreach (string pin in pinyin) { if (!String.IsNullOrEmpty(pin)) { Console.WriteLine(iChar + " 汉语拼音为 " + pin); } } }
private string GetPinYin(string str) { StringBuilder sbPinYin = new StringBuilder(); foreach (char ch in str) { ChineseChar cc = new ChineseChar(ch); string strPinYin = cc.Pinyins[0].Trim(); if (ch == '万') { strPinYin = "WAN4"; } strPinYin = strPinYin.Remove(strPinYin.Length - 1).ToLower(); sbPinYin.Append(strPinYin.Substring(0, 1).ToUpper() + strPinYin.Substring(1)); } return sbPinYin.ToString(); }