//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); } } }
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); }
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="chinese"></param> /// <returns></returns> private static string SpecialAllTrans(char chinese) { string _pinyin = string.Empty; if(ChineseChar.IsValidChar(chinese) && !char.IsWhiteSpace(chinese)) { switch(chinese) { case '区': _pinyin = "QU"; break; case '家': _pinyin = "JIA"; break; case '弄': _pinyin = "NONG"; break; case '藏': _pinyin = "ZANG"; break; default: ChineseChar _chineseChar = new ChineseChar(chinese); _pinyin = _chineseChar.Pinyins[0].Substring(0, _chineseChar.Pinyins[0].Length - 1); break; } } return _pinyin; }
public string ToSpellFull(string words) { var spell = new StringBuilder(); foreach (var word in words.Trim()) { if (ChineseChar.IsValidChar(word)) { var spellsOfWord = GetSpellsOfWord(word); if (spellsOfWord.Count > 1) { spell.AppendFormat("({0})", string.Join("|", spellsOfWord)); } else { spell.Append(spellsOfWord[0]); } } else { spell.Append(word); } } return(spell.ToString()); }
private void btnPinyin_Click(object sender, EventArgs e) { string chs = txtCHS.Text.Trim(); if (chs.Length == 0) { return; } StringBuilder fullSpell = new StringBuilder(); foreach (char c in chs) { if (ChineseChar.IsValidChar(c)) { ChineseChar chschar = new ChineseChar(c); string pin = chschar.Pinyins[0]; pin = pin.Remove(pin.Length - 1); fullSpell.Append(pin); fullSpell.Append(" "); } else { fullSpell.Append(c); fullSpell.Append(" "); } } lblPinyin.Text = fullSpell.ToString(); }
/// <summary> /// 获取转换后的笛卡尔积结果 /// </summary> /// <param name="words">需要转换的字符串</param> /// <returns></returns> private static IEnumerable <IEnumerable <string> > GetPinyinCartesianProduct(string words) { var list = new List <List <string> >(); var array = words.Trim().ToCharArray(); foreach (var item in array) { if (string.IsNullOrWhiteSpace(item.ToString())) { continue; } if (!ChineseChar.IsValidChar(item)) { var self = new List <string> { item.ToString() }; list.Add(self); continue; } var pinyin = new ChineseChar(item); var pinyins = pinyin.Pinyins.Where(s => !string.IsNullOrWhiteSpace(s)) .Select(s => s.Substring(0, s.Length - 1)) .Distinct() .ToList(); list.Add(pinyins); } return(CartesianProduct(list)); }
/// <summary> /// 获取拼音 /// </summary> /// <param name="chinese"></param> /// <param name="ignoreTone">是否忽略音调</param> /// <returns>默认大写</returns> public static string ToPinYin(this string chinese, bool ignoreTone = true) { StringBuilder sb = new StringBuilder(); foreach (char ch in chinese) { if (Regex.IsMatch(ch.ToString(), "[a-zA-Z]")) { sb.Append(ch); } else if (ChineseChar.IsValidChar(ch)) { ChineseChar cc = new ChineseChar(ch); var arr = cc.Pinyins; if (arr == null || arr.Count == 0) { continue; } string name = arr[0]; if (ignoreTone) { name = Regex.Replace(name, @"\d", string.Empty); } sb.Append(name); } } return(sb.ToString()); }
/// <summary> /// 获得中文字符串的拼音--返回多音字不同拼写的组合(多音字(拼写、读音)) /// </summary> /// <param name="str"></param> /// <returns></returns> public static List <string> GetPinYins(string str) { //string py = ""; List <string> pinYins = new List <string>() { "" }; for (int i = 0; i < str.Length; i++) { List <string> charPinYins = new List <string>(); if (ChineseChar.IsValidChar(str[i])) //判断当前字符是不是汉字 { ChineseChar cc = new ChineseChar(str[i]); //构造方法 //py += cc.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToLower(); charPinYins = cc.Pinyins.Where(p => p != null).Select(p => p.TrimEnd('1', '2', '3', '4', '5').ToLower()).Distinct().ToList(); } else//不是汉字的话 加本身 { //py += str[i]; charPinYins.Add(str[i].ToString()); } pinYins = pinYins.Join(charPinYins, p => 1, pc => 1, (p, pc) => p + pc).ToList(); } //return py; return(pinYins); }
public static string ToPinYin(string _chinese) { string result = ""; foreach (char c in _chinese) { if (ChineseChar.IsValidChar(c)) { ChineseChar cc = new ChineseChar(c); foreach (string ccp in cc.Pinyins) { if (!string.IsNullOrWhiteSpace(ccp)) { result += Regex.Replace(ccp, @"[0-9]", "").ToLower() + " "; break; } } } else if (c.ToString() == "\r\n" || c.ToString() == "\n" || c.ToString() == "\t" || c.ToString() == " ") { result = result.Trim() + "\r\n"; } } return(ReplaceMultipleSpaceToOneSpace(result.Trim())); }
private static Bookmark RetriveBookmark(SetRecord record) { string name = record.ReadAs <string>("name"); ChineseChar ch; StringBuilder builder = new StringBuilder(name.Length); foreach (char c in name) { if (ChineseChar.IsValidChar(c)) { ch = new ChineseChar(c); builder.Append(ch.Pinyins[0]); builder.Remove(builder.Length - 1, 1); } else { builder.Append(c); } } string pinyin = builder.ToString().ToLower(); return(new Bookmark() { Name = name, Url = record.ReadAs <string>("url"), NamePinYin = pinyin }); }
public static string GetFullPing(string str, string separator = "_") { var pinyins = new List <string>(); var chs = str.ToCharArray(); for (int i = 0; i < chs.Length; i++) { var ch = chs[i]; if (ChineseChar.IsValidChar(ch)) { ChineseChar cc = new ChineseChar(ch); string v = cc.Pinyins[0]; if (v.Length > 1) { v = v.Substring(0, v.Length - 1).ToLower().FirstUpper(); } pinyins.Add(v); } else { pinyins.Add(ch.ToString()); } } return(string.Join(separator, pinyins.ToArray())); }
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="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> /// Gets pinyin text of <paramref name="s"/>. /// </summary> /// <param name="s"></param> /// <returns></returns> public static string GetPinyin(string s) { if (string.IsNullOrWhiteSpace(s)) { return(s); } string pinyin = null; StringBuilder result = new StringBuilder(s.Length * 5); foreach (char c in s) { try { if (ChineseChar.IsValidChar(c)) { pinyin = new ChineseChar(c).Pinyins[0].ToLower(); result.Append(char.ToUpper(pinyin[0])); if (pinyin.Length > 2) { result.Append(pinyin, 1, pinyin.Length - 2); } } else { result.Append(c); } } catch (NotSupportedException) { result.Append(c); } } return(result.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()); }
/// <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()); }
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()); }
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()); }
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="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()); }
/// <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="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)); }
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); }
/// <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="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()); }
public void Test() { var a = ChineseChar.IsValidChar('4'); var Aa = ChineseChar.IsValidChar('a'); var aaa = ChineseChar.IsValidChar(','); var aaaa = ChineseChar.IsValidChar('汉'); var cc = new ChineseChar('汉'); }
/// <summary> /// 首字母拼音 /// </summary> /// <param name="chineseString"></param> /// <returns></returns> public static string ToShortPinyin(string chineseString) { if (string.IsNullOrEmpty(chineseString)) { return(""); } var hardcodestring = hardCodeStrings.FirstOrDefault(item => chineseString.Contains(item.ChineseString)); if (hardcodestring != null) { chineseString = chineseString.Replace(hardcodestring.ChineseString, hardcodestring.ShortPinYin); } var sb = new StringBuilder(chineseString.Length * 4); byte[] arr = new byte[2]; //每个汉字为2字节 int charCode = 0; int arr1 = 0; int arr2 = 0; for (int j = 0; j < chineseString.Length; j++) //遍历输入的字符 { arr = System.Text.Encoding.UTF8.GetBytes(chineseString[j].ToString()); //根据系统默认编码得到字节码 if (arr.Length == 1) //如果只有1字节说明该字符不是汉字,结束本次循环 { sb.Append(chineseString[j]); continue; } arr1 = (short)(arr[0]); //取字节1 arr2 = (short)(arr[1]); //取字节2 charCode = arr1 * 256 + arr2 - 65536; //计算汉字的编码 if (charCode > -10254 || charCode < -20319) //如果不在汉字编码范围内则不改变 { if (ChineseChar.IsValidChar(chineseString[j])) { ChineseChar chineseChar = new ChineseChar(chineseString[j]); string t = chineseChar.Pinyins.Last(item => item != null); sb.Append(t.Substring(0, 1)); } else { sb.Append(chineseString[j]); } } else { //根据汉字编码范围查找对应的拼音并保存到结果中 //由于charCode的键不一定存在,所以要找比他更小的键上一个键 string charPinyin = getPinyin(charCode); if (!String.IsNullOrEmpty(charPinyin)) { charPinyin = new string(charPinyin[0], 1); } sb.Append(charPinyin); } } return(sb.ToString().ToUpper()); }
public static bool IsCn(string str) { char[] chars = str.ToCharArray(); foreach (char cha in chars) { if (!ChineseChar.IsValidChar(cha)) { return(false); } } return(true); }
private void comMachineLeader_KeyUp(object sender, KeyEventArgs e) { if (comMachineLeader.Text.Trim().Length <= 0) { return; } List <string> username = new List <string>(); if (comProcessName.SelectedItem != null && comProcessName.SelectedItem.ToString() != "") { var process_name = comProcessName.Text.ToString(); var process_type = dic_processType.Where(x => x.ProcessName == process_name).Select(x => x.ProcessType).First(); //user_info = (from p in db.D_UserInfo select p).ToList<D_UserInfo>(); List <D_UserInfo> user = new List <D_UserInfo>(); user = user_info.FindAll(delegate(D_UserInfo s) { return(s.processtype == process_type); }); foreach (D_UserInfo s in user) { username.Add(s.username); } } else { username = user_name; } List <string> tmp = new List <string>(); tmp = username.FindAll(delegate(string s) { char[] chs = { ' ' }; StringBuilder str = new StringBuilder(); foreach (char c in s.ToCharArray()) { if (ChineseChar.IsValidChar(c)) { ChineseChar CC = new ChineseChar(c); //将该汉字转化为拼音集合 ReadOnlyCollection <string> roc = CC.Pinyins; //获取集合中第一个数据即为该汉字的拼音 chs = roc[0].ToLower().ToCharArray(); str.Append(chs[0]); } } return(str.ToString().IndexOf(comMachineLeader.Text.Trim()) == 0); }); comMachineLeader.ItemsSource = tmp; comMachineLeader.IsDropDownOpen = true; }