private static string TreatObjectCase(SentenceInformationDto sentence, WordInformationDto verbe, string questionText)
        {
            questionText = questionText.Replace(verbe.Word, verbe.Lemma);

            var verbeAux =
                sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "aux" && d.GovernorGloss == verbe.Word);

            if (verbeAux != null)
            {
                questionText = questionText.Replace(verbeAux.DependentGloss, "");
            }

            switch (verbe.PartOfSpeech.ToLower())
            {
            case "vbd":
                return($"What did {questionText}");

            case "vbz":
                return($"What does {questionText}");

            case "vbp":
                return($"What do {questionText}");

            default:
                return(null);
            }
        }
 private static bool SubjectIsReferingToPerson(WordInformationDto subject)
 {
     return(subject.NamedEntityRecognition.ToLower() == "person" ||
            subject.PartOfSpeech.ToLower() == "prp" ||
            subject.PartOfSpeech.ToLower() == "prp$" ||
            subject.PartOfSpeech.ToLower() == "nns" ||
            subject.PartOfSpeech.ToLower() == "nn" ||
            subject.PartOfSpeech.ToLower() == "nnp");
 }
Beispiel #3
0
        private static GeneratedQuestion TreatCaseWhereAnswerIsPerson(SentenceInformationDto sentence,
                                                                      WordInformationDto firstWord, WordInformationDto copVerbe, string answer)
        {
            var questionText = sentence.SentenceText
                               .Replace(firstWord.Word, firstWord.Lemma)
                               .Replace(copVerbe.Word, "")
                               .Replace(answer, "");
            string question = $"Who {copVerbe.Word} {questionText}";

            return(new GeneratedQuestion {
                Answer = answer, Question = question
            });
        }
        public static string GenerateAnswer(SentenceInformationDto sentence, SentenceDependencyDto sentenceDependency = null,
                                            WordInformationDto wordInformation = null, WordInformationDto subjectWord = null)
        {
            var baseAnswer = GetBaseAnswer(sentence, sentenceDependency, wordInformation ?? subjectWord);
            var answer     = baseAnswer;
            var answerDet  = GetWordDET(sentence, answer);

            if (answer != string.Empty)
            {
                answer = TreatAnswerCompound(sentence, baseAnswer, answer);

                answer = TreatAnswerNumMod(sentence, baseAnswer, answer);

                answer = TreatAnswerPossession(sentence, baseAnswer, answer);

                answer = TreatAmod(sentence, baseAnswer, answer);

                answer = TreatNMODSentence(sentence, baseAnswer, answer);
            }

            if (subjectWord != null)
            {
                var subjectAMOD = sentence.Dependencies.Where(d => d.Dep == "amod" && d.GovernorGloss == subjectWord.Word);
                foreach (var subjAmod in subjectAMOD)
                {
                    answer = $"{subjAmod.DependentGloss} {subjectWord.Word}";
                }
            }

            if (sentenceDependency != null && string.IsNullOrEmpty(answer))
            {
                answer    = sentenceDependency.DependentGloss;
                answerDet = GetWordDET(sentence, sentenceDependency.DependentGloss);
            }

            if (wordInformation != null)
            {
                answerDet = GetWordDET(sentence, wordInformation.Word);
            }

            if (answerDet != null)
            {
                answer = $"{answerDet.DependentGloss} {answer}";
            }

            return(answer);
        }
