public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType() != this.GetType())
            {
                return(false);
            }
            SeparatorCombination other = (SeparatorCombination)obj;

            return(String.Equals(GroupSeparators, other.GroupSeparators) &&
                   String.Equals(DecimalSeparators, other.DecimalSeparators));
        }
        public static Recognizer Create(System.Globalization.CultureInfo culture, int priority)
        {
            try
            {
                // TODO support non-blank languages for unit separation

                MeasureRegexRecognizer result = new MeasureRegexRecognizer(100, "DEFAULT_MEASURE_RECOGNIZER", culture);

                Core.CharacterSet first = null;
                // augmentation doesn't change FIRST()
                List <string> patterns = NumberRegexRecognizer.ComputeRXPatterns(culture, NumberSeparatorMode.CultureDefault, out first);
                AugmentPatterns(patterns, culture);

                foreach (string p in patterns)
                {
                    // use the same first for all patterns (the number regex pattern computer returns just one pattern anyway)
                    result.Add(p, first, 2);
                }

                SeparatorCombination defaultSc = new SeparatorCombination(culture, false);
                if (defaultSc.IsSwappable())
                {
                    patterns = NumberRegexRecognizer.ComputeRXPatterns(culture, NumberSeparatorMode.Swapped, out first);
                    AugmentPatterns(patterns, culture);

                    foreach (string p in patterns)
                    {
                        result.Add(p, first, 1);
                    }
                }
                if (NumberPatternComputer.DoAddENUSSeparators(culture))
                {
                    patterns = NumberRegexRecognizer.ComputeRXPatterns(culture, NumberSeparatorMode.EnUS, out first);
                    AugmentPatterns(patterns, culture);

                    foreach (string p in patterns)
                    {
                        result.Add(p, first, 0);
                    }
                }

                result.OnlyIfFollowedByNonwordCharacter
                    = true;                     // otherwise "123 ABC" will be recognized as "123 A" "BC" in Japanese
                return(result);
            }
            catch             // (System.Exception e)
            {
                return(null);
            }
        }
        /// <summary>
        /// <param name="augmentGroupSeparators">If true, blanks/nbsp's will be interchangeable</param>
        /// </summary>
        public bool AddSeparatorCombination(string groupSeparator, string decimalSeparator,
                                            bool augmentGroupSeparators)
        {
            SeparatorCombination sc = new SeparatorCombination(groupSeparator, decimalSeparator, augmentGroupSeparators);

            if (!SeparatorCombinations.Contains(sc))
            {
                SeparatorCombinations.Add(sc);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #4
0
        public static Recognizer Create(System.Globalization.CultureInfo culture, int priority)
        {
            try
            {
                NumberRegexRecognizer recog = new NumberRegexRecognizer(100, "DEFAULT_NUMBER_RECOGNIZER", culture.NumberFormat);

                CharacterSet first = null;
                foreach (string p in ComputeRXPatterns(culture, NumberSeparatorMode.CultureDefault, out first))
                {
                    recog.Add(p, first, 2);
                }

                SeparatorCombination defaultSc = new SeparatorCombination(culture, false);
                if (defaultSc.IsSwappable())
                {
                    foreach (string p in ComputeRXPatterns(culture, NumberSeparatorMode.Swapped, out first))
                    {
                        recog.Add(p, first, 1);
                    }
                }
                if (NumberPatternComputer.DoAddENUSSeparators(culture))
                {
                    foreach (string p in ComputeRXPatterns(culture, NumberSeparatorMode.EnUS, out first))
                    {
                        recog.Add(p, first, 1);
                    }
                }

                recog.OnlyIfFollowedByNonwordCharacter
                    = CultureInfoExtensions.UseBlankAsWordSeparator(culture);
                recog.AdditionalTerminators = new CharacterSet();
                recog.AdditionalTerminators.Add('-');                 // TODO other math symbols?
                recog.OverrideFallbackRecognizer = true;

                return(recog);
            }
            catch             // (System.Exception e)
            {
                return(null);
            }
        }
        /// <summary>
        /// <param name="augmentWhitespaceGroupSeparators">If true, blanks/nbsp's will be interchangeable</param>
        /// </summary>
        public static NumberFormatData GetNumberFormatData(System.Globalization.CultureInfo culture,
                                                           bool addSeparatorVariants, bool augmentWhitespaceGroupSeparators)
        {
            if (culture == null)
            {
                throw new ArgumentNullException();
            }
            if (culture.IsNeutralCulture)
            {
                throw new ArgumentException("Cannot compute number format information for neutral cultures");
            }

            string nativeDigits = String.Join("", culture.NumberFormat.NativeDigits);

            if (nativeDigits.Length != 10)
            {
                return(null);
            }

            NumberFormatData result = new NumberFormatData();

            result.Digits.Add(nativeDigits);
            if (!String.Equals(nativeDigits, DefaultDigits))
            {
                result.Digits.Add(DefaultDigits);
            }

            if (DoAddFullwidthVariantDigits(culture) &&
                !result.Digits.Contains(FullWidthDigits))
            {
                result.Digits.Add(FullWidthDigits);
            }

            result.NegativeSigns         = culture.NumberFormat.NegativeSign;
            result.PositiveSigns         = culture.NumberFormat.PositiveSign;
            result.NumberGroupSizes      = culture.NumberFormat.NumberGroupSizes;
            result.NumberNegativePattern = culture.NumberFormat.NumberNegativePattern;

            result.AddSeparatorCombination(culture.NumberFormat.NumberGroupSeparator,
                                           culture.NumberFormat.NumberDecimalSeparator, augmentWhitespaceGroupSeparators);
            result.AddSeparatorCombination(culture.NumberFormat.CurrencyGroupSeparator,
                                           culture.NumberFormat.CurrencyDecimalSeparator, augmentWhitespaceGroupSeparators);

            if (addSeparatorVariants)
            {
                switch (culture.TwoLetterISOLanguageName.ToLowerInvariant())
                {
                case "fr":
                    result.AddSeparatorCombination(",", ".", false);
                    break;

                case "pl":
                    result.AddSeparatorCombination(".", ",", false);
                    break;

                default:
                    break;
                }

                for (int p = result.SeparatorCombinations.Count - 1; p >= 0; --p)
                {
                    SeparatorCombination sc = result.SeparatorCombinations[p];
                    if (sc.IsSwappable())
                    {
                        // note: swapping separators here
                        result.AddSeparatorCombination(sc.DecimalSeparators, sc.GroupSeparators, false);
                    }
                }
            }

            return(result);
        }