public static string ContinuePhrase( Dictionary <string, string> nextWords, string phraseBeginning, int wordsCount) { var parsed = SentencesParserTask.ParseSentences(phraseBeginning); var lastline = parsed[parsed.Count - 1]; var result = new StringBuilder(); for (int i = 0; i < wordsCount; i++) { if (nextWords.ContainsKey(TakeLastWords(lastline))) { result.Append(" " + nextWords[TakeLastWords(lastline)]); lastline.Add(nextWords[TakeLastWords(lastline)]); } else { if (nextWords.ContainsKey(lastline[lastline.Count - 1])) { result.Append(" " + nextWords[lastline[lastline.Count - 1]]); lastline.Add(nextWords[lastline[lastline.Count - 1]]); } else { break; } } } return(phraseBeginning + result.ToString()); }
public static void Main(string[] args) { // Запуск автоматических тестов. Ниже список тестовых наборов, который нужно запустить. // Закомментируйте тесты на те задачи, к которым ещё не приступали, чтобы они не мешались в консоли. // Все непрошедшие тесты var testsToRun = new string[] { //"TextAnalysis.SentencesParser_Tests", // "TextAnalysis.FrequencyAnalysis_Tests", "TextAnalysis.TextGenerator_Tests", }; new AutoRun().Execute(new[] { "--stoponerror", // Останавливать после первого же непрошедшего теста. Закомментируйте, чтобы увидеть все падающие тесты "--noresult", "--test=" + string.Join(",", testsToRun) }); var text = File.ReadAllText("HarryPotterText.txt"); var sentences = SentencesParserTask.ParseSentences(text); var frequency = FrequencyAnalysisTask.GetMostFrequentNextWords(sentences); while (true) { Console.Write("Введите первое слово (например, harry): "); var beginning = Console.ReadLine(); if (string.IsNullOrEmpty(beginning)) { return; } var phrase = TextGeneratorTask.ContinuePhrase(frequency, beginning.ToLower(), 13); Console.WriteLine(phrase); } }
public void CorrectlyParse_SpecialCharacters() { var originalText = "b;\tc;\rd;\ne;\r\nf;\r\n\r\ng"; var escapedText = Regex.Escape(originalText); var expected = new List <List <string> > { new List <string> { "b" }, new List <string> { "c" }, new List <string> { "d" }, new List <string> { "e" }, new List <string> { "f" }, new List <string> { "g" } }; var actual = SentencesParserTask.ParseSentences(originalText); AssertAllSentencesEqual(expected, actual, escapedText); }
public void CorrectlyParse_NullSentences() { var text = @" And for Di, who heard this one first. 1.THE BOY WHO LIVED "; var expected = new List <List <string> > { new List <string> { "and", "for", "di", "who", "heard", "this", "one", "first" }, new List <string> { "the", "boy", "who", "lived" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void NotReturnEmptySentence([Values("..", "...!!?", "")] string text) { var expected = new List <List <string> >(); var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void ReturnCorrectResult_OnTextWithOneSentenceWithTwoWords() { var text = "b, c"; var expected = new List <List <string> > { new List <string> { "b", "c" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void ReturnCorrectResult_OnTextWithOneSentence_WithWordContainingApostrophe() { var text = "it's"; var expected = new List <List <string> > { new List <string> { "it's" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public static void Main(string[] args) { // Запуск автоматических тестов. Ниже список тестовых наборов, который нужно запустить. // Закомментируйте тесты на те задачи, к которым ещё не приступали, чтобы они не мешались в консоли. // Все непрошедшие тесты File.WriteAllText("C:\test\new.txt", "123456"); var testsToRun = new string[] { "TextAnalysis.SentencesParser_Tests", "TextAnalysis.FrequencyAnalysis_Tests", "TextAnalysis.TextGenerator_Tests", }; new AutoRun().Execute(new[] { "--stoponerror", // Останавливать после первого же непрошедшего теста. Закомментируйте, чтобы увидеть все падающие тесты "--noresult", "--test=" + string.Join(",", testsToRun) }); var text = File.ReadAllText("HarryPotterText.txt"); var sentences = SentencesParserTask.ParseSentences(text); var frequency = FrequencyAnalysisTask.GetMostFrequentNextWords(sentences); //Расскомментируйте этот блок, если хотите выполнить последнюю задачу до первых двух. /* * frequency = new Dictionary<string, string> * { * {"harry", "potter"}, * {"potter", "boy" }, * {"boy", "who" }, * {"who", "likes" }, * {"boy who", "survived" }, * {"survived", "attack" }, * {"he", "likes" }, * {"likes", "harry" }, * {"ron", "likes" }, * {"wizard", "harry" }, * }; */ while (true) { Console.Write("Введите первое слово (например, harry): "); var beginning = Console.ReadLine(); if (string.IsNullOrEmpty(beginning)) { return; } var phrase = TextGeneratorTask.ContinuePhrase(frequency, beginning.ToLower(), 10); Console.WriteLine(phrase); } }
public void CorrectlyParse_OneSentenceWithWordDelimiter( [Values('^', '#', '$', '-', '+', '1', '=', ' ', '\t', '\n', '\r')] char delimiter) { var text = "x" + delimiter + "y"; var expected = new List <List <string> > { new List <string> { "x", "y" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void IgnoreLeadingAndTrailingSpaces() { var text = " B . C"; var expected = new List <List <string> > { new List <string> { "b" }, new List <string> { "c" }, }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void ReturnResult_InLowerCase() { var text = "B. C. D"; var expected = new List <List <string> > { new List <string> { "b" }, new List <string> { "c" }, new List <string> { "d" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void ReturnCorrectResult_OnBigText() { var text = File.ReadAllText("HarryPotterText.txt"); var expected = new List <List <string> > { new List <string> { "the" }, new List <string> { "boy" }, new List <string> { "who" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public void CorrectlyParse_ShortSentence() { var text = "a b c d. b c d. e b c a d."; var expected = new List <List <string> > { new List <string> { "a", "b", "c", "d" }, new List <string> { "b", "c", "d" }, new List <string> { "e", "b", "c", "a", "d" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }
public static string ContinuePhrase(Dictionary <string, string> nextWords, string phraseBeginning, int wordsCount) { var words = SentencesParserTask.CreateSentense(phraseBeginning); var startLenght = words.Count; while (words.Count < startLenght + wordsCount) { var word = CreateNextWord(words, nextWords); if (word != "") { words.Add(word); } else { break; } } return(string.Join(" ", words)); }
public static void Main() { var text = File.ReadAllText("Text.txt"); Console.WriteLine("SentencesParserTask:"); var sentences = SentencesParserTask.ParseSentences(text); for (int i = 0; i < Math.Min(10, sentences.Count); i++) { Console.WriteLine(string.Join("|", sentences[i]) + "."); } Console.WriteLine(); Console.WriteLine("FrequencyAnalysisTask:"); var frequency = FrequencyAnalysisTask.GetMostFrequentNextWords(sentences); foreach (var keyValuePair in frequency.Take(10)) { Console.WriteLine($"{keyValuePair.Key}|{keyValuePair.Value}"); } Console.WriteLine(); Console.WriteLine("BigramGeneratorTask:"); foreach (var start in new[] { "harry", "he", "boy", "ron", "wizard" }) { var phrase = BigramGeneratorTask.ContinuePhraseWithBigramms(frequency, start, 5); Console.WriteLine(phrase); } Console.Read(); /* * Эту часть задания можете выполнить по желанию. * * Console.WriteLine(); * Console.WriteLine("TrigramGeneratorTask:"); * foreach (var start in new[] { "harry potter", "ron weasley", "hermione granger" }) * { * var phrase = TrigramGeneratorTask.ContinuePhraseWithTrigramms(frequency, start, 5); * Console.WriteLine(phrase); * } */ }
public void CorrectlyParse_SentenceDelimiters() { var text = "a.b!c?d:e;f(g)h;i"; var expected = new List <List <string> > { new List <string> { "a" }, new List <string> { "b" }, new List <string> { "c" }, new List <string> { "d" }, new List <string> { "e" }, new List <string> { "f" }, new List <string> { "g" }, new List <string> { "h" }, new List <string> { "i" } }; var actual = SentencesParserTask.ParseSentences(text); AssertAllSentencesEqual(expected, actual, text); }