Example #1
0
        private ModelResult BuildModelResult(ParseResult pn)
        {
            try
            {
                var end        = pn.Start.Value + pn.Length.Value - 1;
                var resolution = new SortedDictionary <string, object>();
                if (pn.Value != null)
                {
                    resolution.Add(ResolutionKey.Value, pn.ResolutionStr);
                }

                var extractorSupportsSubtype = ExtractorsSupportingSubtype.Exists(e => Extractor.GetType().ToString().Contains(e));

                // Check if current extractor supports the Subtype field in the resolution
                // As some languages like German, we miss handling some subtypes between "decimal" and "integer"
                if (!string.IsNullOrEmpty(pn.Type) &&
                    Constants.ValidSubTypes.Contains(pn.Type) && extractorSupportsSubtype)
                {
                    resolution.Add(ResolutionKey.SubType, pn.Type);
                }

                string specificNumberType;

                // For ordinal and ordinal.relative - "ordinal.relative" only available in English for now
                if (ModelTypeName.Equals(Constants.MODEL_ORDINAL, StringComparison.Ordinal))
                {
                    specificNumberType = pn.Type;
                    resolution.Add(ResolutionKey.Offset, pn.Metadata.Offset);
                    resolution.Add(ResolutionKey.RelativeTo, pn.Metadata.RelativeTo);
                }
                else
                {
                    specificNumberType = ModelTypeName;
                }

                return(new ModelResult
                {
                    Start = pn.Start.Value,
                    End = end,
                    Resolution = resolution,
                    Text = pn.Text,
                    TypeName = specificNumberType,
                });
            }
            catch (Exception)
            {
                // Nothing to do. Exceptions in result process should not affect other extracted entities.
                // No result.
            }

            return(null); // Only in failure cases. These will be filtered out before final output.
        }
        public List <ModelResult> Parse(string query)
        {
            var parsedNumbers = new List <ParseResult>();

            // Preprocess the query
            query = QueryProcessor.Preprocess(query, caseSensitive: true);

            try
            {
                var extractResults = Extractor.Extract(query);

                foreach (var result in extractResults)
                {
                    var parseResult = Parser.Parse(result);
                    if (parseResult.Data is List <ParseResult> parseResults)
                    {
                        parsedNumbers.AddRange(parseResults);
                    }
                    else
                    {
                        parsedNumbers.Add(parseResult);
                    }
                }
            }
            catch (Exception)
            {
                // Nothing to do. Exceptions in parse should not break users of recognizers.
                // No result.
            }

            return(parsedNumbers.Select(o =>
            {
                var end = o.Start.Value + o.Length.Value - 1;
                var resolution = new SortedDictionary <string, object>();
                if (o.Value != null)
                {
                    resolution.Add(ResolutionKey.Value, o.ResolutionStr);
                }

                var extractorType = Extractor.GetType().ToString();

                // Only support "subtype" for English for now
                // As some languages like German, we miss handling some subtypes between "decimal" and "integer"
                if (!string.IsNullOrEmpty(o.Type) &&
                    Constants.ValidSubTypes.Contains(o.Type) && extractorType.Contains(Constants.ENGLISH))
                {
                    resolution.Add(ResolutionKey.SubType, o.Type);
                }

                var type = string.Empty;

                // for ordinal and ordinal.relative
                // Only support "subtype" for English for now
                if (ModelTypeName.Equals(Constants.MODEL_ORDINAL) && extractorType.Contains(Constants.ENGLISH))
                {
                    if (o.Metadata != null && o.Metadata.IsOrdinalRelative)
                    {
                        type = $"{ModelTypeName}.{Constants.RELATIVE}";
                    }
                    else
                    {
                        type = ModelTypeName;
                    }

                    resolution.Add(ResolutionKey.Offset, o.Metadata.Offset);
                    resolution.Add(ResolutionKey.RelativeTo, o.Metadata.RelativeTo);
                }
                else
                {
                    type = ModelTypeName;
                }

                return new ModelResult
                {
                    Start = o.Start.Value,
                    End = end,
                    Resolution = resolution,
                    Text = o.Text,
                    TypeName = type,
                };
            }).ToList());
        }