• 生成拼音


    void Main()
    {
        var index = 0;
        var count = CM_BookPages.Count(p=>p.PinYin == null || p.PinYin == "");
        this.Connection.Open();
        foreach (var page in CM_BookPages.Where(p=>p.PinYin == null || p.PinYin == "").ToList())
        {
            var pinYin = string.Join(",",PinYinHelper.GetAllPinYinForChineseSentence(page.Title));
            var sql = string.Format( "UPDATE CM_BookPage SET PinYin = '{0}' WHERE PageID = {1};", pinYin,page.PageID);
            var cmd = this.Connection.CreateCommand();
            cmd.CommandTimeout = 0;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            var r = cmd.ExecuteNonQuery();
            
            index += r;
            if(index % 100 ==0)
                (index.ToString() + "/" + count.ToString()).Dump();
        }
        
        this.Connection.Close();
        
        "over".Dump();
    }
    
    public static class PinYinHelper
    {
        private static Dictionary<KeyValuePair<char, int>, char> toneChars = new Dictionary<KeyValuePair<char, int>, char>();
    
        private static void InitToneChars()
        {
            toneChars.Add(new KeyValuePair<char, int>('a', 1), 'ā');
            toneChars.Add(new KeyValuePair<char, int>('a', 2), 'á');
            toneChars.Add(new KeyValuePair<char, int>('a', 3), 'ǎ');
            toneChars.Add(new KeyValuePair<char, int>('a', 4), 'à');
            toneChars.Add(new KeyValuePair<char, int>('a', 5), 'a');
            toneChars.Add(new KeyValuePair<char, int>('o', 1), 'ō');
            toneChars.Add(new KeyValuePair<char, int>('o', 2), 'ó');
            toneChars.Add(new KeyValuePair<char, int>('o', 3), 'ǒ');
            toneChars.Add(new KeyValuePair<char, int>('o', 4), 'ò');
            toneChars.Add(new KeyValuePair<char, int>('o', 5), 'o');
            toneChars.Add(new KeyValuePair<char, int>('e', 1), 'ē');
            toneChars.Add(new KeyValuePair<char, int>('e', 2), 'é');
            toneChars.Add(new KeyValuePair<char, int>('e', 3), 'ě');
            toneChars.Add(new KeyValuePair<char, int>('e', 4), 'è');
            toneChars.Add(new KeyValuePair<char, int>('e', 5), 'e');
            toneChars.Add(new KeyValuePair<char, int>('i', 1), 'ī');
            toneChars.Add(new KeyValuePair<char, int>('i', 2), 'í');
            toneChars.Add(new KeyValuePair<char, int>('i', 3), 'ǐ');
            toneChars.Add(new KeyValuePair<char, int>('i', 4), 'ì');
            toneChars.Add(new KeyValuePair<char, int>('i', 5), 'i');
            toneChars.Add(new KeyValuePair<char, int>('u', 1), 'ū');
            toneChars.Add(new KeyValuePair<char, int>('u', 2), 'ú');
            toneChars.Add(new KeyValuePair<char, int>('u', 3), 'ǔ');
            toneChars.Add(new KeyValuePair<char, int>('u', 4), 'ù');
            toneChars.Add(new KeyValuePair<char, int>('u', 5), 'u');
            toneChars.Add(new KeyValuePair<char, int>('v', 1), 'ǖ');
            toneChars.Add(new KeyValuePair<char, int>('v', 2), 'ǘ');
            toneChars.Add(new KeyValuePair<char, int>('v', 3), 'ǚ');
            toneChars.Add(new KeyValuePair<char, int>('v', 4), 'ǜ');
            toneChars.Add(new KeyValuePair<char, int>('v', 5), 'ü');
        }
    
        static PinYinHelper()
        {
            InitToneChars();
        }
    
        private static string GetTonePinYin(string jianPin, int tone)
        {
            jianPin = jianPin.ToLower();
    
            var chars = new char[] { 'a', 'e', 'i', 'o', 'u', 'v' };
            foreach (var c in chars)
            {
                var p = jianPin.LastIndexOf(c);
                if (p < 0)
                    continue;
    
                var toneKey = new KeyValuePair<char, int>(c, tone);
                return jianPin.Substring(0, p) + toneChars[toneKey] + jianPin.Substring(p + 1);
            }
    
            return string.Empty;
        }
    
        public static string GetPinYinForChineseWord(char c)
        {
            if (!ChineseChar.IsValidChar(c))
                return string.Empty;
    
            ChineseChar chineseChar = new ChineseChar(c);
            var pinYin = chineseChar.Pinyins[0];
            return GetTonePinYin(pinYin.Substring(0, pinYin.Length - 1), int.Parse(pinYin.Last().ToString()));
        }
    
        public static List<string> GetPinYinListForChineseWord(char c)
        {
            if (!ChineseChar.IsValidChar(c))
                return new List<string>() {" "};
    
            var result = new List<string>();
            var chineseChar = new ChineseChar(c);
            for (var i = 0; i < chineseChar.PinyinCount; i++)
            {
                var item = chineseChar.Pinyins[i];
                var pinYin = item.Substring(0, item.Length - 1);
                result.Add(pinYin.ToLower());
            }
            return result.Distinct().ToList();
        }
    
        public static string GetPinYinForChineseSentence(string text)
        {
            var sb = new StringBuilder();
            foreach (var c in text)
                sb.Append(GetPinYinForChineseWord(c)).Append(" ");
            return sb.ToString();
        }
        
        public static List<string> GetAllPinYinForChineseSentence(string text)
        {
            var charPinYins = new List<List<string>>();
            foreach (var c in text)
                charPinYins.Add(GetPinYinListForChineseWord(c).Select(item=>item.Replace(" ",string.Empty)).ToList());
            return GetAllSentencePinYin(charPinYins);
        }
    
        public static char GetFirstWordPinYinChar(string text)
        {
            var result = ' ';
    
            try
            {
                text = text.Trim();
                if (text.Length < 1)
                    return result;
    
                var c = text[0];
                ChineseChar chineseChar = new ChineseChar(c);
                if (chineseChar.PinyinCount < 1)
                    return c;
    
                if (chineseChar.Pinyins[0].Length < 1)
                    return c;
    
                return chineseChar.Pinyins[0][0];
            }
            catch
            {
                return result;
            }
        }
        
        public static List<string> GetAllSentencePinYin(List<List<string>> wordPinYinList)
        {
            if (wordPinYinList.Count == 0)
                return new List<string> { string.Empty };
    
            if (wordPinYinList.Count == 1)
                return wordPinYinList[0];
                
            var itemsLeft = wordPinYinList[0];
            var itemsRight = GetAllSentencePinYin(wordPinYinList.Skip(1).ToList());
            var query = from a in itemsLeft join b in itemsRight on 1 equals 1 select a + b;
            return query.ToList();
        }
    }
  • 相关阅读:
    记一个微信公众号个性化菜单的坑
    初学c# -- 学习笔记(六) winfrom组件圆角
    Swift中添加标记(MARK)和警告(TODO FIXME)
    iOS Xib布局某些控件显示或隐藏<约束的修改>
    [!] Unable to satisfy the following requirements:
    React Native使用NetInfo对当前系统网络的判断
    iOS自定义下拉列表
    [!] The version of CocoaPods used to generate the lockfile (1.4.0.beta.1) is higher than the version of the current executable (1.3.0.beta.1). Incompatibility issues may arise.
    iOS实现TextField光标居中
    iOS优秀第三方框架
  • 原文地址:https://www.cnblogs.com/nanfei/p/10667238.html
Copyright © 2020-2023  润新知