nekotrap

Dic

Oct 20th, 2020 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Text;
  3.  
  4. namespace TextAnalysis
  5. {
  6.     static class TextGeneratorTask
  7.     {
  8.         public static void NextWord(Dictionary<string, string> dictionary, string startGram, StringBuilder sb)
  9.         {
  10.             foreach (var word in dictionary)
  11.             {
  12.                 if (word.Key != startGram) continue;
  13.                 sb.Append(" " + word.Value);
  14.                 break;
  15.             }
  16.         }
  17.        
  18.         public static string ContinuePhrase(Dictionary<string, string> nextWords, string phraseBeginning, int wordsCount)
  19.         {
  20.             var str = ""; var flag = true;
  21.             var stringBuilder = new StringBuilder($"{phraseBeginning}");
  22.             var words = phraseBeginning.Split(' ');
  23.             for (var i = 0; i < wordsCount; i++)
  24.             {
  25.                 if (words.Length >= 2)
  26.                 {
  27.                     str = words[words.Length - 2] + " " + words[words.Length - 1];
  28.                     if (nextWords.ContainsKey(str))
  29.                     {
  30.                         flag = false;
  31.                         NextWord(nextWords, str, stringBuilder);
  32.                     }
  33.                 }
  34.                 if (flag)
  35.                 {
  36.                     str = words[words.Length - 1];
  37.                     NextWord(nextWords, str, stringBuilder);
  38.                 }
  39.                 words = stringBuilder.ToString().Split(' ');
  40.             }
  41.             phraseBeginning = stringBuilder.ToString();
  42.             return phraseBeginning;
  43.         }
  44.     }
  45. }
Add Comment
Please, Sign In to add comment