示例#1
0
        /// <summary>
        /// Parses the input definition into a collection of <see cref="Dto.Sense"/>s.
        /// </summary>
        public void Parse(Definition definition)
        {
            if (definition == null)
            {
                return;
            }

            foreach (var sourceSseqs in _def.SenseSequences)
            {
                var senseSequence = new SenseSequence();

                foreach (var sourceSseq in sourceSseqs)
                {
                    // sourceSseq comes in pairs: a string that defines the object ("sense", "sen", "pseq", etc.) , followed by the actual object
                    if (sourceSseq.Length == 2)
                    {
                        if (sourceSseq[0].Name == Response.SseqEnum.Bs)
                        {
                            var sourceSense = sourceSseq[1].Sense;

                            // a binding substitute (bs) should always contain a nested sense object
                            if (sourceSense?.SubSense != null)
                            {
                                var sense = ParseGeneralSenseProperties <Sense>(sourceSense.SubSense);
                                sense.SenseNumber = sourceSense.SubSense.SenseNumber;
                                ParseSpecificSenseProperties(sourceSense.SubSense, sense);
                                sense.IsBindingSubstitute = true;

                                senseSequence.Senses.Add(sense);
                            }
                        }

                        if (sourceSseq[0].Name == Response.SseqEnum.Sense)
                        {
                            var sourceSense = sourceSseq[1].Sense;
                            if (sourceSense != null)
                            {
                                var sense = ParseGeneralSenseProperties <Sense>(sourceSense);
                                sense.SenseNumber = sourceSense.SenseNumber;
                                ParseSpecificSenseProperties(sourceSense, sense);

                                if (sourceSense.CrossReferences.Any())
                                {
                                    sense.CrossReferences = CrossReferenceHelper.Parse(sourceSense.CrossReferences).ToList();
                                }

                                senseSequence.Senses.Add(sense);
                            }
                        }
                        else if (sourceSseq[0].Name == Response.SseqEnum.Sen)
                        {
                            var sourceSense = sourceSseq[1].Sense;
                            if (sourceSense != null)
                            {
                                var sense = ParseGeneralSenseProperties <Sense>(sourceSense);
                                sense.SenseNumber      = sourceSense.SenseNumber;
                                sense.IsTruncatedSense = true;

                                if (sourceSense.CrossReferences.Any())
                                {
                                    sense.CrossReferences = CrossReferenceHelper.Parse(sourceSense.CrossReferences).ToList();
                                }

                                senseSequence.Senses.Add(sense);
                            }
                        }
                        else if (sourceSseq[0].Name == Response.SseqEnum.Pseq && sourceSseq[1].SenseSequences != null)
                        {
                            var pseq = new SenseSequenceSense()
                            {
                                IsParenthesizedSenseSequence = true,
                                Senses = new List <SenseSequenceSense>()
                            };

                            foreach (var sourceSequence in sourceSseq[1].SenseSequences)
                            {
                                if (sourceSequence[0].Name == Response.SseqEnum.Bs)
                                {
                                    var sourceSense = sourceSequence[1].Sense;

                                    // a binding substitute (bs) should always contain a nested sense object
                                    if (sourceSense?.SubSense != null)
                                    {
                                        var sense = ParseGeneralSenseProperties <Sense>(sourceSense.SubSense);
                                        sense.SenseNumber = sourceSense.SubSense.SenseNumber;
                                        ParseSpecificSenseProperties(sourceSense.SubSense, sense);
                                        sense.IsBindingSubstitute = true;

                                        pseq.Senses.Add(sense);
                                    }
                                }

                                if (sourceSequence[0].Name == Response.SseqEnum.Sense)
                                {
                                    var sourceSense = sourceSequence[1].Sense;
                                    if (sourceSense != null)
                                    {
                                        var sense = ParseGeneralSenseProperties <Sense>(sourceSense);
                                        sense.SenseNumber = sourceSense.SenseNumber;
                                        ParseSpecificSenseProperties(sourceSense, sense);

                                        pseq.Senses.Add(sense);
                                    }
                                }
                            }

                            senseSequence.Senses.Add(pseq);
                        }
                    }
                    // else?
                    //sourceSseq.Length != 2, should not occur
                }

                definition.SenseSequence.Add(senseSequence);
            }
        }
示例#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);
        }