/// <summary> /// /// </summary> /// <param name="message"></param> /// <returns></returns> public static PinyinHelper Convert(string message) { if (string.IsNullOrEmpty(message)) { return(new PinyinHelper()); } //生成词典 var dict = PinyinDictionary.Instance; //只取词典中的中文词汇,无需拼音 var wordList = dict.Dictionary.Keys.ToList <string>(); //进行正向分词 var wordsLeft = Segmentation.SegMMLeftToRight(message, ref wordList); //判断分词是否正常返回 if (wordsLeft == null) { return(new PinyinHelper() { Pinyin = Hz2Py.GetPinyin(message), Szm = Hz2Py.GetFirstPinyin(message) }); } List <string> stringBuilder = new List <string>(); string pinyin; foreach (string word in wordsLeft) { //如果是单字,要检测字典中是否包含该单字 if (word.Length == 1 && !dict.Dictionary.ContainsKey(word)) { //如果词典中不包含该中文单字,就要从微软的dll库读取拼音 if (UTF8Encoding.UTF8.GetBytes(word).Length == 1) { pinyin = word; } else { pinyin = PinyinConvert.GetFirstPinYinCount(word.ToCharArray()[0]).ToUpper(); pinyin = Regex.Replace(pinyin, @"\d", ""); } } else { //一般情况不用检测,直接取词典中的拼音即可 pinyin = dict.Dictionary.ContainsKey(word) ? dict.Dictionary[word].ToUpper() : word; pinyin = Regex.Replace(pinyin, @"\d", ""); } stringBuilder.Add(pinyin); } return(new PinyinHelper() { Pinyin = string.Join("", stringBuilder.ToArray()).Replace(" ", "").Replace('\'', ' '), Szm = Hz2Py.GetFirstPinyin(message).Replace('\'', ' ') }); }
private void AddPinYin(string table) { var dbHelper = new DbHelper(this.con); var connection = new NpgsqlConnection(this.con); connection.Open(); var command = connection.CreateCommand(); var readcon = new NpgsqlConnection(this.con); readcon.Open(); var readcommand = readcon.CreateCommand(); readcommand.CommandType = CommandType.Text; dbHelper.AddColumn("quanpin", table); dbHelper.AddColumn("szm", table); Console.WriteLine(table); command.CommandType = CommandType.Text; command.CommandTimeout = 1000 * 60; command.CommandText = string.Format("SELECT COUNT(1) FROM {0} where quanpin is null", table); int rowCount = int.Parse(command.ExecuteScalar().ToString()); if (rowCount != 0) { var max = rowCount / 2000 + 1; Console.WriteLine(max); for (int i = 0; i < max; i++) { Console.WriteLine(i); readcommand.CommandText = string.Format( "SELECT gid,name from {0} where quanpin is null limit 2000", table); var reader = readcommand.ExecuteReader(); while (reader.Read()) { var gid = reader.GetInt32(0); var name = reader.GetString(1); command.CommandText = string.Format( "UPDATE {0} SET quanpin='{1}',szm='{2}' where gid = {3}", table, Hz2Py.GetPinyin(name).Replace('\'', ' '), Hz2Py.GetFirstPinyin(name).Replace('\'', ' '), gid); command.ExecuteNonQuery(); } reader.Close(); } } readcon.Close(); }