예제 #1
0
파일: Program.cs 프로젝트: weikety/Chinese
        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");
        }
예제 #2
0
        public void MutilZeroTest()
        {
            Assert.Equal("二十亿零一", ChineseNumber.GetString(20_0000_0001));
            Assert.Equal("二十兆零一", ChineseNumber.GetString(20_0000_0000_0001));

            Assert.Equal(20_0000_0001, ChineseNumber.GetNumber("二十亿零一"));
            Assert.Equal(20_0000_0000_0001, ChineseNumber.GetNumber("二十兆零一"));
        }
예제 #3
0
        public void BigIntegerTest()
        {
            Assert.Equal("一穰二千三百四十五秭六千七百八十九垓零一百二十三京四千五百六十七兆八千九百零一亿二千三百四十五万六千七百八十九", ChineseNumber.GetString(1_2345_6789_0123_4567_8901_2345_6789m));
            Assert.Equal("壹穰贰仟叁佰肆拾伍秭陆仟柒佰捌拾玖垓零壹佰贰拾叁京肆仟伍佰陆拾柒兆捌仟玖佰零壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖", ChineseNumber.GetString(1_2345_6789_0123_4567_8901_2345_6789m, x => x.Upper = true));

            Assert.Equal(1_2345_6789_0123_4567_8901_2345_6789m, ChineseNumber.GetNumber("一穰二千三百四十五秭六千七百八十九垓零一百二十三京四千五百六十七兆八千九百零一亿二千三百四十五万六千七百八十九"));
            Assert.Equal(1_2345_6789_0123_4567_8901_2345_6789m, ChineseNumber.GetNumber("壹穰贰仟叁佰肆拾伍秭陆仟柒佰捌拾玖垓零壹佰贰拾叁京肆仟伍佰陆拾柒兆捌仟玖佰零壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖"));
        }
예제 #4
0
        public void CodeLowerTest()
        {
            Assert.Equal("一〇〇〇〇一", ChineseNumber.GetCodeString(10_0001.ToString(), upper: false));
            Assert.Equal("一〇〇一〇一", ChineseNumber.GetCodeString(10_0101.ToString(), upper: false));
            Assert.Equal("一〇一〇〇一", ChineseNumber.GetCodeString(10_1001.ToString(), upper: false));
            Assert.Equal("一〇一〇一〇", ChineseNumber.GetCodeString(10_1010.ToString(), upper: false));

            Assert.Equal(10_0001.ToString(), ChineseNumber.GetCodeNumber("一〇〇〇〇一"));
            Assert.Equal(10_0101.ToString(), ChineseNumber.GetCodeNumber("一〇〇一〇一"));
            Assert.Equal(10_1001.ToString(), ChineseNumber.GetCodeNumber("一〇一〇〇一"));
            Assert.Equal(10_1010.ToString(), ChineseNumber.GetCodeNumber("一〇一〇一〇"));
        }
