private Recognizer CreateCurrencyRecognizer(Core.Resources.IResourceDataAccessor accessor,
                                                    System.Globalization.CultureInfo actualCulture)
        {
            Wordlist wl = new Wordlist();

            using (System.IO.Stream data = accessor.ReadResourceData(actualCulture, Core.Resources.LanguageResourceType.CurrencySymbols, true))
            {
                if (data != null)
                {
                    wl.Load(data);
                }
            }

            if (wl.Count == 0)
            {
                return(null);
            }

            return(CurrencyRegexRecognizer.Create(actualCulture, wl, 100));
        }
Exemplo n.º 2
0
        public static Recognizer Create(System.Globalization.CultureInfo culture,
                                        Core.Wordlist currencySymbols,
                                        int priority)
        {
            try
            {
                // TODO support non-blank languages for unit separation

                Core.CharacterSet curFirst     = null;
                string            currenciesRx = currencySymbols.GetRegularExpression(out curFirst);

                int currencyPattern = culture.NumberFormat.CurrencyPositivePattern;

                bool currencyPrecedesNumber = (currencyPattern % 2) == 0;
                bool currencyIsSeparated    = (currencyPattern >= 2);

                CurrencyRegexRecognizer result = new CurrencyRegexRecognizer(100, "DEFAULT_CURRENCY_RECOGNIZER", culture);

                Core.CharacterSet first = null;
                // augmentation doesn't change FIRST()
                // TODO use currency pattern instead of number pattern?
                List <string> patterns = NumberRegexRecognizer.ComputeRXPatterns(culture, NumberSeparatorMode.CultureDefault, out first);

                if (currencyPrecedesNumber)
                {
                    first.Add(curFirst);
                }

                AugmentPatterns(patterns, currenciesRx, 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);
                }

                /*
                 * Be strict for currencies (only flexible for measurements and numbers)
                 *
                 * if (NumberRegexRecognizer.CanSwapSeparators(culture))
                 * {
                 *      patterns = NumberRegexRecognizer.ComputeRXPatterns(culture, NumberSeparatorMode.Swapped, out first);
                 *      AugmentPatterns(patterns, currenciesRx, culture);
                 *
                 *      foreach (string p in patterns)
                 *      {
                 *              result.Add(p, first, 1);
                 *      }
                 * }
                 * if (NumberRegexRecognizer.AddENUSSeparators(culture))
                 * {
                 *      patterns = NumberRegexRecognizer.ComputeRXPatterns(culture, NumberSeparatorMode.EnUS, out first);
                 *      AugmentPatterns(patterns, currenciesRx, culture);
                 *
                 *      foreach (string p in patterns)
                 *      {
                 *              result.Add(p, first, 0);
                 *      }
                 * }
                 */

                result.OnlyIfFollowedByNonwordCharacter
                    = Core.CultureInfoExtensions.UseBlankAsWordSeparator(culture);

                return(result);
            }
            catch             // (System.Exception e)
            {
                return(null);
            }
        }