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); } }
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 }); }
public static bool SentenceContainsYear(SentenceInformationDto sentence) { if (sentence.Words.Any(w => w.NamedEntityRecognition.ToLower() == "data")) { return(true); } var sentenceIN = sentence.Dependencies.Where(d => d.Dep == "nmod:in"); return(sentenceIN.Any(sentenceDep => StringIsYear(sentenceDep.DependentGloss))); }
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 TreatAmod(SentenceInformationDto sentence, string baseAnser, string answer) { var amodList = sentence.Dependencies.Where(d => d.Dep == "amod" && d.GovernorGloss == baseAnser).ToList(); foreach (var subjAmod in amodList) { answer = Helper.ElementIsBeforeWord(sentence.Words, subjAmod.DependentGloss, baseAnser) ? $"{subjAmod.DependentGloss} {answer}" : $"{answer} {subjAmod.DependentGloss}"; } return(answer); }
public static string GetSubjectPossession(SentenceInformationDto res) { var subjectHasPossesion = res.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nmod:poss"); var subjectPoss = String.Empty; if (subjectHasPossesion != null) { subjectPoss = FindWordInList(res.Words, subjectHasPossesion.DependentGloss).Word; } return(subjectPoss); }
private static bool MustContinue(SentenceInformation sentence, SentenceInformationDto sentenceInfoDto) { if (Helper.SentenceIsInvalid(sentenceInfoDto)) { return(true); } if (sentence.Dependencies.Count > 20 && !Helper.SentenceContainsYear(sentenceInfoDto)) { return(true); } return(false); }
private static string TreatAnswerCompound(SentenceInformationDto sentence, string baseAnswer, string answer) { var answerCompound = GetWordCompound(sentence, baseAnswer).ToList(); answerCompound = OrderByWordPosition(answerCompound, sentence.Words); foreach (var compound in answerCompound) { var compoundIsBefore = Helper.ElementIsBeforeWord(sentence.Words, compound.DependentGloss, baseAnswer); answer = compoundIsBefore ? $"{compound.DependentGloss} {answer}" : $"{answer} {compound.DependentGloss}"; } return(answer); }
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 }); }
private static GeneratedQuestion TreatCaseWithPersonAndVerbeNot_IS_(SentenceInformationDto sentence, SentenceDependencyDto sentenceCOP, string answer) { var questionText = Helper.TrimQuestionAfter(sentence.SentenceText, answer); questionText = questionText.Replace(answer, "") .Replace(sentenceCOP.DependentGloss, ""); string question = $"Who {sentenceCOP.DependentGloss} {questionText}"; return(new GeneratedQuestion { Answer = answer, Question = question }); }
private static string TreatAnswerNumMod(SentenceInformationDto sentence, string baseAnswer, string answer) { var sentenceNUMMOD = sentence.Dependencies .Where(d => d.Dep.ToLower() == "nummod" && d.GovernorGloss == baseAnswer) .ToList(); foreach (var numMod in sentenceNUMMOD) { answer = Helper.ElementIsBeforeWord(sentence.Words, numMod.DependentGloss, baseAnswer) ? $"{numMod.DependentGloss} {answer}" : $"{answer} {numMod.DependentGloss}"; } return(answer); }
private static string TreatAnswerPossession(SentenceInformationDto sentence, string baseAnswer, string answer) { var answerPoss = GetAnswerPossession(sentence, baseAnswer); if (answerPoss != null) { var possCase = GetPossessionCase(sentence, answerPoss.DependentGloss); answer = possCase != null ? $"{answerPoss.DependentGloss}{possCase.DependentGloss} {answer}" : $"{answerPoss.DependentGloss} {answer}"; } return(answer); }
public void GenerateQuestions(string topic) { topic = topic.Replace(" ", "_"); var outputJsonPath = DirectoryManager.GetOutputJsonPath(topic); var tpr = new TextProcessing(); var resultList = tpr.GetSentencesInformationFromJson(outputJsonPath); var questionList = new List <TopicQuestion>(); foreach (var sentence in resultList) { var dependencies = GetSentenceDependency(sentence); var words = GetSentenceWords(sentence); var sentenceInfo = new SentenceInformationDto(sentence.SentenceText, dependencies, words); if (MustContinue(sentence, sentenceInfo)) { continue; } GeneratedQuestion generatedQuestion; try { generatedQuestion = QuestionGenerator.Generate(sentenceInfo); } catch { continue; } if (string.IsNullOrEmpty(generatedQuestion?.Question)) { continue; } var cleanQuestion = QuestionCleaner.RemovePunctuationFromEnd(generatedQuestion.Question); cleanQuestion = $"{cleanQuestion}?"; var question = new TopicQuestion { Topic = topic, InitialSentence = sentence.SentenceText, Question = cleanQuestion, Answer = generatedQuestion.Answer }; questionList.Add(question); } DirectoryManager.WriteQuestionsToFile(questionList, topic); }
private static GeneratedQuestion TreatCaseWhereAnswerIsObject(SentenceInformationDto sentence, string answer) { var answerWords = answer.Split(' '); string questionText = null; foreach (var answerWord in answerWords) { questionText = sentence.SentenceText .Replace(answerWord, ""); } string question = $"What {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); }
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); }
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); }
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}"); }
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); }
private static string TreatNMODSentence(SentenceInformationDto sentence, string baseAnswer, string answer) { var sentenceNMODFor = sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nmod:for" && d.GovernorGloss == baseAnswer); if (sentenceNMODFor != null) { answer = $"{answer} for"; var compound = sentence.Dependencies.Where(d => d.Dep.ToLower() == "compound" && d.GovernorGloss == sentenceNMODFor.DependentGloss).ToList(); foreach (var elem in compound) { answer = Helper.ElementIsBeforeWord(sentence.Words, elem.DependentGloss, sentenceNMODFor.DependentGloss) ? $"{answer} {elem.DependentGloss} {sentenceNMODFor.DependentGloss}" : $"{answer} {sentenceNMODFor.DependentGloss} {elem.DependentGloss}"; } if (compound.Count == 0) { answer = $"{answer} for {sentenceNMODFor.DependentGloss}"; } } return(answer); }
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); }
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 }); } }
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); }
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 }); }
public static WordInformationDto GetSentenceDate(SentenceInformationDto sentence) { return(sentence.Words.FirstOrDefault(w => w.NamedEntityRecognition.ToLower() == "date")); }
private static IEnumerable <SentenceDependencyDto> GetWordCompound(SentenceInformationDto sentence, string word) { return(sentence.Dependencies.Where(d => d.Dep.ToLower() == "compound" && d.GovernorGloss == word)); }
private static SentenceDependencyDto GetWordDET(SentenceInformationDto sentence, string word) { return(sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "det" && d.GovernorGloss == word)); }
private static SentenceDependencyDto GetPossessionCase(SentenceInformationDto sentence, string poss) { return(sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "case" && d.GovernorGloss == poss)); }
private static SentenceDependencyDto GetAnswerPossession(SentenceInformationDto sentence, string answer) { return(sentence.Dependencies.FirstOrDefault(d => d.Dep.ToLower() == "nmod:poss" && d.GovernorGloss == answer)); }