Example #1
0
        private static IEnumerable <Synonym> ParseSynonyms(Response.Synonym[] sources)
        {
            foreach (var source in sources)
            {
                var synonym = new Synonym
                {
                    ParagraphLabel = source.Pl
                };

                if (source.Sarefs?.Any() == true)
                {
                    synonym.SeeInAdditionReference = new List <string>(source.Sarefs);
                }

                foreach (var dt in source.Pt)
                {
                    if (dt[0].TypeLabelOrText == DefiningTextTypes.Text)
                    {
                        synonym.DefiningTexts.Add(new DefiningText(dt[1].TypeLabelOrText));
                    }
                    else if (dt[0].TypeLabelOrText == DefiningTextTypes.VerbalIllustration)
                    {
                        foreach (var dtc in dt[1].DefiningTextComplexTypes)
                        {
                            synonym.DefiningTexts.Add(VisHelper.Parse(dtc.DefiningText));
                        }
                    }
                }

                yield return(synonym);
            }
        }
Example #2
0
        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);
        }
Example #3
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
                    });
                }
            }
        }