/
katherinesiv
/
CSharp_Practice5
Обзор
Документация
Войти
/
katherinesiv
/
CSharp_Practice5
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
TextGeneratorTask.cs
36 строк
1 KB
Сиваева Екатерина
create TextGeneratorTask.cs
14 ноя 2025, 09:41
14 ноя 2025, 09:41
bf76687
Код
Авторство
О чём код?
namespace TextAnalysis; static class TextGeneratorTask { public static string ContinuePhrase( Dictionary<string, string> nextWords, string phraseBeginning, int wordsCount) { if (wordsCount == 0) return phraseBeginning; var words = phraseBeginning.Split(' ').ToList(); var continuePhrase = FindNextWord(nextWords, words); if (continuePhrase == null) return phraseBeginning; var currentPhrase = phraseBeginning + " " + continuePhrase; return ContinuePhrase(nextWords, currentPhrase, wordsCount - 1); } private static string FindNextWord(Dictionary<string, string> nextWords, List <string> words) { if (words.Count >= 2) { var key = words[words.Count - 2] + " " + words[words.Count - 1]; if (nextWords.ContainsKey(key)) return nextWords[key]; key = words[words.Count - 1]; if (nextWords.ContainsKey(key)) return nextWords[key]; } else if (words.Count == 1 ) { var key = words[words.Count - 1]; if (nextWords.ContainsKey(key)) return nextWords[key]; } return null; } }