Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3.  
  4. namespace TextAnalysis
  5. {
  6.     static class FrequencyAnalysisTask
  7.     {
  8.         public static Dictionary<string, string> GetMostFrequentNextWords(List<List<string>> text)
  9.         {
  10.             var result = new Dictionary<string, string>();
  11.             var bigramms = new Dictionary<string, int>();
  12.             var trigramms = new Dictionary<string, int>();
  13.             bool findBigramm = true;
  14.             bool findTrigramm = true;
  15.             for (var currentSentenceCounter = 0; currentSentenceCounter < text.Capacity; currentSentenceCounter++)
  16.             {
  17.                 for (var currentWordCounter = 0; currentWordCounter < text.Capacity - 1; currentWordCounter++)
  18.                 {
  19.                     findBigramm = true;
  20.                     findTrigramm = true;
  21.                     var bigramm = text[currentSentenceCounter][currentWordCounter] + ' ' + text[currentSentenceCounter][currentWordCounter + 1];
  22.                     string trigramm = null;
  23.                     if (currentWordCounter < text.Capacity - 2)
  24.                     {
  25.                         trigramm = text[currentSentenceCounter][currentWordCounter] + ' ' +
  26.                                    text[currentSentenceCounter][currentWordCounter + 1] + ' ' +
  27.                                    text[currentSentenceCounter][currentWordCounter + 2];
  28.                     }
  29.                     if (bigramms.ContainsKey(bigramm))
  30.                     {
  31.                         findBigramm = false;
  32.                     }
  33.                     if (trigramms.ContainsKey(trigramm))
  34.                     {
  35.                         findTrigramm = false;
  36.                     }
  37.                     if (findBigramm || findTrigramm)
  38.                     {
  39.                         var tempWordCounter = currentWordCounter;
  40.                         for (var sentenceCounter = currentSentenceCounter;
  41.                             sentenceCounter < text.Capacity;
  42.                             sentenceCounter++)
  43.                         {
  44.                             for (var wordCounter = tempWordCounter; wordCounter < text.Capacity - 1; wordCounter++)
  45.                             {
  46.                                 if ((text[sentenceCounter][wordCounter] + ' ' + text[sentenceCounter][wordCounter + 1])
  47.                                     .Equals(bigramm))
  48.                                 {
  49.                                     if (!bigramms.ContainsKey(bigramm)) bigramms[bigramm] = 0;
  50.                                     bigramms[bigramm]++;
  51.                                 }
  52.  
  53.                                 if ((text[currentSentenceCounter][currentWordCounter] + ' ' +
  54.                                      text[currentSentenceCounter][currentWordCounter + 1] + ' ' +
  55.                                      text[currentSentenceCounter][currentWordCounter + 2]).Equals(trigramm))
  56.                                 {
  57.                                     if (!trigramms.ContainsKey(trigramm)) trigramms[trigramm] = 0;
  58.                                     trigramms[trigramm]++;
  59.                                 }
  60.                             }
  61.                             tempWordCounter = 0;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             return result;
  67.         }
  68.    }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement