码迷,mamicode.com
首页 > 其他好文 > 详细

生成拼音

时间:2019-04-07 21:48:58      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:sele   oid   tin   HERE   uml   build   dump   lse   mat   

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();
    }
}

 

生成拼音

标签:sele   oid   tin   HERE   uml   build   dump   lse   mat   

原文地址:https://www.cnblogs.com/nanfei/p/10667238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!