Ejemplo n.º 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;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new sense instance from a source sense with properties that occur on all sense types.
        /// </summary>
        private T ParseGeneralSenseProperties <T>(Response.SenseBase sourceSense)
            where T : SenseBase, new()
        {
            var sense = new T();

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

            if (sourceSense.SubjectStatusLabels.Any())
            {
                sense.SubjectStatusLabels = new List <Label>();
                foreach (var subjectStatusLabel in sourceSense.SubjectStatusLabels)
                {
                    sense.SubjectStatusLabels.Add(subjectStatusLabel);
                }
            }

            if (!string.IsNullOrEmpty(sourceSense.SenseSpecificGrammaticalLabel))
            {
                sense.SenseSpecificGrammaticalLabel = sourceSense.SenseSpecificGrammaticalLabel;
            }

            if (sourceSense.Variants.Any())
            {
                sense.Variants = VariantHelper.Parse(sourceSense.Variants, _language, _parseOptions.AudioFormat).ToList();
            }

            if (sourceSense.Pronunciations.Any())
            {
                sense.Pronunciations = new List <Pronunciation>();
                foreach (var pronunciation in sourceSense.Pronunciations)
                {
                    sense.Pronunciations.Add(PronunciationHelper.Parse(pronunciation, _language, _parseOptions.AudioFormat));
                }
            }

            if (sourceSense.Inflections.Any())
            {
                sense.Inflections = InflectionHelper.Parse(sourceSense.Inflections, _language, _parseOptions.AudioFormat).ToList();
            }

            if (sourceSense.Etymologies.Any())
            {
                sense.Etymology = sourceSense.Etymologies.ParseEtymology();
            }

            return(sense);
        }
