/
katherinesiv
/
CSharp_Practice5
Обзор
Документация
Войти
/
katherinesiv
/
CSharp_Practice5
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
FrequencyAnalysisTask.cs
84 строки
3 KB
Сиваева Екатерина
create FrequencyAnalysisTask.cs
14 ноя 2025, 09:41
14 ноя 2025, 09:41
97f5b73
Код
Авторство
О чём код?
namespace TextAnalysis; static class FrequencyAnalysisTask { public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text) { var countBigrams = new Dictionary<string, Dictionary<string, int>>(); var countTrigrams = new Dictionary<string, Dictionary<string, int>>(); foreach (var sentence in text) { if (sentence.Count < 2) continue; MakeBigrams(sentence, countBigrams); MakeTrigrams(sentence, countTrigrams); } var result = BuildFinalModel(countBigrams, countTrigrams); return result; } private static void MakeBigrams(List<string> sentence, Dictionary<string, Dictionary<string, int>> bigramCounts) { for (var i = 0; i < sentence.Count - 1; i++) { var firstWord = sentence[i]; var secondWord = sentence[i + 1]; if (!bigramCounts.ContainsKey(firstWord)) bigramCounts[firstWord] = new Dictionary<string, int>(); if (!bigramCounts[firstWord].ContainsKey(secondWord)) bigramCounts[firstWord][secondWord] = 0; bigramCounts[firstWord][secondWord]++; } } private static void MakeTrigrams(List<string> sentence, Dictionary<string, Dictionary<string, int>> trigramCounts) { for (var i = 0; i < sentence.Count - 2; i++) { var firstTwoWords = $"{sentence[i]} {sentence[i + 1]}"; var thirdWord = sentence[i + 2]; if (!trigramCounts.ContainsKey(firstTwoWords)) trigramCounts[firstTwoWords] = new Dictionary<string, int>(); if (!trigramCounts[firstTwoWords].ContainsKey(thirdWord)) trigramCounts[firstTwoWords][thirdWord] = 0; trigramCounts[firstTwoWords][thirdWord]++; } } private static Dictionary<string, string> BuildFinalModel( Dictionary<string, Dictionary<string, int>> bigramCounts, Dictionary<string, Dictionary<string, int>> trigramCounts) { var result = new Dictionary<string, string>(); foreach (var bigram in bigramCounts) result[bigram.Key] = FindMostFrequentNextWord(bigram.Value); foreach (var trigram in trigramCounts) result[trigram.Key] = FindMostFrequentNextWord(trigram.Value); return result; } private static string FindMostFrequentNextWord(Dictionary<string, int> nextWordCounts) { var maxFrequency = nextWordCounts.Values.Max(); var mostFrequentWords = nextWordCounts .Where(pair => pair.Value == maxFrequency) .Select(pair => pair.Key) .ToList(); if (mostFrequentWords.Count == 1) return mostFrequentWords[0]; else return mostFrequentWords.OrderBy(word => word, StringComparer.Ordinal).First(); } }