Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Text;
- namespace TextAnalysis
- {
- static class TextGeneratorTask
- {
- public static void NextWord(Dictionary<string, string> dictionary, string startGram, StringBuilder sb)
- {
- foreach (var word in dictionary)
- {
- if (word.Key != startGram) continue;
- sb.Append(" " + word.Value);
- break;
- }
- }
- public static string ContinuePhrase(Dictionary<string, string> nextWords, string phraseBeginning, int wordsCount)
- {
- var str = ""; var flag = true;
- var stringBuilder = new StringBuilder($"{phraseBeginning}");
- var words = phraseBeginning.Split(' ');
- for (var i = 0; i < wordsCount; i++)
- {
- if (words.Length >= 2)
- {
- str = words[words.Length - 2] + " " + words[words.Length - 1];
- if (nextWords.ContainsKey(str))
- {
- flag = false;
- NextWord(nextWords, str, stringBuilder);
- }
- }
- if (flag)
- {
- str = words[words.Length - 1];
- NextWord(nextWords, str, stringBuilder);
- }
- words = stringBuilder.ToString().Split(' ');
- }
- phraseBeginning = stringBuilder.ToString();
- return phraseBeginning;
- }
- }
- }
Add Comment
Please, Sign In to add comment