public static IEnumerable <Inflection> Parse(Response.Inflection[] sources, Language language, AudioFormat audioFormat)
        {
            foreach (var source in sources)
            {
                var inflection = new Inflection
                {
                    Label   = source.Label,
                    Cutback = source.Cutback,
                    Value   = source.Value,
                    SenseSpecificInflectionPluralLabel = source.SenseSpecificInflectionPluralLabel
                };

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

                if (source.Alternate != null)
                {
                    inflection.Alternate = new AlternateInflection
                    {
                        Cutback    = source.Alternate.Cutback,
                        Inflection = source.Alternate.Inflection
                    };
                }

                yield return(inflection);
            }
        }
        /// <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);
        }
        private static void ParseBasicStuff(ParseOptions options, Response.DictionaryEntry result, Entry searchResult)
        {
            if (result.Artwork != null)
            {
                searchResult.Artwork = new Artwork
                {
                    Caption       = result.Artwork.Caption,
                    HtmlLocation  = ArtworkLinkCreator.CreatePageLink(result.Artwork),
                    ImageLocation = ArtworkLinkCreator.CreateDirectLink(result.Artwork)
                };
            }

            if (!string.IsNullOrEmpty(result.Date))
            {
                searchResult.Date = result.Date;
            }

            if (result.HeadwordInformation != null)
            {
                foreach (var pronunciation in result.HeadwordInformation.Pronunciations)
                {
                    var pron = PronunciationHelper.Parse(pronunciation, searchResult.Metadata.Language, options.AudioFormat);
                    searchResult.Headword.Pronunciations.Add(pron);
                }

                searchResult.Headword.Text = result.HeadwordInformation.Headword;
                searchResult.Headword.ParenthesizedSubjectStatusLabel = result.HeadwordInformation.ParenthesizedSubjectStatusLabel;
            }

            if (result.AlternateHeadwords.Any())
            {
                searchResult.AlternateHeadwords = new List <AlternateHeadwordInformation>();
                foreach (var alternateHeadword in result.AlternateHeadwords)
                {
                    var alternateHeadwordInformation = new AlternateHeadwordInformation
                    {
                        HeadwordCutback = alternateHeadword.HeadwordCutback,
                        ParenthesizedSubjectStatusLabel = alternateHeadword.ParenthesizedSubjectStatusLabel,
                        Text = alternateHeadword.Headword
                    };

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

                    searchResult.AlternateHeadwords.Add(alternateHeadwordInformation);
                }
            }
        }
Exemple #4
0
        public static IEnumerable <Variant> Parse(Response.Variant[] sources, Language language, AudioFormat audioFormat)
        {
            foreach (var source in sources)
            {
                var variant = new Variant
                {
                    Cutback = source.Cutback,
                    Label   = source.VariantLabel,
                    SenseSpecificInflectionPluralLabel = source.SenseSpecificInflectionPluralLabel,
                    Text = source.Text
                };

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

                yield return(variant);
            }
        }
        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);
        }
        private static Entry CreateSearchResult(Response.DictionaryEntry result, ParseOptions options)
        {
            var searchResult = new Entry
            {
                Metadata     = result.ParseMetadata(),
                PartOfSpeech = result.FunctionalLabel ?? string.Empty,
                ShortDefs    = result.Shortdefs,
                Homograph    = result.Homograph.GetValueOrDefault()
                               // TODO
                               //Synonyms = result.Metadata.Synonyms.SelectMany(s => s).ToList(),
                               //Antonyms = result.Metadata.Antonyms.SelectMany(s => s).ToList()
            };

            if (!string.IsNullOrEmpty(result.FunctionalLabel))
            {
                searchResult.PartOfSpeech = result.FunctionalLabel;
            }

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

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

            ParseBasicStuff(options, result, searchResult);

            searchResult.Conjugations = ParseConjugations(result.Supplemental);
            if (result.CrossReferences.Any())
            {
                searchResult.CrossReferences = CrossReferenceHelper.Parse(result.CrossReferences).ToList();
            }

            if (result.CognateCrossReferences.Any())
            {
                searchResult.CognateCrossReferences = CognateCrossReferenceHelper.Parse(result.CognateCrossReferences).ToList();
            }

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

            if (result.Synonyms.Any())
            {
                searchResult.Synonyms = ParseSynonyms(result.Synonyms).ToList();
            }

            if (result.DirectionalCrossReferences.Any())
            {
                searchResult.DirectionalCrossReferences = new List <FormattedText>();
                foreach (var crossReference in result.DirectionalCrossReferences)
                {
                    searchResult.DirectionalCrossReferences.Add(new FormattedText(crossReference));
                }
            }

            if (result.Usages.Any())
            {
                searchResult.Usages = new List <Usage>();
                foreach (var srcUsage in result.Usages)
                {
                    var usage = new Usage
                    {
                        ParagraphLabel = srcUsage.ParagraphLabel
                    };

                    foreach (var complexTypeWrapper in srcUsage.ParagraphText)
                    {
                        var type = complexTypeWrapper[0].TypeLabelOrText;
                        if (type == DefiningTextTypes.Text)
                        {
                            usage.ParagraphTexts.Add(new DefiningText(complexTypeWrapper[1].TypeLabelOrText));
                        }
                        else if (type == DefiningTextTypes.VerbalIllustration)
                        {
                            foreach (var dt in complexTypeWrapper[1].DefiningTextComplexTypes)
                            {
                                usage.ParagraphTexts.Add(VisHelper.Parse(dt.DefiningText));
                            }
                        }
                        else if (type == DefiningTextTypes.InAdditionReference)
                        {
                            // TODO, requires sample json
                        }
                    }

                    searchResult.Usages.Add(usage);
                }
            }

            if (result.Table != null)
            {
                searchResult.Table = new Table
                {
                    Displayname = result.Table.Displayname,
                    TableId     = result.Table.Tableid
                };
            }

            if (result.Bios.Any())
            {
                searchResult.BiographicalNote = new BiographicalNote();
                foreach (var bioElement in result.Bios)
                {
                    foreach (var element in bioElement)
                    {
                        var typeOrText = element[0].TypeOrText;
                        if (typeOrText == "bionw")
                        {
                            var note    = element[1].BiographicalNote;
                            var content = new BiographicalNameWrap()
                            {
                                FirstName     = note.Biopname,
                                AlternateName = note.Bioname,
                                Surname       = note.Biosname
                            };

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

                            searchResult.BiographicalNote.Contents.Add(content);
                        }

                        else if (typeOrText == "biodate" || typeOrText == "text" || typeOrText == "biotx")
                        {
                            searchResult.BiographicalNote.Contents.Add(new DefiningText(element[1].TypeOrText));
                        }
                    }
                }
            }

            return(searchResult);
        }
Exemple #7
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
                    });
                }
            }
        }