Ejemplo n.º 1
0
        public override bool Parse(SyntacticState state)
        {
            if (state.IsEndOfData)
            {
                return(false);
            }

            // ensure that identifier is captured
            LexicalEntry entry = state.GetInner(state.InnerPosition);

            if (entry.Key != Identifier.S.Key)
            {
                return(false);
            }

            // check that identifier consists only of specified letters
            string       checkOuter = state.GetOuter(entry);
            LexicalState checkState = new LexicalState(checkOuter);

            if (!m_check.ParseFull(checkState))
            {
                return(false);
            }

            state.AddAbsolute(
                Key,
                state.InnerPosition + 1,
                entry.EndPosition);

            return(true);
        }
        public override bool Parse(SyntacticState state)
        {
            // this terminal consists of two lexical punctuators
            // and should be parsed in a special way
            if (state.InnerPosition + 1 >= state.InnerLength)
            {
                return(false);
            }

            LexicalEntry e1 = state.GetInner(state.InnerPosition);

            if (state.GetOuter(e1) != ">")
            {
                return(false);
            }

            LexicalEntry e2 = state.GetInner(state.InnerPosition + 1);

            if (state.GetOuter(e2) != ">=")
            {
                return(false);
            }

            if (e2.StartPosition != e1.StartPosition + 1)
            {
                return(false);
            }

            state.AddAbsolute(
                Key,
                state.InnerPosition + 2,
                state.OuterPosition + 3);

            return(true);
        }
Ejemplo n.º 3
0
        private static void ParseLexicalEntryContent(XmlNode lexiconChild, LexicalEntry lexicalEntry, IDictionary <string, Sense> senses, IDictionary <string, Synset> synsets, bool loadRelations)
        {
            foreach (XmlNode lexicalEntryChild in lexiconChild.ChildNodes)
            {
                if (lexicalEntryChild.Name == "Lemma")
                {
                    lexicalEntry.Lemma        = lexicalEntryChild.Attributes["writtenForm"].Value;
                    lexicalEntry.PartOfSpeech = PartOfSpeechExtensions.Parse(lexicalEntryChild.Attributes["partOfSpeech"].Value);
                }
                else if (lexicalEntryChild.Name == "Form")
                {
                    lexicalEntry.Forms.Add(lexicalEntryChild.Attributes["writtenForm"].Value);
                }
                else if (lexicalEntryChild.Name == "Sense")
                {
                    var sense = senses.GetOrAdd(
                        lexicalEntryChild.Attributes["id"].Value,
                        id => new Sense {
                        Id = id
                    });

                    sense.LexicalEntry = lexicalEntry;
                    sense.Synset       = synsets.GetOrAdd(
                        lexicalEntryChild.Attributes["synset"].Value,
                        id => new Synset {
                        Id = id
                    });

                    if (loadRelations)
                    {
                        foreach (XmlNode relationNode in lexicalEntryChild.ChildNodes)
                        {
                            var relation = new SenseRelation
                            {
                                Type     = Enum.Parse <SenseRelationType>(relationNode.Attributes["relType"].Value),
                                SourceId = sense.Id,
                                TargetId = relationNode.Attributes["target"].Value
                            };
                            sense.Relations.Add(relation);
                        }
                    }

                    lexicalEntry.Senses.Add(sense);
                }
                else if (lexicalEntryChild.Name == "SyntacticBehaviour")
                {
                    lexicalEntry.SyntacticBehaviours.Add(new SyntacticBehaviour
                    {
                        Senses = lexicalEntryChild.Attributes["senses"].Value,
                        SubcategorizationFrame = lexicalEntryChild.Attributes["subcategorizationFrame"].Value
                    });
                }
            }
        }
        private List <LexicalEntry> CreateLexicalEntryFromOxfordDictionaryResponse(string word, string response)
        {
            var odData     = JsonConvert.DeserializeObject <OxfordDictionaryEntityV2>(response);
            var result     = new List <LexicalEntry>();
            var audioFiles = new List <AudioFile>();

            var odLexicalEntries = odData?.Results?.FirstOrDefault()?.LexicalEntries;

            if (odLexicalEntries?.Count > 0)
            {
                foreach (var le in odLexicalEntries)
                {
                    var lexicalEntry = new LexicalEntry
                    {
                        Word            = le.Text,
                        LexicalCategory = le.LexicalCategory.Text,
                        OxfordDictionaryLexicalEntryV2Json = JsonConvert.SerializeObject(le)
                    };

                    var audioFileUri = le.Pronunciations?.FirstOrDefault()?.AudioFile;
                    if (audioFileUri != null)
                    {
                        var audioFile = audioFiles.FirstOrDefault(af => af.Url == audioFileUri.AbsoluteUri)
                                        ?? _oxfordDictionariesCacheDBContext.AudioFiles.FirstOrDefault(af => af.Url == audioFileUri.AbsoluteUri);
                        if (audioFile == null)
                        {
                            audioFile = new AudioFile
                            {
                                Url            = audioFileUri.AbsoluteUri,
                                FileName       = Path.GetFileName(audioFileUri.LocalPath),
                                Data           = _fileDownloader.GetFileFromUrl(audioFileUri.AbsoluteUri),
                                LexicalEntries = new List <LexicalEntry>()
                            };
                            audioFiles.Add(audioFile);
                        }
                        lexicalEntry.AudioFile = audioFile;
                    }
                    _oxfordDictionariesCacheDBContext.LexicalEntries.Add(lexicalEntry);
                    result.Add(lexicalEntry);
                }
            }
            else
            {
                var lexicalEntry = new LexicalEntry {
                    Word = word
                };
                result.Add(lexicalEntry);
                _oxfordDictionariesCacheDBContext.LexicalEntries.Add(lexicalEntry);
            }
            _oxfordDictionariesCacheDBContext.SaveChanges();
            return(result);
        }
        public override bool Parse(SyntacticState state)
        {
            // this is a special case when we are trying to
            // catch null-coalescing-expression without trailing "?"
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            // ensure that usual null-coalescing-expression can be parsed
            if (!NullCoalescingExpression.S.Parse(state))
            {
                return(false);
            }

            // we should not do anything if captured expression
            // doesn't end with a "?"
            LexicalEntry entry = state.GetInner(state.InnerPosition - 1);

            if (state.GetOuter(entry) != "?")
            {
                return(true);
            }

            // get a position of the last type entry
            int lastTypeIndex = 0;

            for (int i = innerIndex; i <= state.InnerPosition; i++)
            {
                if (state.CheckEntry(Type.S.Key, i))
                {
                    lastTypeIndex = i;
                }
            }

            // reset parsing and make another attempt
            // ignoring nullable types after specified index
            state.Reset(innerIndex, outerIndex);
            state.SetFlag(StateFlags.IgnoreNullableAfterPosition, lastTypeIndex - 1);

            bool parsed = ParseMany(
                state,
                ConditionalOrExpression.S,
                DoubleQuestionTerminal.S);

            state.ResetFlag(StateFlags.IgnoreNullableAfterPosition);
            return(parsed);
        }