Beispiel #5
0
        private static string GetSentenceCases(SentenceInformationDto sentence, WordInformationDto sentenceDate,
                                               string composedAnswer)
        {
            var answerCases = sentence.Dependencies
                              .Where(d => d.Dep == "case" && d.GovernorGloss == sentenceDate.Word)
                              .ToList();

            var wordsToReplace = composedAnswer;

            foreach (var ansCase in answerCases)
            {
                wordsToReplace = Helper.ElementIsBeforeWord(sentence.Words, ansCase.DependentGloss, sentenceDate.Word)
                    ? $"{ansCase.DependentGloss} {composedAnswer}"
                    : $"{composedAnswer} {ansCase.DependentGloss}";
            }
            return(wordsToReplace);
        }
        public static GeneratedQuestion TreatSentenceWithAgent(SentenceInformationDto sentence, SentenceDependencyDto sentenceAGENT,
                                                               WordInformationDto subject)
        {
            var answer = sentenceAGENT.DependentGloss;

            var subjectDet = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "det" &&
                                                                  d.GovernorGloss == subject.Word);
            var subj = subject.Word;

            if (subjectDet != null)
            {
                subj = $"{subjectDet.DependentGloss} {subject.Word}";
            }

            var otherAgents = sentence.Dependencies.Where(d =>
                                                          d.Dep.ToLower() == "nmod:agent" &&
                                                          d.GovernorGloss == sentenceAGENT.GovernorGloss &&
                                                          d.DependentGloss != answer);

            var connectionCC = sentence.Dependencies.FirstOrDefault(d =>
                                                                    d.Dep.ToLower() == "cc" &&
                                                                    d.GovernorGloss == answer);

            if (connectionCC != null)
            {
                foreach (var agent in otherAgents)
                {
                    answer = $"{answer} {connectionCC.DependentGloss} {agent.DependentGloss}";
                }
            }

            var verbe      = Helper.FindWordInList(sentence.Words, sentenceAGENT.GovernorGloss);
            var answerWord = Helper.FindWordInList(sentence.Words, sentenceAGENT.DependentGloss);

            if (answerWord.PartOfSpeech.ToLower() == "nnp" ||
                answerWord.NamedEntityRecognition.ToLower() == "person")
            {
                var question = $"Who {verbe.Word} {subj.ToLower()}";
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
            return(null);
        }
