-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGeneratorTask.cs
More file actions
59 lines (55 loc) · 1.98 KB
/
TextGeneratorTask.cs
File metadata and controls
59 lines (55 loc) · 1.98 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections.Generic;
using System.Text;
namespace TextAnalysis
{
static class TextGeneratorTask
{
public static string ContinuePhrase(Dictionary<string, string> nextWords, string phraseBeginning, int wordsCount)
{
var builder = new StringBuilder();
var lastWords = new List<string>();
if (phraseBeginning.Contains(" "))
{
var words = phraseBeginning.Split(' ');
lastWords.Add(words[words.Length - 2]);
lastWords.Add(words[words.Length - 1]);
builder.Append(phraseBeginning);
}
else
{
lastWords.Add(phraseBeginning);
builder.Append(phraseBeginning);
}
for (var i = 0; i < wordsCount; i++)
{
if (lastWords.Count > 1)
{
var key = $"{lastWords[0]} {lastWords[1]}";
if (nextWords.ContainsKey(key))
{
builder.Append($" {nextWords[key]}");
lastWords.Add(nextWords[key]);
lastWords.RemoveAt(0);
}
else if (nextWords.ContainsKey(lastWords[1]))
{
builder.Append($" {nextWords[lastWords[1]]}");
lastWords.Add(nextWords[lastWords[1]]);
lastWords.RemoveAt(0);
}
else return builder.ToString();
}
else
{
if (nextWords.ContainsKey(lastWords[0]))
{
builder.Append($" {nextWords[lastWords[0]]}");
lastWords.Add(nextWords[lastWords[0]]);
}
else return builder.ToString();
}
}
return builder.ToString();
}
}
}