Example #1
0
        public string TestVCharType(char ch, HanyuPinyinVCharType vcharType)
        {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.VCharType = vcharType;
            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]);
        }
Example #2
0
        private string pinyin(string chinese)
        {
            try
            {
                var a = chinese.ToCharArray()
                        .Select(p =>
                {
                    //if (char.IsLetter(p)) return p;

                    var v = PinyinHelper.ToHanyuPinyinStringArray(p);
                    if (v == null || !v.Any() || !v.First().Any())
                    {
                        return(p);
                    }

                    return(v.First().First());
                })
                        .ToArray();

                return(string.Join("", a));
            }
            catch (Exception e)
            {
                log.WarnFormat("ex @ pinyin {0} | {1}", chinese, e.Message);
                return(chinese);
            }
        }
Example #3
0
        public static string ConvertPinyin(char c, string isFormat)
        {
            CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            TextInfo    text        = cultureInfo.TextInfo;

            Pinyin4net.Format.HanyuPinyinOutputFormat format = new Pinyin4net.Format.HanyuPinyinOutputFormat();
            format.CaseType  = Pinyin4net.Format.HanyuPinyinCaseType.LOWERCASE;
            format.ToneType  = Pinyin4net.Format.HanyuPinyinToneType.WITHOUT_TONE;
            format.VCharType = Pinyin4net.Format.HanyuPinyinVCharType.WITH_U_UNICODE;
            var py = PinyinHelper.ToHanyuPinyinStringArray(c, format);

            if (py == null)
            {
                return(string.Empty);
            }

            if (isFormat == "big")
            {
                return(text.ToTitleCase(py.FirstOrDefault()));
            }
            else
            {
                return(py.FirstOrDefault());
            }
        }
Example #4
0
        public static string ConvertToPinYin(char ch)
        {
            string pinyinStr = string.Empty;

            if (!string.IsNullOrWhiteSpace(ch.ToString()))
            {
                string[] stringArr = PinyinHelper.ToHanyuPinyinStringArray(ch, format);
                if (stringArr != null)
                {
                    if (stringArr.Count() > 1)
                    {
                        pinyinStr = stringArr[0];
                    }
                    else
                    {
                        pinyinStr = stringArr[0];
                    }
                    //foreach (var s in stringArr)
                    //{
                    //    pinyinStr += s;
                    //}
                }
            }
            return(pinyinStr);
        }
Example #5
0
        /// <summary>
        /// 简体中文转拼音
        /// </summary>
        /// <param name="chs">简体中文字</param>
        /// <returns>拼音</returns>
        private static string ChsToPinYin(this string chs)
        {
            var myRegex   = new Regex("^[\u4e00-\u9fa5]$");
            var returnstr = "";
            var nowchar   = chs.ToCharArray();

            for (var j = 0; j < nowchar.Length; j++)
            {
                if (myRegex.IsMatch(nowchar[j].ToString()))
                {
                    var pingStrs = PinyinHelper.ToHanyuPinyinStringArray(nowchar[j], format);
                    if (pingStrs.Any())
                    {
                        returnstr += pingStrs[0];
                    }
                    else
                    {
                        returnstr += nowchar[j].ToString();
                    }
                }
                else
                {
                    returnstr += nowchar[j].ToString();
                }
            }
            return(returnstr);
        }
Example #6
0
        public void TestToneMarkWithUAndColon(char ch, HanyuPinyinVCharType vcharType)
        {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.ToneType  = HanyuPinyinToneType.WITH_TONE_MARK;
            format.VCharType = vcharType;
            PinyinHelper.ToHanyuPinyinStringArray(ch, format);
        }
Example #7
0
        public void TestToneMarkWithUAndColon(char ch, HanyuPinyinVCharType vcharType)
        {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.ToneType  = HanyuPinyinToneType.WITH_TONE_MARK;
            format.VCharType = vcharType;
            Assert.Throws <InvalidHanyuPinyinFormatException>(() => PinyinHelper.ToHanyuPinyinStringArray(ch, format));
        }
Example #8
0
        public string TestToneMark(char ch)
        {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.ToneType  = HanyuPinyinToneType.WITH_TONE_MARK;
            format.VCharType = HanyuPinyinVCharType.WITH_U_UNICODE;
            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]);
        }
Example #9
0
        public void TestVCharType(char ch, HanyuPinyinVCharType vcharType, string result)
        {
            var format = new HanyuPinyinOutputFormat
            {
                VCharType = vcharType
            };

            Assert.Equal(result, PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]);
        }
