-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGenerator.cs
More file actions
34 lines (29 loc) · 1.09 KB
/
TextGenerator.cs
File metadata and controls
34 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Collections.Generic;
using System.Text;
namespace TextAnalysis
{
static class TextGenerator
{
public static string ContinuePhrase(
Dictionary<string, string> nextWords,
string phraseBeginning,
int wordsCount)
{
var result = new StringBuilder(phraseBeginning);
for (var word = wordsCount; word > 0; word--)
{
var phrase = result.ToString().Split(' ');
var bigram = phrase[phrase.Length - 1];
var trigram = "";
if (phrase.Length > 1)
trigram = phrase[phrase.Length - 2] + " " + bigram;
if (phrase.Length > 1 && nextWords.ContainsKey(trigram))
result.Append(" " + nextWords[trigram]);
else if (nextWords.ContainsKey(bigram))
result.Append(" " + nextWords[bigram]);
else return result.ToString();
}
return result.ToString();
}
}
}