Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace TextAnalysis
- {
- static class TextGeneratorTask
- {
- public static string ContinuePhrase(
- Dictionary<string, string> nextWords,
- string phraseBeginning,
- int wordsCount)
- {
- if (wordsCount == 0) return phraseBeginning;
- int counter = 0;
- string firstWord = "";
- string secondWord = "";
- for (int i = phraseBeginning.Length - 1; i >= 0; i--)
- {
- if (phraseBeginning[i] == ' ')
- {
- counter++;
- continue;
- }
- if (counter == 2) break;
- if (counter == 0) firstWord = phraseBeginning[i] + firstWord;
- if (counter == 1) secondWord = phraseBeginning[i] + secondWord;
- }
- for (int i = 0; i < wordsCount; i++)
- {
- if (nextWords.ContainsKey(secondWord + " " + firstWord))
- {
- string buf = nextWords[secondWord + " " + firstWord];
- secondWord = firstWord;
- firstWord = buf;
- phraseBeginning += " " + buf;
- }
- else if (nextWords.ContainsKey(firstWord))
- {
- secondWord = firstWord;
- firstWord = nextWords[firstWord];
- phraseBeginning += " " + firstWord;
- }
- else break;
- }
- return phraseBeginning;
- }
- }
- }
Add Comment
Please, Sign In to add comment