Example #10
0
        public string TestToneMark(char ch)
        {
            var format = new HanyuPinyinOutputFormat {
                ToneType  = HanyuPinyinToneType.WithToneMark,
                VCharType = HanyuPinyinVCharType.WithUUnicode
            };

            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format) [0]);
        }
Example #11
0
        public void TestToneMarkWithUAndColon(char ch, HanyuPinyinVCharType vcharType)
        {
            var format = new HanyuPinyinOutputFormat {
                ToneType  = HanyuPinyinToneType.WithToneMark,
                VCharType = vcharType
            };

            Assert.Throws <InvalidHanyuPinyinFormatException> (() => PinyinHelper.ToHanyuPinyinStringArray(ch, format));
        }
Example #12
0
        public string TestCaseType(char ch, HanyuPinyinVCharType vcharType)
        {
            var format = new HanyuPinyinOutputFormat {
                CaseType  = HanyuPinyinCaseType.Uppercase,
                VCharType = vcharType
            };

            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format) [0]);
        }
Example #13
0
        public string TestWithToneNumber(
            char ch, HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType)
        {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.ToneType  = HanyuPinyinToneType.WITH_TONE_NUMBER;
            format.VCharType = vcharType;
            format.CaseType  = caseType;
            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]);
        }
Example #14
0
        public void TestToneMark(char ch, string result)
        {
            var format = new HanyuPinyinOutputFormat
            {
                ToneType  = HanyuPinyinToneType.WITH_TONE_MARK,
                VCharType = HanyuPinyinVCharType.WITH_U_UNICODE
            };

            Assert.Equal(result, PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]);
        }
Example #15
0
        public string[] TestCharWithMultiplePronouciations(
            char ch, HanyuPinyinToneType toneType,
            HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType)
        {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.ToneType  = toneType;
            format.VCharType = vcharType;
            format.CaseType  = caseType;
            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format));
        }
Example #16
0
        public void TestWithToneNumber(char ch, HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType, string result)
        {
            var format = new HanyuPinyinOutputFormat
            {
                ToneType  = HanyuPinyinToneType.WITH_TONE_NUMBER,
                VCharType = vcharType,
                CaseType  = caseType
            };

            Assert.Equal(result, PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]);
        }
Example #17
0
        public string TestWithToneNumber(
            char ch, HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType)
        {
            var format = new HanyuPinyinOutputFormat {
                ToneType  = HanyuPinyinToneType.WithToneNumber,
                VCharType = vcharType,
                CaseType  = caseType
            };

            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format) [0]);
        }
Example #18
0
 private static char getShouZiMuChar(char ch)
 {
     string[] pinyinArray = PinyinHelper.ToHanyuPinyinStringArray(ch, outputFormat);
     if (pinyinArray == null)
     {
         return(ch);
     }
     else
     {
         return(pinyinArray.First().First());
     }
 }
Example #19
0
        public string[] TestCharWithMultiplePronunciations(
            char ch, HanyuPinyinToneType toneType,
            HanyuPinyinVCharType vcharType, HanyuPinyinCaseType caseType)
        {
            var format = new HanyuPinyinOutputFormat {
                ToneType  = toneType,
                VCharType = vcharType,
                CaseType  = caseType
            };

            return(PinyinHelper.ToHanyuPinyinStringArray(ch, format));
        }
Example #20
0
        public void TestCharWithMultiplePronouciations(
            char ch, HanyuPinyinToneType toneType, HanyuPinyinVCharType vcharType,
            HanyuPinyinCaseType caseType, string[] result)
        {
            var format = new HanyuPinyinOutputFormat
            {
                ToneType  = toneType,
                VCharType = vcharType,
                CaseType  = caseType
            };

            Assert.True(Enumerable.SequenceEqual(result, PinyinHelper.ToHanyuPinyinStringArray(ch, format)));
        }
Example #21
0
 private static char[] getShouZiMuArray(char ch)
 {
     string[] pinyinArray = PinyinHelper.ToHanyuPinyinStringArray(ch, outputFormat);
     if (pinyinArray == null)
     {
         return new char[] { ch }
     }
     ;
     else
     {
         return(pinyinArray.Select(e => e.First()).Distinct().ToArray());
     }
 }
Example #22
0
        public static string ChineseToPinyin(this string s)
        {
            var sb = new StringBuilder();

            foreach (var c in s)
            {
                if (char.GetUnicodeCategory(c) == UnicodeCategory.OtherLetter)
                {
                    sb.Append(string.Join(" ", Program.GetCulture()
                                          .TextInfo.ToTitleCase(string.Join(" ",
                                                                            PinyinHelper.ToHanyuPinyinStringArray(c)[0]))) + " ");
                }
                else
                {
                    sb.Append(c);
                }
            }
            return(sb.ToString().Trim());
        }
