-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGeneratorTask.cs
More file actions
63 lines (60 loc) · 1.85 KB
/
TextGeneratorTask.cs
File metadata and controls
63 lines (60 loc) · 1.85 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
60
61
62
63
using System;
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 list = new List<string>();
var begins = phraseBeginning.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (begins.Length == 0)
{
list.Add(phraseBeginning);
}
else list.AddRange(begins);
while (wordsCount > 0)
{
if (list.Count>1)
{
var key = list[list.Count - 2] + " " + list[list.Count-1];
if (nextWords.ContainsKey(key))
{
list.Add(nextWords[key]);
wordsCount--;
}
else
{
key = list[list.Count - 1];
if (nextWords.ContainsKey(key))
{
list.Add(nextWords[key]);
wordsCount--;
}
else break;
}
}
else
{
var key2 = list[list.Count - 1];
if (nextWords.ContainsKey(key2))
{
list.Add(nextWords[key2]);
wordsCount--;
}
else break;
}
}
var sb = new StringBuilder();
foreach (var word in list)
{
sb.Append(word + " ");
}
return sb.ToString().TrimEnd();
}
}
}