Beispiel #7
0
        public static GeneratedQuestion CreateQuestionBasedOnSubject(SentenceInformationDto sentence, SentenceDependencyDto subjectFromSentence,
                                                                     WordInformationDto subject)
        {
            if (subjectFromSentence == null)
            {
                return(null);
            }
            var subjectRelationWord = Helper.FindWordInList(sentence.Words, subjectFromSentence.GovernorGloss);
            var answer = AnswerGenerator.GenerateAnswer(sentence, wordInformation: subject);

            if (subjectRelationWord.PartOfSpeech == "VBZ" ||
                subjectRelationWord.PartOfSpeech == "VBN" ||
                subjectRelationWord.PartOfSpeech == "VBD")
            {
                string question;

                if (subject.NamedEntityRecognition.ToLower() == "person" ||
                    subject.PartOfSpeech.ToLower() == "prp" ||
                    subject.PartOfSpeech.ToLower() == "nnp")
                {
                    question = sentence.SentenceText.Replace(answer, "Who");
                    question = Helper.TrimQuestion(question, "Who");
                }
                else
                {
                    question = sentence.SentenceText.Replace(answer, "What");
                    question = Helper.TrimQuestion(question, "What");
                }
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
            if (subjectRelationWord.PartOfSpeech == "NN" ||
                subjectRelationWord.PartOfSpeech == "NNS" ||
                subjectRelationWord.PartOfSpeech == "NNP" ||
                subjectRelationWord.PartOfSpeech == "NNPS")
            {
            }

            return(null);
        }
        public static GeneratedQuestion TreatSentenceWithNSUBJ(SentenceInformationDto sentence, SentenceDependencyDto sentenceNSUBJ,
                                                               WordInformationDto subject)
        {
            var subjectRelation = Helper.FindWordInList(sentence.Words, sentenceNSUBJ.GovernorGloss);

            var answer = AnswerGenerator.GenerateAnswer(sentence, subjectWord: subject);

            if (subjectRelation.PartOfSpeech.ToLower() == "vbz" ||
                subjectRelation.PartOfSpeech.ToLower() == "vbd" ||
                subjectRelation.PartOfSpeech.ToLower() == "vbn")
            {
                string question;
                if (SubjectIsReferingToPerson(subject))
                {
                    var questionText = sentence.SentenceText.Replace(answer, "Who ");
                    question = $"{questionText}";
                    question = Helper.TrimQuestion(question, "Who");
                    return(new GeneratedQuestion {
                        Answer = answer, Question = question
                    });
                }
                if (subject.NamedEntityRecognition.ToLower() != "o")
                {
                    var questionText = sentence.SentenceText.Replace(answer, "Which ");
                    question = $"{questionText}";
                    question = Helper.TrimQuestion(question, "Which");
                }
                else
                {
                    var questionText = sentence.SentenceText.Replace(answer, "What ");
                    question = $"{questionText}";
                    question = Helper.TrimQuestion(question, "What");
                }
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
            return(null);
        }
Beispiel #9
0
        public static GeneratedQuestion TreatSimpleXCOMPSentence(SentenceInformationDto sentence, SentenceDependencyDto subjectFromRes,
                                                                 WordInformationDto subject, SentenceDependencyDto sentenceXCOMP)
        {
            var subjectPossession = Helper.GetSubjectPossession(sentence);

            var answer       = AnswerGenerator.GenerateAnswer(sentence, subjectFromRes, subjectWord: subject);
            var predicate    = sentence.Words.FirstOrDefault(w => w.Word == subjectFromRes.GovernorGloss);
            var wordInResult = sentence.Words.FirstOrDefault(w => w.Word == sentenceXCOMP.DependentGloss);

            if (wordInResult == null || predicate == null)
            {
                return(null);
            }

            var pos = wordInResult.PartOfSpeech;

            if (pos != "JJ")
            {
                return(null);
            }

            if (subjectPossession != null)
            {
                var question = $"How does {subjectPossession} {subject.Lemma} {predicate.Lemma}";
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
            else
            {
                var question = $"How does {subject.Lemma} {predicate.Lemma}";
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
        }
Beispiel #10
0
        public static GeneratedQuestion TreatDateSentence(SentenceInformationDto sentence, WordInformationDto sentenceDate,
                                                          SentenceDependencyDto subjectFromSentence)
        {
            var composedAnswer = AnswerGenerator.GenerateAnswer(sentence, subjectWord: sentenceDate);
            var sentencesCases = GetSentenceCases(sentence, sentenceDate, composedAnswer);
            var firstWord      = sentence.Words.First();
            var question       = sentence.SentenceText
                                 .Replace(sentencesCases, "");

            if (question.Contains(firstWord.Word) && !WordIsName(firstWord))
            {
                question = question.Replace(firstWord.Word, firstWord.Lemma);
            }

            if (subjectFromSentence != null)
            {
                return(GenerateQuestion(sentence, sentenceDate, subjectFromSentence));
            }

            question = $"When did {question}";
            return(new GeneratedQuestion {
                Answer = composedAnswer, Question = question
            });
        }
 private static string GetBaseAnswer(SentenceInformationDto sentence, SentenceDependencyDto sentenceDependency, WordInformationDto wordInformation)
 {
     if (sentenceDependency != null && wordInformation != null && (wordInformation.NamedEntityRecognition.ToLower() == "person" ||
                                                                   wordInformation.PartOfSpeech.ToLower() == "nn"))
     {
         return(wordInformation.Word);
     }
     if (sentenceDependency != null)
     {
         var sentenceDep = Helper.FindWordInList(sentence.Words, sentenceDependency.DependentGloss);
         if (sentenceDep.PartOfSpeech.ToLower() == "vb" ||
             sentenceDep.PartOfSpeech.ToLower() == "vbd" ||
             sentenceDep.PartOfSpeech.ToLower() == "vbg" ||
             sentenceDep.PartOfSpeech.ToLower() == "vbn" ||
             sentenceDep.PartOfSpeech.ToLower() == "vbp" ||
             sentenceDep.PartOfSpeech.ToLower() == "vbz")
         {
             return(sentenceDependency.GovernorGloss);
         }
         return(sentenceDependency.DependentGloss);
     }
     return(wordInformation != null ? wordInformation.Word : string.Empty);
 }
Beispiel #12
0
 private static bool MustReturnNull(WordInformationDto wordInfo)
 {
     return(wordInfo == null || wordInfo.PartOfSpeech.ToLower() == "prp" ||
            wordInfo.PartOfSpeech.ToLower() == "nnps");
 }
Beispiel #13
0
        public static GeneratedQuestion TreatSimpleCOPSentence(SentenceInformationDto sentence, WordInformationDto subject,
                                                               SentenceDependencyDto sentenceCOP)
        {
            var copPartOfSpeech = Helper.FindWordInList(sentence.Words, sentenceCOP.GovernorGloss).PartOfSpeech;
            var copVerbe        = Helper.FindWordInList(sentence.Words, sentenceCOP.DependentGloss);
            var baseAnswer      = Helper.FindWordInList(sentence.Words, sentenceCOP.GovernorGloss);
            var subjectWord     = Helper.FindWordInList(sentence.Words, subject.Word);

            if (MustReturnNull(subjectWord))
            {
                return(null);
            }
            var answer = AnswerGenerator.GenerateAnswer(sentence, sentenceCOP, subjectWord: subjectWord);

            var    firstWord = sentence.Words.First();
            string question;

            if (baseAnswer.NamedEntityRecognition.ToLower() == "person" && copVerbe.Lemma == "be")
            {
                return(TreatCaseWhereAnswerIsPerson(sentence, firstWord, copVerbe, answer));
            }

            if (subjectWord.PartOfSpeech.ToLower() == "nn")
            {
                return(TreatCaseWhereAnswerIsObject(sentence, answer));
            }

            if (copVerbe != null && copVerbe.Lemma == "be")
            {
                question = $"What {copVerbe.Word} {subjectWord.Word}";
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }

            if (subjectWord.NamedEntityRecognition.ToLower() == "person" ||
                subjectWord.PartOfSpeech.ToLower() == "nnp" ||
                subjectWord.PartOfSpeech.ToLower() == "nns")
            {
                question = $"{sentence.SentenceText.Replace(answer, "Who")}";
                question = Helper.TrimQuestion(question, "Who");
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }

            if (baseAnswer.NamedEntityRecognition.ToLower() == "person" ||
                baseAnswer.PartOfSpeech.ToLower() == "nnp")
            {
                return(TreatCaseWithPersonAndVerbeNot_IS_(sentence, sentenceCOP, answer));
            }

            question = copPartOfSpeech == "JJ" || copPartOfSpeech == "NNS" ?
                       $"{sentence.SentenceText.Replace(answer, "What")}" :
                       $"{sentence.SentenceText.Replace(answer, "Which")}";

            if (sentence.SentenceText + "?" == question)
            {
                return(null);
            }
            question = Helper.TrimQuestion(question, "What");
            question = Helper.TrimQuestion(question, "Which");
            return(new GeneratedQuestion {
                Answer = answer, Question = question
            });
        }
Beispiel #14
0
        public static GeneratedQuestion TreatSentenceWithIN(SentenceInformationDto sentence, SentenceDependencyDto sentenceIN,
                                                            SentenceDependencyDto subjectFromSentence, WordInformationDto subject)
        {
            var inWord = Helper.FindWordInList(sentence.Words, sentenceIN.DependentGloss);

            if (inWord.PartOfSpeech.ToLower() != "nn")
            {
                return(null);
            }
            var nounOF = sentence.Dependencies.FirstOrDefault(d => d.Dep == "nmod:of" &&
                                                              d.GovernorGloss == inWord.Word);

            if (nounOF == null || subjectFromSentence == null)
            {
                return(null);
            }
            var answer   = nounOF.DependentGloss;
            var verbe    = Helper.FindWordInList(sentence.Words, subjectFromSentence.GovernorGloss);
            var verbeAND = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "conj:and" &&
                                                                d.GovernorGloss == verbe.Word);

            if (verbeAND != null)
            {
                var verbeAndWord = Helper.FindWordInList(sentence.Words, verbeAND.DependentGloss);

                if (verbe.PartOfSpeech.ToLower() == "vbz")
                {
                    var question = $"Where does {subject.Word} {verbeAndWord.Word} and {verbe.Word}";
                    return(new GeneratedQuestion {
                        Answer = answer, Question = question
                    });
                }
                else
                {
                    var question = $"Where did {subject.Word} {verbeAndWord.Lemma} and {verbe.Lemma}";
                    return(new GeneratedQuestion {
                        Answer = answer, Question = question
                    });
                }
            }
            if (verbe.PartOfSpeech.ToLower() == "vbz")
            {
                var question = $"Where does {subject.Word}  {verbe.Word}";
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
            else
            {
                var question = $"Where did {subject.Word} {verbe.Lemma}";
                return(new GeneratedQuestion {
                    Answer = answer, Question = question
                });
            }
        }
        private static string TreatPersonCase(SentenceInformationDto sentence, string questionText,
                                              SentenceDependencyDto sentenceDependency, WordInformationDto baseAnswer, bool isPast = false)
        {
            if (baseAnswer.NamedEntityRecognition.ToLower() == "person" ||
                baseAnswer.PartOfSpeech.ToLower() == "nnp")
            {
                return(isPast ?
                       $"Who did {questionText}" :
                       $"Who {questionText}");
            }

            var dobjAux = sentence.Dependencies
                          .FirstOrDefault(d => d.Dep.ToLower() == "auxpass" && d.GovernorGloss == sentenceDependency.GovernorGloss);

            if (dobjAux != null)
            {
                questionText = questionText.Replace(dobjAux.DependentGloss, "");
                return($"How {dobjAux.DependentGloss} {questionText}");
            }
            return($"How is {questionText}");
        }
Beispiel #16
0
        private static GeneratedQuestion GenerateQuestion(SentenceInformationDto sentence, WordInformationDto sentenceDate,
                                                          SentenceDependencyDto subjectFromSentence)
        {
            var question     = string.Empty;
            var verbeWord    = Helper.FindWordInList(sentence.Words, subjectFromSentence.GovernorGloss);
            var verbeAuxPass = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "auxpass" &&
                                                                    d.GovernorGloss == verbeWord.Word);

            if (verbeWord.PartOfSpeech.ToLower() == "vbd")
            {
                question = question.Replace(verbeWord.Word, verbeWord.Lemma);
            }

            var verbeAux = sentence.Dependencies.FirstOrDefault(d =>
                                                                d.Dep.ToLower() == "aux" &&
                                                                d.GovernorGloss == verbeWord.Word);

            if (verbeAux != null)
            {
                question = question.Replace(verbeAux.DependentGloss, "");
                question = $"When {verbeAux.DependentGloss} {question}";
                return(new GeneratedQuestion {
                    Answer = sentenceDate.Word, Question = question
                });
            }

            if (verbeAuxPass != null)
            {
                question = question.Replace(verbeAuxPass.DependentGloss, "");
                question = $"When {verbeAuxPass.DependentGloss} {question}";
                return(new GeneratedQuestion {
                    Answer = sentenceDate.Word, Question = question
                });
            }
            return(null);
        }
Beispiel #17
0
        public static GeneratedQuestion TreatSimpleCCSentence(SentenceInformationDto sentence, WordInformationDto subject)
        {
            if (subject.Word == null)
            {
                return(null);
            }
            var subjectWord = Helper.FindWordInList(sentence.Words, subject.Word);

            string question;
            var    answer = AnswerGenerator.GenerateAnswer(sentence, subjectWord: subjectWord);

            if (subjectWord.PartOfSpeech.ToLower() == "nnp" ||
                subjectWord.PartOfSpeech.ToLower() == "nns" ||
                subjectWord.PartOfSpeech.ToLower() == "prp" ||
                subjectWord.NamedEntityRecognition.ToLower() == "person")
            {
                question = $"{sentence.SentenceText.Replace(answer, "Who")}";
                question = Helper.TrimQuestion(question, "Who");
            }
            else
            {
                question = $"{sentence.SentenceText.Replace(answer, "Who/What")}";
                question = Helper.TrimQuestion(question, "Who/What");
            }
            return(new GeneratedQuestion {
                Answer = answer, Question = question
            });
        }
Beispiel #18
0
 private static bool WordIsName(WordInformationDto word)
 {
     return(word.NamedEntityRecognition.ToLower() == "person" ||
            word.NamedEntityRecognition.ToLower() == "location");
 }
        public static GeneratedQuestion Generate(SentenceInformationDto sentence)
        {
            var sentenceDET     = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "det");
            var composedSubject = string.Empty;

            if (sentenceDET != null)
            {
                composedSubject = $"{sentenceDET.DependentGloss} {sentenceDET.GovernorGloss}";
            }

            SentenceDependencyDto subjectFromSentence;
            var sentenceIN = sentence.Dependencies.FirstOrDefault(d => d.Dep == "nmod:in");

            if (sentenceIN != null)
            {
                var verbeFromIn = sentenceIN.GovernorGloss;
                subjectFromSentence = sentence.Dependencies.FirstOrDefault(d => (d.Dep.ToLower() == "nsubjpass" ||
                                                                                 d.Dep.ToLower() == "nsubj") && d.GovernorGloss == verbeFromIn);
            }
            else if (composedSubject != string.Empty)
            {
                subjectFromSentence = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nsubj");
            }
            else
            {
                subjectFromSentence = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nsubjpass") ??
                                      sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nsubj");
            }

            var subject = new WordInformationDto();

            if (subjectFromSentence != null)
            {
                subject = Helper.FindWordInList(sentence.Words, subjectFromSentence.DependentGloss);
            }


            var sentenceDate = Helper.GetSentenceDate(sentence);

            if (sentenceDate != null)
            {
                return(BasedOnDateGenerator.TreatDateSentence(sentence, sentenceDate, subjectFromSentence));
            }

            if (sentenceIN != null)
            {
                return(BasedOnINQGenerator.TreatSentenceWithIN(sentence, sentenceIN, subjectFromSentence, subject));
            }

            var sentenceCOP = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "cop");

            if (sentenceCOP != null)
            {
                return(BasedOnCOPQGenerator.TreatSimpleCOPSentence(sentence, subject, sentenceCOP));
            }

            var sentenceAGENT = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nmod:agent");

            if (sentenceAGENT != null)
            {
                return(BasedOnAGENTQGenerator.TreatSentenceWithAgent(sentence, sentenceAGENT, subject));
            }

            var sentenceDOBJ = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "dobj");

            if (sentenceDOBJ != null)
            {
                return(BasedOnDOBJQGenerator.TreatSentenceWithDOBJ(sentence, sentenceDOBJ));
            }

            var sentenceNSUBJ     = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nsubj");
            var sentenceNSUBJPASS = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nsubjpass");

            if (sentenceNSUBJ != null && sentenceNSUBJPASS == null)
            {
                return(BasedOnNSUBJQGenerator.TreatSentenceWithNSUBJ(sentence, sentenceNSUBJ, subject));
            }

            var sentenceXCOMP = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "xcomp");

            if (sentenceXCOMP != null)
            {
                return(BasedOnXCOMPQGenerator.TreatSimpleXCOMPSentence(sentence, subjectFromSentence, subject, sentenceXCOMP));
            }

            var sentenceCC = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "cc");

            if (sentenceCC != null)
            {
                return(BasedOnCCQGenerator.TreatSimpleCCSentence(sentence, subject));
            }

            var questionBasedOnSubject = BasedOnSubjectQGenerator.CreateQuestionBasedOnSubject(sentence, subjectFromSentence, subject);

            return(questionBasedOnSubject);
        }