Example #23
0
        public static string GetInitials(string chineseString)
        {
            if (string.IsNullOrWhiteSpace(chineseString))
            {
                return(string.Empty);
            }

            Func <char, string> getPinYin = c => {
                string[] pinYin = PinyinHelper.ToHanyuPinyinStringArray(c);
                if (pinYin == null)
                {
                    return(c.ToString());
                }
                var one = pinYin[0];
                return(one[0].ToString());
            };
            var result = chineseString.ToCharArray().Select(getPinYin).ToArray();

            return(string.Join("", result));
        }
Example #24
0
        /// <summary>
        /// 汉字转换拼音
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static string ToPinYin(this string self)
        {
            if (self.NullEmpty())
            {
                return(string.Empty);
            }
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

            format.ToneType = HanyuPinyinToneType.WITHOUT_TONE;
            StringBuilder sb = new StringBuilder();

            foreach (var item in self)
            {
                string[] pinyinStr = PinyinHelper.ToHanyuPinyinStringArray(item, format);
                sb.Append(pinyinStr.JoinBy(" "));
            }

            //return NPinyin.Pinyin.GetPinyin(self, Encoding.UTF8);

            return(sb.ToString());
        }
Example #25
0
        public static string GetPinYin(string chineseString)
        {
            if (string.IsNullOrWhiteSpace(chineseString))
            {
                return(string.Empty);
            }
            Func <char, string> getPinYin = c => {
                HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
                format.ToneType = HanyuPinyinToneType.WITHOUT_TONE;
                string[] pinYin = PinyinHelper.ToHanyuPinyinStringArray(c, format);
                if (pinYin == null)
                {
                    return(c.ToString());
                }
                var one = pinYin[0];
                return(one);
            };
            var result = chineseString.ToCharArray().Select(getPinYin).ToArray();

            return(string.Join("", result));
        }
        public string[] Convert(char chineseCharacter, EToneType toneType)
        {
            var format = new HanyuPinyinOutputFormat
            {
                CaseType  = HanyuPinyinCaseType.Lowercase,
                ToneType  = HanyuPinyinToneType.WithToneMark,
                VCharType = HanyuPinyinVCharType.WithUUnicode
            };

            switch (toneType)
            {
            case EToneType.Without:
                format.ToneType = HanyuPinyinToneType.WithoutTone;
                break;

            case EToneType.Number:
                format.ToneType = HanyuPinyinToneType.WithToneNumber;
                break;
            }

            return(PinyinHelper.ToHanyuPinyinStringArray(chineseCharacter, format));
        }
Example #27
0
        static void Main(string[] args)
        {
            var luckyArray = new[] { 1, 3, 5, 6, 7, 8, 11, 13, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 31, 32, 33, 35, 37, 41, 45, 47, 48, 49, 52, 57, 61, 63, 65, 67, 68, 81 };

            var a = 15;  //林

            var numList = new List <(int, int)>();

            for (int b = 1; b < 23; b++)
            {
                for (int c = 1; c < 23; c++)
                {
                    var 天格 = a + 1;
                    var 人格 = a + b;
                    var 地格 = b + c;
                    var 外格 = c + 1;
                    var 总格 = a + b + c;
                    if (luckyArray.Contains(人格) &&
                        luckyArray.Contains(地格) &&
                        luckyArray.Contains(外格) &&
                        luckyArray.Contains(总格))
                    {
                        numList.Add((b, c));
                    }
                }
            }

            var array = Dictionary.Split("画:").SelectMany(x => x.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)).Where(x => x.Contains("(")).ToArray();

            var charList = array.SelectMany(x =>
            {
                var index = Array.IndexOf(array, x);

                var chars = x.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                return(chars.Select(y =>
                {
                    var ar1 = y.Split(new[] { "(", ")" }, StringSplitOptions.RemoveEmptyEntries);
                    return new CharProperty
                    {
                        Char = ar1[0],
                        FiveElement = Enum.Parse <FiveElementEnum>(ar1[1]),
                        StrokeNumber = index + 1,
                        PinYin = new PinYin(PinyinHelper.ToHanyuPinyinStringArray(ar1[0].ToCharArray()[0])[0])
                    };
                }));
            }).ToList();

            var exclude = @"芫芩芪笈午荀烜两极老纭桔打茹姐妹底萌炳桃菊綦魁菁郢萤瑗日月卞金木水火土琴仇牙铭熙耿炯婷仪凤玲鹏诚人犬牛刁刀芬娟
