Exemplo n.º 1
0
        public static SentenceUnderstander LoadFromPatterns(
            IEnumerable <GenerativePattern> generativePatterns,
            IRussianGrammarParser grammarParser,
            IRussianLexicon russianLexicon,
            MeaningBuilder meaningBuilder)
        {
            var concreteUnderstanders = new List <ConcretePatternOfUnderstanding>();
            var result = new SentenceUnderstander(concreteUnderstanders);

            foreach (var generativePattern in generativePatterns)
            {
                var concretePatterns = generativePattern
                                       .GenerateConcretePatterns(grammarParser, russianLexicon)
                                       .AsImmutable();

                var ambiguouslyUnderstoodPatterns = (
                    from it in concretePatterns
                    let pattern = it.PatternWithMeaning.Pattern
                                  let understanding = Understand(
                        pattern.AsParsedSentence(),
                        concreteUnderstanders,
                        meaningBuilder)
                                                      where understanding.IsRight
                                                      select new
                {
                    Pattern = pattern,
                    NewPatternId = generativePattern.PatternId,
                    ExistingPatternId = understanding.Right !.PatternId
                }
Exemplo n.º 2
0
        public static AnnotatedSentence ParseAnnotatedPreservingQuotes(
            this IRussianGrammarParser @this,
            string annotatedText,
            IRussianLexicon russianLexicon)
        {
            var quotedSequences           = LocateQuotedSequences(annotatedText);
            var quotationSubstitutions    = MakeSubstitutionsOfQuotedSequencesWithSingleWords(quotedSequences);
            var textWithSubstitutedQuotes = ApplySubstitutions(quotationSubstitutions, annotatedText);
            var markedupWords             = @this.ParseMarkup(textWithSubstitutedQuotes).ToArray();
            var sentenceWithoutMarkup     = string.Join(" ", markedupWords.Select(w => w.Content));
            var parsedText        = WorkaroundForSituationsWhenParserRetunsFlatChainOfElements(@this, sentenceWithoutMarkup);
            var sentenceStructure = RestoreQuotations(quotationSubstitutions, parsedText).Single(
                _ => true,
                _ => new InvalidOperationException(
                    $"Text '{textWithSubstitutedQuotes}' could not be tokenized unambiguously. Please reformulate it."));

            ProgramLogic.Check(
                (
                    markedupWords.Select(w => w.Content).AsImmutable(),
                    IterateDepthFirst(parsedText)
                    .OrderBy(it => it.PositionInSentence)
                    .Select(it => it.Content)
                    .AsImmutable()
                ) switch
            {
                var(markups, grammars) when !markups.SequenceEqual(grammars, Impl.RussianIgnoreCase) =>
                Some(
                    "Parsing marked-up sentence gave the result that is incompatible with " +
                    "the structure returned by russian grammmar parser." + Environment.NewLine +
                    "Markup parsing result: " + string.Join(";", markups) + Environment.NewLine +
                    "Russian grammar parsing result: " + string.Join(";", grammars)),
                _ => None
            });
Exemplo n.º 3
0
 public Patterns(
     ScenarioContext scenarioContext,
     IRussianGrammarParser grammarParser,
     IRussianLexicon russianLexicon)
 {
     _scenarioContext = scenarioContext;
     _grammarParser   = grammarParser;
     _russianLexicon  = russianLexicon;
 }
Exemplo n.º 4
0
 public static SentenceUnderstander LoadFromEmbeddedResources(
     IRussianGrammarParser grammarParser,
     IRussianLexicon russianLexicon,
     MeaningBuilder meaningBuilder)
 =>
 LoadFromPatterns(
     ParsePatterns(ReadEmbeddedResource("Ginger.Runner.SentenceUnderstandingRules.txt")),
     grammarParser,
     russianLexicon,
     meaningBuilder);
Exemplo n.º 5
0
 public InternalFunctionalityTestSteps(
     IRussianGrammarParser grammarParser,
     IRussianLexicon russianLexicon)
 =>
 (_grammarParser, _russianLexicon) = (grammarParser, russianLexicon);