Ejemplo n.º 3
0
        private static IEnumerable <UndefinedRunOn> ParseUros(Entry result, Response.UndefinedRunOn [] uros, ParseOptions options)
        {
            var searchResults = new List <UndefinedRunOn>();

            foreach (var uro in uros)
            {
                var searchResult = new UndefinedRunOn
                {
                    EntryWord    = uro.EntryWord,
                    PartOfSpeech = uro.FunctionalLabel
                };

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

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

                if (uro.Pronunciations.Any())
                {
                    searchResult.Pronunciations = new List <Pronunciation>();
                    foreach (var pronunciation in uro.Pronunciations)
                    {
                        var pron = PronunciationHelper.Parse(pronunciation, result.Metadata.Language,
                                                             options.AudioFormat);
                        searchResult.Pronunciations.Add(pron);
                    }
                }

                if (uro.AlternateEntry != null)
                {
                    searchResult.AlternateEntry = new AlternateUndefinedEntryWord
                    {
                        Text        = uro.AlternateEntry.Text,
                        TextCutback = uro.AlternateEntry.TextCutback
                    };
                }

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

                if (uro.Inflections.Any())
                {
                    searchResult.Inflections = InflectionHelper.Parse(uro.Inflections, result.Metadata.Language, options.AudioFormat).ToList();
                }

                searchResults.Add(searchResult);
            }

            return(searchResults);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parses the defining text contents from the source sense and stores it on the target sense.
        /// </summary>
        public static void ParseDefiningText(this Response.SenseBase sourceSense, SenseBase targetSense, Language language, AudioFormat audioFormat)
        {
            foreach (var definingTextObjects in sourceSense.DefiningTexts)
            {
                if (definingTextObjects.Length != 2)
                {
                    continue;
                }

                var definitionType = definingTextObjects[0].TypeOrText;

                if (definitionType == DefiningTextTypes.Text)
                {
                    string definitionText = definingTextObjects[1].TypeOrText;

                    if (targetSense is Sense sense)
                    {
                        sense.Synonyms = SynonymsParser.ExtractSynonyms(definitionText).ToList();

                        var definingText = new DefiningText(definitionText);
                        targetSense.DefiningTexts.Add(definingText);
                        if (sense.Synonyms.Any())
                        {
                            // not very robust, but until now I only found sx links at the beginning of a string in the spanish-english dictionary
                            // in that case the synonyms should be removed from the text, in other cases we keep them between square brackets
                            if (definingText.Text.RawText.StartsWith("{sx"))
                            {
                                foreach (var synonym in sense.Synonyms)
                                {
                                    definitionText = definitionText.Replace(synonym, "");
                                }
                            }
                            else
                            {
                                foreach (var synonym in sense.Synonyms)
                                {
                                    definitionText = definitionText.Replace(synonym, $"[{synonym}]");
                                }
                            }
                        }
                    }
                    else
                    {
                        targetSense.DefiningTexts.Add(new DefiningText(definitionText));
                    }
                }

                else if (definitionType == DefiningTextTypes.VerbalIllustration)
                {
                    foreach (var dto in definingTextObjects[1].DefiningTextObjects)
                    {
                        if (dto.DefiningText != null)
                        {
                            var vis = VisHelper.Parse(dto.DefiningText);
                            targetSense.DefiningTexts.Add(vis);
                        }
                    }
                }

                else if (definitionType == DefiningTextTypes.GenderLabel)
                {
                    targetSense.DefiningTexts.Add(new GenderLabel(definingTextObjects[1].TypeOrText));
                }

                if (definitionType == DefiningTextTypes.BiographicalNameWrap)
                {
                    var dt = definingTextObjects[1].DefiningText;

                    var biographicalNameWrap = new BiographicalNameWrap
                    {
                        AlternateName = dt.Altname,
                        FirstName     = dt.Pname,
                        Surname       = dt.Surname
                    };

                    if (dt.Pronunciations.Any())
                    {
                        biographicalNameWrap.Pronunciations = new List <Pronunciation>();
                        foreach (var pronunciation in dt.Pronunciations)
                        {
                            biographicalNameWrap.Pronunciations.Add(PronunciationHelper.Parse(pronunciation, language, audioFormat));
                        }
                    }

                    targetSense.DefiningTexts.Add(biographicalNameWrap);
                }

                else if (definitionType == DefiningTextTypes.CalledAlsoNote)
                {
                    var ca             = definingTextObjects[1].DefiningText;
                    var calledAlsoNote = new CalledAlsoNote
                    {
                        Intro = ca.Intro
                    };

                    foreach (var cat in ca.Cats)
                    {
                        var calledAlsoTarget = new CalledAlsoTarget
                        {
                            ParenthesizedNumber             = cat.ParenthesizedNumber,
                            Reference                       = cat.Reference,
                            TargetText                      = cat.Text,
                            ParenthesizedSubjectStatusLabel = cat.ParenthesizedSubjectStatusLabel
                        };

                        if (cat.Pronunciations.Any())
                        {
                            calledAlsoTarget.Pronunciations = new List <Pronunciation>();
                            foreach (var pronunciation in cat.Pronunciations)
                            {
                                calledAlsoTarget.Pronunciations.Add(PronunciationHelper.Parse(pronunciation, language, audioFormat));
                            }
                        }

                        calledAlsoNote.Targets.Add(calledAlsoTarget);
                    }

                    targetSense.DefiningTexts.Add(calledAlsoNote);
                }

                else if (definitionType == DefiningTextTypes.RunIn)
                {
                    var arr = definingTextObjects[1].DefiningTextObjects;
                    foreach (var definingTextObject in arr)
                    {
                        var typeOrLabel = definingTextObject.DefiningTextComplexTypeWrappers[0].TypeLabelOrText;
                        if (typeOrLabel == "riw")
                        {
                            var ri = definingTextObject.DefiningTextComplexTypeWrappers[1].RunInWrap;
                            if (ri != null)
                            {
                                var runInWord = new RunInWord
                                {
                                    Text       = ri.Text,
                                    RunInEntry = ri.RunInEntry
                                };

                                if (ri.Vrs.Any())
                                {
                                    runInWord.Variants = VariantHelper.Parse(ri.Vrs, language, audioFormat).ToList();
                                }

                                if (ri.Pronunciations.Any())
                                {
                                    runInWord.Pronunciations = new List <Pronunciation>();
                                    foreach (var pronunciation in ri.Pronunciations)
                                    {
                                        runInWord.Pronunciations.Add(PronunciationHelper.Parse(pronunciation, language, audioFormat));
                                    }
                                }

                                targetSense.DefiningTexts.Add(runInWord);
                            }
                        }

                        if (typeOrLabel == DefiningTextTypes.Text)
                        {
                            targetSense.DefiningTexts.Add(new DefiningText(definingTextObject.DefiningTextComplexTypeWrappers[1].TypeLabelOrText));
                        }
                    }
                }

                else if (definitionType == DefiningTextTypes.SupplementalNote)
                {
                    var arr = definingTextObjects[1].DefiningTextObjects[0].DefiningTextComplexTypeWrappers;
                    var supplementalInformationNote = new SupplementalInformationNote
                    {
                        Text = arr[1].TypeLabelOrText
                    };

                    targetSense.DefiningTexts.Add(supplementalInformationNote);

                    // todo nested ri, vis, requires sample json
                }

                else if (definitionType == DefiningTextTypes.UsageNote)
                {
                    var arr = definingTextObjects[1].DefiningTextObjects[0].DefiningTextComplexTypeWrappers;
                    if (arr == null)
                    {
                        continue;
                    }

                    var un = new UsageNote();

                    foreach (var dtWrapper in arr.Where(x => x.DefiningTextComplexTypes?.Any() == true))
                    {
                        var unType = dtWrapper.DefiningTextComplexTypes[0].TypeOrLabel;
                        if (unType == DefiningTextTypes.Text)
                        {
                            un.Text = dtWrapper.DefiningTextComplexTypes[1].TypeOrLabel;
                        }
                        else if (unType == DefiningTextTypes.VerbalIllustration)
                        {
                            if (un.VerbalIllustrations == null)
                            {
                                un.VerbalIllustrations = new List <VerbalIllustration>();
                            }

                            foreach (var definingText in dtWrapper.DefiningTextComplexTypes[1].DefiningTexts)
                            {
                                var vis = VisHelper.Parse(definingText);
                                un.VerbalIllustrations.Add(vis);
                            }
                        }

                        // todo "ri", requires sample json
                    }

                    targetSense.DefiningTexts.Add(un);
                }
                else if (definitionType == DefiningTextTypes.GenderForms)
                {
                    var dt = definingTextObjects[1].DefiningText;
                    targetSense.DefiningTexts.Add(new GenderForms
                    {
                        GenderWordCutback    = dt.GenderWordCutback,
                        GenderWordSpelledOut = dt.GenderWordSpelledOut
                    });
                }
            }
        }