花乳使娓旃针笫站笋珍芙哥芸二又子口它兄母五弓己廿女土兀上下半包北丙必布氐卉甲巨抖昉杰荃吉
卯末丕目去仨他戊只仔但弟佃囤攻罕妞汝妥伍卡巳靠鼐万暴凯铭娘鑫祥嘉伟亮鸿俚尺耳强耒长短止仉
局岑囯局桓闳茼荃筌祁集棋吾言戎竹岛疾笆祗羌邕纨杷";

            var list1 = charList.Where(x => !exclude.Contains(x.Char) &&
                                       x.StrokeNumber < 15 &&
                                       (x.FiveElement == FiveElementEnum.火 ||
                                        x.FiveElement == FiveElementEnum.木) &&
                                       x.PinYin.Initials != "y" &&
                                       x.PinYin.Final != "e" &&
                                       x.PinYin.Tone != 4);
            var list2 = charList.Where(x => !exclude.Contains(x.Char) &&
                                       x.StrokeNumber < 15 &&
                                       x.FiveElement == FiveElementEnum.木 &&
                                       x.PinYin.Tone < 3);

            var nameList = numList.SelectMany(x =>
                                              list1.Where(y =>
                                                          y.StrokeNumber == x.Item1).SelectMany(y =>
                                                                                                list2.Where(z =>
                                                                                                            z.StrokeNumber == x.Item2 &&
                                                                                                            z.PinYin.Initials != y.PinYin.Initials &&
                                                                                                            z.PinYin.Final != y.PinYin.Final &&
                                                                                                            z.PinYin.Tone != y.PinYin.Tone).Select(z =>
                                                                                                                                                   $"叶{y}{z}"))).ToList();

            var result = string.Join("、", nameList /*.Where(x => x.Contains('洋'))*/);

            Console.WriteLine(@"
--------------------------------------------------------");
            Console.WriteLine(result);
            Console.WriteLine(@"
--------------------------------------------------------");
            Console.WriteLine($"共 {nameList.Count} 个");
        }
Example #28
0
        private void FetchAll(object sender, EventArgs e)
        {
            if (ListViewFetching.Items.Count == 0)
            {
                return;
            }
            IsLoading = true;
            string startTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            (new Thread(new ThreadStart(delegate {
                for (int i = 0; i < ListViewFetching.Items.Count; i++)
                {
                    ListViewItem item = ListViewFetching.Items[i];
                    List <FileInfo> re = Fetch(item.SubItems[0].Text, item.SubItems[1].Text);
                    foreach (FileInfo r in re)
                    {
                        char[] nameCharArray = r.Name.ToCharArray();
                        string pinyin = "";
                        foreach (char nc in nameCharArray)
                        {
                            string[] pinyinGroup = PinyinHelper.ToHanyuPinyinStringArray(nc);
                            if (pinyinGroup != null && pinyinGroup.Length > 0)
                            {
                                pinyin += pinyinGroup[0].Substring(0, pinyinGroup[0].Length - 1);
                            }
                            else
                            {
                                pinyin += nc;
                            }
                        }
                        try {
                            DBLinker.Execute($@"
                                insert into files values(
                                    '{ r.Directory }',
                                    '{ r.Name }',
                                    '{ r.Extension.Replace(".", "") }',
                                    '',
                                    '{ pinyin }',
                                    '{ startTime }'
                                );
                            ");
                        } catch { }
                    }
                    // difference (in seconds) between now and startTime
                    TimeSpan ts = DateTime.Now - Convert.ToDateTime(startTime);
                    long secondsUsed = (long)ts.TotalSeconds;
                    // set fetch log
                    DBLinker.Execute($@"
                        insert into fetchLog values(
                            '{ startTime }',
                            '{ item.SubItems[0].Text }',
                            '{ re.Count.ToString() }',
                            '{ secondsUsed }'
                        );
                    ");
                }
                // delete old data
                DBLinker.Execute($@"
                    delete from files where updateTime != '{ startTime }';
                ");
                GetFileList();
                GetFetchLog();
                GetRunLog();
                IsLoading = false;
            }))).Start();
        }
Example #29
0
 public void TestNullInput()
 {
     Assert.Throws <ArgumentNullException>(() => PinyinHelper.ToHanyuPinyinStringArray('李', null));
 }
Example #30
0
 public void TestNonChineseCharacter(char ch)
 {
     Assert.IsNull(PinyinHelper.ToHanyuPinyinStringArray(ch));
 }