Ejemplo n.º 6
0
        public override bool Parse(SyntacticState state)
        {
            if (state.IsEndOfData)
            {
                return(false);
            }

            LexicalEntry entry = state.GetInner(state.InnerPosition);

            if (entry.Key != Identifier.S.Key)
            {
                return(false);
            }

            // parse all identidiers except those
            // which are similar to LINQ keywords
            string name = state.GetOuter(entry);

            if (name == "ascending" ||
                name == "by" ||
                name == "descending" ||
                name == "equals" ||
                name == "from" ||
                name == "group" ||
                name == "in" ||
                name == "into" ||
                name == "join" ||
                name == "let" ||
                name == "on" ||
                name == "orderby" ||
                name == "select" ||
                name == "where")
            {
                return(false);
            }

            state.AddAbsolute(
                Key,
                state.InnerPosition + 1,
                entry.EndPosition);

            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Tries to parse an entity from the specified syntactic machine state.
        /// In case of success returns true and advances parsing position.
        /// </summary>
        public override bool Parse(SyntacticState state)
        {
            if (state.IsEndOfData)
            {
                return(false);
            }

            LexicalEntry entry = state.GetInner(state.InnerPosition);

            if (entry.Key != m_item.Key)
            {
                return(false);
            }

            state.AddAbsolute(
                Key,
                state.InnerPosition + 1,
                entry.EndPosition);

            return(true);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Tries to parse an entity from the specified syntactic machine state.
        /// In case of success returns true and advances parsing position.
        /// </summary>
        public override bool Parse(SyntacticState state)
        {
            if (state.IsEndOfData)
            {
                return(false);
            }

            LexicalEntry entry = state.GetInner(state.InnerPosition);
            string       text  = state.GetOuter(entry);

            if (text != m_text)
            {
                return(false);
            }

            state.AddAbsolute(
                Key,
                state.InnerPosition + 1,
                entry.EndPosition);

            return(true);
        }
Ejemplo n.º 9
0
        public void TestExistingWord()
        {
            var searchResult = _client.SearchEntries("study", CancellationToken.None).Result;

            // result is present
            Assert.True(searchResult != null);
            // result is not empty
            Assert.True(searchResult.Results.Length > 0);
            // result match
            Result result = searchResult.Results.First();

            Assert.True(result.Id == "study" && result.Word == "study");
            // element in lexical
            Assert.True(result.LexicalEntries.Length == 2);
            Assert.True(result.LexicalEntries.First().LexicalCategory == "Noun");
            // Sense
            LexicalEntry lexicalEntry = result.LexicalEntries.First();

            Assert.True(lexicalEntry.Entries.Length == 1);
            Assert.True(lexicalEntry.Entries.First().Senses.Length == 5);
            Sense[] eachDefinition = lexicalEntry.Entries.First().Senses;
            Assert.True(eachDefinition.First().Definitions.First() == "the devotion of time and attention to gaining knowledge of an academic subject, especially by means of books");
        }
Ejemplo n.º 10
0
        public void LexicalEntry_Uncovered()
        {
            LexicalEntry entry = new LexicalEntry("key", 1, 2);

            Assert.AreEqual("key [1:2]", entry.ToString());
        }
Ejemplo n.º 11
0
        public override bool Parse(SyntacticState state)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            // null-coalescing-expression is required
            if (!NullCoalescingExpression.S.Parse(state))
            {
                return(false);
            }

            // if captured expression ends with "?"
            LexicalEntry entry = state.GetInner(state.InnerPosition - 1);

            if (state.GetOuter(entry) == "?")
            {
                // check whether "? and :" part goes after
                bool full = ParseAll(
                    state,
                    QuestionTerminal.S,
                    Expression.S,
                    ColonTerminal.S,
                    Expression.S);

                // if so, everything is OK
                if (full)
                {
                    state.AddBack(Key, innerIndex, outerIndex);
                    return(true);
                }

                // if not, make another attempt to parse conditional expression
                // handling null-coalescing-expression without trailing "?"
                state.Reset(innerIndex, outerIndex);

                // if we could do that, we can return
                if (ParseAll(
                        state,
                        NullCoalescingExpressionShorten.S,
                        QuestionTerminal.S,
                        Expression.S,
                        ColonTerminal.S,
                        Expression.S))
                {
                    return(true);
                }

                // if not, parse initial null-coalescing-expression once again
                NullCoalescingExpression.S.Parse(state);
            }

            ParseAll(
                state,
                QuestionTerminal.S,
                Expression.S,
                ColonTerminal.S,
                Expression.S);

            state.AddBack(Key, innerIndex, outerIndex);
            return(true);
        }
Ejemplo n.º 12
0
        public ParseResult Parse(string fileName, bool loadRelations)
        {
            var result         = new ParseResult();
            var lexicalEntries = new Dictionary <string, LexicalEntry>();
            var senses         = new Dictionary <string, Sense>();
            var synsets        = new Dictionary <string, Synset>();

            var document = new XmlDocument();

            document.Load(fileName);

            foreach (XmlNode lexicon in document.DocumentElement.ChildNodes)
            {
                var language = lexicon.Attributes["language"].Value;

                Console.WriteLine($"Started processing lexicon \"{lexicon.Attributes["label"].Value} ({language})\".");

                foreach (XmlNode lexiconChild in lexicon.ChildNodes)
                {
                    if (lexiconChild.Name == "LexicalEntry")
                    {
                        var lexicalEntry = new LexicalEntry
                        {
                            Id       = lexiconChild.Attributes["id"].Value,
                            Language = language,
                        };
                        lexicalEntries.Add(lexicalEntry.Id, lexicalEntry);

                        ParseLexicalEntryContent(lexiconChild, lexicalEntry, senses, synsets, loadRelations);
                    }
                    else if (lexiconChild.Name == "Synset")
                    {
                        var synset = synsets.GetOrAdd(
                            lexiconChild.Attributes["id"].Value,
                            id => new Synset {
                            Id = id,
                        });

                        synset.Ili          = lexiconChild.Attributes["ili"].Value;
                        synset.PartOfSpeech = PartOfSpeechExtensions.Parse(lexiconChild.Attributes["partOfSpeech"].Value);

                        ParseSynsetContent(lexiconChild, synset, loadRelations);
                    }
                }
            }
            Console.WriteLine($"Loaded {lexicalEntries.Count} lexical entries, {senses.Count} senses and {synsets.Count} synsets.");

            result.LexicalEntries = lexicalEntries.Values;
            if (loadRelations)
            {
                var senseRelations  = new List <SenseRelation>();
                var synsetRelations = new List <SynsetRelation>();

                foreach (var sense in senses.Values)
                {
                    senseRelations.AddRange(sense.Relations);
                    sense.Relations = null;
                }
                foreach (var synset in synsets.Values)
                {
                    synsetRelations.AddRange(synset.Relations);
                    synset.Relations = null;
                }
                result.SenseRelations  = senseRelations;
                result.SynsetRelations = synsetRelations;
                Console.WriteLine($"Loaded {senseRelations.Count} sense relations, {synsetRelations.Count} synset relations.");
            }

            return(result);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Gets input data related to specified entry.
 /// </summary>
 public string GetOuter(LexicalEntry entry)
 {
     return(m_outerData.Substring(entry.StartPosition, entry.Length));
 }