예제 #5
0
        public void CodeUpperTest()
        {
            Assert.Equal("壹零零零零壹", ChineseNumber.GetCodeString(10_0001.ToString(), upper: true));
            Assert.Equal("壹零零壹零壹", ChineseNumber.GetCodeString(10_0101.ToString(), upper: true));
            Assert.Equal("壹零壹零零壹", ChineseNumber.GetCodeString(10_1001.ToString(), upper: true));
            Assert.Equal("壹零壹零壹零", ChineseNumber.GetCodeString(10_1010.ToString(), upper: true));

            Assert.Equal(10_0001.ToString(), ChineseNumber.GetCodeNumber("壹零零零零壹"));
            Assert.Equal(10_0101.ToString(), ChineseNumber.GetCodeNumber("壹零零壹零壹"));
            Assert.Equal(10_1001.ToString(), ChineseNumber.GetCodeNumber("壹零壹零零壹"));
            Assert.Equal(10_1010.ToString(), ChineseNumber.GetCodeNumber("壹零壹零壹零"));
        }
        public override void Interpret(MoneyContext context)
        {
            int value = context.GetNumIntList().First();

            Cn = (ChineseNumber)value;
            if ((prior != null) && (prior.IsZero()) && (prior.Cn == Cn))
            {
                return;
            }
            context.GetChineseCharList().AddFirst(EnumUtils.stringValueOf(Cn));
            return;
        }
 public static bool TryGetNumber(String s, out int value)
 {
     value = -1;
     try
     {
         ChineseNumber n = (ChineseNumber)EnumUtils.enumValueOf(s, typeof(ChineseNumber));
         value = (int)n;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
예제 #8
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
        }
예제 #9
0
        public void SimplifiedUpperTest()
        {
            var options = new ChineseNumberOptions {
                Simplified = true, Upper = true
            };

            Assert.Equal("壹", ChineseNumber.GetString(1, options));
            Assert.Equal("拾万零壹", ChineseNumber.GetString(10_0001, options));
            Assert.Equal("拾万零壹佰零壹", ChineseNumber.GetString(10_0101, options));
            Assert.Equal("拾万壹仟零壹", ChineseNumber.GetString(10_1001, options));
            Assert.Equal("拾万壹仟零壹拾", ChineseNumber.GetString(10_1010, options));

            Assert.Equal(1, ChineseNumber.GetNumber("壹"));
            Assert.Equal(10_0001, ChineseNumber.GetNumber("拾万零壹"));
            Assert.Equal(10_0101, ChineseNumber.GetNumber("拾万零壹佰零壹"));
            Assert.Equal(10_1001, ChineseNumber.GetNumber("拾万壹仟零壹"));
            Assert.Equal(10_1010, ChineseNumber.GetNumber("拾万壹仟零壹拾"));
        }
예제 #10
0
        public void SimplifiedLowerTest()
        {
            var options = new ChineseNumberOptions {
                Simplified = true, Upper = false
            };

            Assert.Equal("一", ChineseNumber.GetString(1, options));
            Assert.Equal("十万零一", ChineseNumber.GetString(10_0001, options));
            Assert.Equal("十万零一百零一", ChineseNumber.GetString(10_0101, options));
            Assert.Equal("十万一千零一", ChineseNumber.GetString(10_1001, options));
            Assert.Equal("十万一千零一十", ChineseNumber.GetString(10_1010, options));

            Assert.Equal(1, ChineseNumber.GetNumber("一"));
            Assert.Equal(10_0001, ChineseNumber.GetNumber("十万零一"));
            Assert.Equal(10_0101, ChineseNumber.GetNumber("十万零一百零一"));
            Assert.Equal(10_1001, ChineseNumber.GetNumber("十万一千零一"));
            Assert.Equal(10_1010, ChineseNumber.GetNumber("十万一千零一十"));
        }
예제 #11
0
        public void LowerTest()
        {
            var options = new ChineseNumberOptions {
                Simplified = false, Upper = false
            };

            Assert.Equal("一", ChineseNumber.GetString(1, options));
            Assert.Equal("一十万零一", ChineseNumber.GetString(10_0001, options));
            Assert.Equal("一十万零一百零一", ChineseNumber.GetString(10_0101, options));
            Assert.Equal("一十万一千零一", ChineseNumber.GetString(10_1001, options));
            Assert.Equal("一十万一千零一十", ChineseNumber.GetString(10_1010, options));

            Assert.Equal(1, ChineseNumber.GetNumber("一"));
            Assert.Equal(2, ChineseNumber.GetNumber("两"));
            Assert.Equal(22222, ChineseNumber.GetNumber("两万二千两百二十二"));
            Assert.Throws <ArgumentException>(() => ChineseNumber.GetNumber("两万二千两百二十两"));
            Assert.Equal(10_0001, ChineseNumber.GetNumber("一十万零一"));
            Assert.Equal(10_0101, ChineseNumber.GetNumber("一十万零一百零一"));
            Assert.Equal(10_1001, ChineseNumber.GetNumber("一十万一千零一"));
            Assert.Equal(10_1010, ChineseNumber.GetNumber("一十万一千零一十"));
        }
예제 #12
0
 public void AliasTest()
 {
     Assert.Equal(22222, ChineseNumber.GetNumber("两万二千两百二十二"));
     Assert.Throws <ArgumentException>(() => ChineseNumber.GetNumber("两"));
     Assert.Throws <ArgumentException>(() => ChineseNumber.GetNumber("两万二千两百二十两"));
 }
 public override void Interpret(MoneyContext context)
 {
     int value = context.GetNumIntList().First();
     Cn = (ChineseNumber)value;
     if ((prior != null) && (prior.IsZero()) && (prior.Cn == Cn))
         return;
     context.GetChineseCharList().AddFirst(EnumUtils.stringValueOf(Cn));
     return;
 }