Example #1
0
        private static void ParseDros(Entry result, Response.DefinedRunOn[] dros, ParseOptions parseOptions)
        {
            var searchResults = new List <DefinedRunOn>();

            if (dros == null)
            {
                return;
            }

            foreach (var dro in dros)
            {
                var searchResult = new DefinedRunOn
                {
                    Phrase       = dro.Phrase,
                    PartOfSpeech = dro.FunctionalLabel,
                    ParenthesizedSubjectStatusLabel = dro.ParenthesizedSubjectStatusLabel,
                };

                if (dro.GeneralLabels.Any())
                {
                    searchResult.GeneralLabels = new List <Label>();
                    foreach (var generalLabel in dro.GeneralLabels)
                    {
                        searchResult.GeneralLabels.Add(generalLabel);
                    }
                }

                if (dro.Sls.Any())
                {
                    searchResult.SubjectStatusLabels = new List <Label>();
                    foreach (var sls in searchResult.SubjectStatusLabels)
                    {
                        searchResult.SubjectStatusLabels.Add(sls);
                    }
                }

                foreach (var droDef in dro.Definitions)
                {
                    var senseParser = new SenseParser(droDef, result.Metadata.Language, parseOptions);
                    var def         = new Definition();
                    senseParser.Parse(def);

                    searchResult.Definitions.Add(def);
                }

                if (dro.Et.Any())
                {
                    searchResult.Etymology = dro.Et.ParseEtymology();
                }

                if (dro.Vrs.Any())
                {
                    searchResult.Variants = VariantHelper.Parse(dro.Vrs, result.Metadata.Language, parseOptions.AudioFormat).ToList();
                }

                searchResults.Add(searchResult);
            }

            result.DefinedRunOns = searchResults;
        }
Example #2
0
        /// <inheritdoc />
        public ResultModel Parse(string searchTerm, IEnumerable <Response.DictionaryEntry> results, ParseOptions options)
        {
            if (searchTerm == null)
            {
                throw new ArgumentNullException(nameof(searchTerm));
            }

            if (options == null)
            {
                options = ParseOptions.Default;
            }

            var resultModel = new ResultModel
            {
                SearchText  = searchTerm.ToLowerInvariant(),
                RawResponse = JsonConvert.SerializeObject(results, SerializerSettings)
            };

            foreach (var result in results)
            {
                var searchResult = CreateSearchResult(result, options);

                // parse definitions
                foreach (var def in result.Definitions)
                {
                    var definition = new Definition
                    {
                        VerbDivider = def.VerbDivider
                    };

                    var senseParser = new SenseParser(def, searchResult.Metadata.Language, options);
                    senseParser.Parse(definition);

                    if (def.Sls.Any())
                    {
                        definition.SubjectStatusLabels = new List <Label>();
                        foreach (var label in def.Sls)
                        {
                            definition.SubjectStatusLabels.Add(label);
                        }
                    }

                    searchResult.Definitions.Add(definition);
                }

                // we're done with this search result, add to collection
                resultModel.Entries.Add(searchResult);

                if (result.Variants.Any())
                {
                    searchResult.Variants = VariantHelper.Parse(result.Variants, searchResult.Metadata.Language, options.AudioFormat).ToList();
                }

                // parse and add any additional results (they appear in the 'DefinedRunOns' ('dro') property)
                if (result.DefinedRunOns.Any())
                {
                    ParseDros(searchResult, result.DefinedRunOns, options);
                }

                // parse and add any 'undefined run-ons'
                if (result.UndefinedRunOns.Any())
                {
                    searchResult.UndefinedRunOns = ParseUros(searchResult, result.UndefinedRunOns, options).ToList();
                }

                if (result.Quotes.Any())
                {
                    // parse and add any quotes
                    searchResult.Quotes = new List <Quote>();

                    foreach (var sourceQuote in result.Quotes)
                    {
                        var quote = QuoteHelper.Parse(sourceQuote);
                        searchResult.Quotes.Add(quote);
                    }
                }

                if (result.Et.Any())
                {
                    searchResult.Etymology = result.Et.ParseEtymology();
                }
            }

            return(resultModel);
        }