Пример #1
0
        static void Main(string[] args)
        {
            var sb          = new StringBuilder();
            var levelGroups = new[] { ChineseNumber.UpperLevels, ChineseNumber.LowerLevels };
            var nums        = new int[9].Let(i => i + 1);

            foreach (var levels in levelGroups.AsKvPairs())
            {
                foreach (var level in levels.Value.AsKvPairs())
                {
                    foreach (var num in nums)
                    {
                        var snumber     = levels.Key == 0 ? ChineseNumber.GetString(num, x => x.Upper = true) : ChineseNumber.GetString(num);
                        var str         = $"{snumber}{level.Value}";
                        var pinyin      = Pinyin.GetString(str);
                        var simplified  = ChineseConverter.ToSimplified(str);
                        var traditional = ChineseConverter.ToTraditional(str);
                        var tag         = num * Math.Pow(10, level.Key);
                        sb.AppendLine($@"{" ".Repeat(12)}new ChineseWord {{ Pinyin = ""{pinyin}"", Simplified = ""{simplified}"", Traditional = ""{traditional}"", Tag = {tag} }},");
                    }
                }
            }

            var csFile = Build(sb.ToString());

            File.WriteAllText("../../../NumberWords.cs", csFile);

            Console.WriteLine("File saved: NumberWords.cs");
        }
    private void Button_Translate_Click(object sender, RoutedEventArgs e)
    {
        AudioUtil.ClickSound();

        try
        {
            if (TextBox_InputMessage.Text != "")
            {
                var str = (e.OriginalSource as Button).Content.ToString();

                switch (str)
                {
                case "中英互译":
                    Translation();
                    break;

                case "简转繁":
                    TextBox_InputMessage.Text = ChineseConverter.ToTraditional(TextBox_InputMessage.Text);
                    break;

                case "繁转简":
                    TextBox_InputMessage.Text = ChineseConverter.ToSimplified(TextBox_InputMessage.Text);
                    break;

                case "转拼音":
                    TextBox_InputMessage.Text = Pinyin.GetString(TextBox_InputMessage.Text, PinyinFormat.WithoutTone);
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            MsgBoxUtil.Exception(ex);
        }
    }
Пример #3
0
        public void Test1()
        {
            var str = "免费,跨平台,开源!";

            Assert.Equal("mian3 fei4,kua4 ping2 tai1,kai1 yuan2!", Pinyin.GetString(str, PinyinFormat.Default));
            Assert.Equal("mian fei,kua ping tai,kai yuan!", Pinyin.GetString(str, PinyinFormat.WithoutTone));
            Assert.Equal("miǎn fèi,kuà píng tāi,kāi yuán!", Pinyin.GetString(str, PinyinFormat.PhoneticSymbol));
        }
Пример #4
0
        public void Test1()
        {
            var lexicon = new ChineseLexicon(Additional.CommonWords, Builtin.ChineseChars);

            using (var scope = lexicon.BeginScope())
            {
                var pinyin = Pinyin.GetString("我是来自重庆的重量级选手");
            }
        }
Пример #5
0
        public void Test1()
        {
            var str = "免费,跨平台,开源!";

            Assert.Equal("mian3 fei4,kua4 ping2 tai2,kai1 yuan2!", Pinyin.GetString(str, PinyinFormat.Default));
            Assert.Equal("mian fei,kua ping tai,kai yuan!", Pinyin.GetString(str, PinyinFormat.WithoutTone));
            Assert.Equal("miǎn fèi,kuà píng tái,kāi yuán!", Pinyin.GetString(str, PinyinFormat.Phonetic));
            Assert.Equal("mf,kpt,ky!", Pinyin.GetString(str, PinyinFormat.InitialConsonant));
        }
Пример #6
0
        public string Generate()
        {
            var sb          = new StringBuilder();
            var levelGroups = new[] { ChineseNumber.UpperLevels, ChineseNumber.LowerLevels };
            var nums        = new int[9].Let(i => i + 1);
            var total       = 0;

            foreach (var levels in levelGroups.AsKvPairs())
            {
                foreach (var level in levels.Value.AsKvPairs())
                {
                    foreach (var num in nums)
                    {
                        var snumber     = levels.Key == 0 ? ChineseNumber.GetString(num, x => x.Upper = true) : ChineseNumber.GetString(num);
                        var str         = $"{snumber}{level.Value}";
                        var simplified  = ChineseConverter.ToSimplified(str);
                        var traditional = ChineseConverter.ToTraditional(str);

                        var chineseWord = new ChineseWord
                        {
                            Simplified        = simplified,
                            Traditional       = traditional,
                            SimplifiedPinyin  = Pinyin.GetString(ChineseType.Simplified, simplified),
                            TraditionalPinyin = Pinyin.GetString(ChineseType.Traditional, traditional),
                            Tag = num * Math.Pow(10, level.Key),
                        };

                        var line = $"{" ".Repeat(12)}new {nameof(ChineseWord)} " +
                                   $"{{ " +
                                   $"{nameof(ChineseWord.Simplified)} = \"{chineseWord.Simplified}\"" +
                                   $", {nameof(ChineseWord.Traditional)} = \"{chineseWord.Traditional}\"" +
                                   $", {nameof(ChineseWord.SimplifiedPinyin)} = \"{chineseWord.SimplifiedPinyin}\"" +
                                   $", {nameof(ChineseWord.TraditionalPinyin)} = \"{chineseWord.TraditionalPinyin}\"" +
                                   $", {nameof(ChineseWord.Tag)} = {chineseWord.Tag}" +
                                   $" }},";
                        sb.AppendLine(line);
                        total += 1;
                    }
                }
            }

            sb.AppendLine($@"{" ".Repeat(12)}new {nameof(ChineseWord)} {{ Simplified = ""两"", Traditional = ""两"", SimplifiedPinyin = ""liang3"", TraditionalPinyin = ""liang3"", Tag = 2 }},");
            sb.AppendLine($@"{" ".Repeat(12)}new {nameof(ChineseWord)} {{ Simplified = ""两百"", Traditional = ""两百"", SimplifiedPinyin = ""liang3 bai3"", TraditionalPinyin = ""liang3 bai3"", Tag = 200 }},");
            sb.AppendLine($@"{" ".Repeat(12)}new {nameof(ChineseWord)} {{ Simplified = ""两千"", Traditional = ""两千"", SimplifiedPinyin = ""liang3 qian1"", TraditionalPinyin = ""liang3 qian1"", Tag = 2000 }},");
            total += 3;

            sb.AppendLine($@"{" ".Repeat(12)}// Total {total} words.");

            return(Wrap(sb.ToString()).Replace("she4", "shi2"));  // 拾 = shi2
        }
Пример #7
0
        public void ChineseLexiconTest()
        {
            var str = "他是重量级选手。";

            Assert.Equal("ta1 shi4 chong2 liang2 ji2 shua1 shou3。", Pinyin.GetString(str, PinyinFormat.Default));

            var words = new ChineseWord[]
            {
                new ChineseWord {
                    Pinyin = "zhong4 liang4", Simplified = "重量", Traditional = "重量"
                },
            };

            using (new ChineseLexicon(words))
            {
                Assert.Equal("ta1 shi4 zhong4 liang4 ji2 shua1 shou3。", Pinyin.GetString(str, PinyinFormat.Default));
            }
        }
Пример #8
0
        public void Test1()
        {
            using var tokenizer = new ChineseLexicon(Builtin.ChineseChars, new[]
            {
                new ChineseWord { Simplified = "中国", Traditional = "中國", Pinyin = "zhong1 guo2" },
                new ChineseWord { Word = "北京", Pinyin = "bei3 jing1" },
                new ChineseWord { Simplified = "重庆", Traditional = "重慶", Pinyin = "chong2 qing4" },
                new ChineseWord { Simplified = "直辖市", Traditional = "直轄市", Pinyin = "zhi2 xia2 shi4" },
            }).BeginScope();

            var sentence = "中国北京是直辖市,重庆也是直辖市。";
            var actual   = ChineseTokenizer.SplitWords(ChineseTypes.Simplified, sentence);
            var excepted = new[] { "中国", "北京", "是", "直辖市", ",", "重庆", "也", "是", "直辖市", "。" };
            var pinyin   = Pinyin.GetString(sentence, PinyinFormat.Phonetic);

            Assert.Equal(excepted, actual);
            Assert.Equal("zhōng guó běi jīng shì zhí xiá shì,chóng qìng yě shì zhí xiá shì。", pinyin);
        }
Пример #9
0
        public void Test1()
        {
            using var tokenizer = new ChineseLexicon(new[]
            {
                new ChineseWord { Pinyin = "zhong1 guo2", Simplified = "中国" },
                new ChineseWord { Pinyin = "bei3 jing1", Simplified = "北京" },
                new ChineseWord { Pinyin = "chong2 qing4", Simplified = "重庆" },
                new ChineseWord { Pinyin = "zhi2 xia2 shi4", Simplified = "直辖市" },
            });

            var sentence = "中国北京是直辖市,重庆也是直辖市。";
            var actual   = ChineseTokenizer.SplitWords(sentence, ChineseType.Simplified);
            var excepted = new[] { "中国", "北京", "是", "直辖市", ",", "重庆", "也", "是", "直辖市", "。" };
            var pinyin   = Pinyin.GetString(sentence, PinyinFormat.Phonetic);

            Assert.Equal(excepted, actual);
            Assert.Equal("zhōng guó běi jīng shì zhí xiá shì,chóng qìng yě shì zhí xiá shì。", pinyin);
        }
Пример #10
0
        public void ChineseLexiconTest2()
        {
            Assert.Equal("服務器中止了服務。", ChineseConverter.ToTraditional("服务器中止了服务。"));

            var words = new[]
            {
                new ChineseWord {
                    Simplified = "服务器", Traditional = "伺服器", SimplifiedPinyin = "fu2 wu4 qi4", TraditionalPinyin = "si4 fu2 qi4"
                },
            };

            using (new ChineseLexicon(Builtin.ChineseChars, words).BeginScope())
            {
                Assert.Equal("伺服器中止了服務。", ChineseConverter.ToTraditional("服务器中止了服务。"));
                Assert.Equal("fu2 wu4 qi4 zhong1 zhi3 le5 fu2 wu4。", Pinyin.GetString(ChineseTypes.Simplified, "服务器中止了服务。"));
                Assert.Equal("si4 fu2 qi4 zhong1 zhi3 le5 fu2 wu4。", Pinyin.GetString(ChineseTypes.Traditional, "伺服器中止了服務。"));
            }
        }
Пример #11
0
        public void ChineseLexiconTest()
        {
            var str    = "他是重量级选手。";
            var pinyin = Pinyin.GetString(str, PinyinFormat.Default);

            Assert.Equal("ta1 shi4 chong2 liang2 ji2 shua1 shou3。".Flow(StringFlow.HexString), pinyin.Flow(StringFlow.HexString));

            var words = new ChineseWord[]
            {
                new ChineseWord {
                    Pinyins = new[] { "zhong4 liang4" }, Simplified = "重量", Traditional = "重量"
                },
            };

            using (new ChineseLexicon(BuiltinWords.Basic, words))
            {
                Assert.Equal("ta1 shi4 zhong4 liang4 ji2 shua1 shou3。", Pinyin.GetString(str, PinyinFormat.Default));
            }
        }
Пример #12
0
        public void ChineseLexiconTest1()
        {
            var str    = "他是来自重庆的重量级选手。";
            var pinyin = Pinyin.GetString(str, PinyinFormat.Default);

            Assert.Equal("ta1 shi4 lai2 zi4 zhong4 qing4 de5 zhong4 liang4 ji2 xuan3 shou3。", pinyin);

            var words = new ChineseWord[]
            {
                new ChineseWord {
                    Simplified = "重庆", Traditional = "重慶", Pinyin = "chong2 qing4"
                },
                new ChineseWord {
                    Simplified = "重量", Traditional = "重量", Pinyin = "zhong4 liang4"
                },
            };

            using (new ChineseLexicon(Builtin.ChineseChars, words).BeginScope())
            {
                Assert.Equal("ta1 shi4 lai2 zi4 chong2 qing4 de5 zhong4 liang4 ji2 xuan3 shou3。", Pinyin.GetString(str, PinyinFormat.Default));
            }
        }
Пример #13
0
 /// <summary>
 /// 获取中文拼音
 /// </summary>
 /// <param name="Chinese"></param>
 /// <param name="format"></param>
 /// <param name="chineseType"></param>
 /// <returns></returns>
 public static string CHNPinYin(string Chinese, PinyinFormat format, ChineseTypes chineseType = ChineseTypes.Simplified)
 {
     return(Pinyin.GetString(chineseType, Chinese, format));
 }