-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessonGenerator.cs
More file actions
235 lines (204 loc) · 9.6 KB
/
Copy pathLessonGenerator.cs
File metadata and controls
235 lines (204 loc) · 9.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static Pythonic.ListHelpers;
namespace KeyTrain
{
/// <summary>
/// Abstract class for generating keyboard lessons
/// </summary>
public abstract class LessonGenerator
{
protected static int defaultLessonLength => ConfigManager.lessonLength;
public abstract string CurrentText { get; }
public abstract string NextText();
public abstract HashSet<char> alphabet { get; protected set; }
public DefaultDict<char, int> characterFrequencyCount { get; protected set; }
}
/// <summary>
/// Keyboard lessons with predefined text, split into chunks
/// </summary>
class PresetTextLesson : LessonGenerator
{
static List<string> queuedTexts;
static int currTextid = 0;
public override string CurrentText => queuedTexts[currTextid % queuedTexts.Count].Trim();
public override HashSet<Char> alphabet{ get; protected set;}
public PresetTextLesson(string text, int maxLength = 0)
{
if (maxLength == 0) maxLength = defaultLessonLength;
text = text.Trim();
alphabet = text.ToUpper().ToHashSet();
characterFrequencyCount = new DefaultDict<char, int>();
foreach (char c in text.ToUpper())
{
characterFrequencyCount[c] += 1;
}
queuedTexts = new List<string>();
//TODO:
//Divide up the text into about to equal chunks, each of which is smaller than maxLength
int start = 0;
int remaining = text.Length;
do
{
double chunks = Math.Ceiling((double)remaining / maxLength);
int targetLength = (int)Math.Ceiling(remaining / chunks);
int chunklength = text.Substring(start, targetLength).LastIndexOf(" ");
if(chunklength == - 1) //word is too long; chop it up
{
chunklength = start + maxLength;
}
// .Substring complains about going over the length
queuedTexts.Add(text.Substring(start, Math.Min( chunklength, remaining)));
start += chunklength + 1;
remaining = text.Length - start;
} while (remaining > 0);
}
public override string NextText()
{
currTextid++;
return CurrentText;
}
public void Restart()
{
currTextid = 0;
}
}
/// <summary>
/// Keyboard lessons with randomly generated text, emphasizing certain keys as you need
/// </summary>
class RandomizedLesson : LessonGenerator
{
private string text;
private List<string> dict;
private List<string> shuffled;
private int place = 0;
private int chunkLength;
private List<char> punctuation = new List<char>();
Random random;
public static int seed = 0; //different on startup
public static int offset = 0; //keep from skipping back and forth abusing the shuffle button
Random NextRandom() => new Random(seed + MainPage.stats.WPMLOG.Count + offset); //
const int minSampleSize = 10; //minimum amount of words deemed enough to pick from and seem random
int capitals => ConfigManager.Settings["capitalsLevel"];
/// <summary>
/// Cleans dictionary of unwanted elements eg. control characters, too short or too long words
/// </summary>
/// <param name="dirty"></param>
/// <returns></returns>
private List<string> Sanitize(IEnumerable<string> dirty)
{
return dirty.Where(w => w.Length > 2).ToList();
}
public override string CurrentText => text.Trim();
public override string NextText()
{
text = "";
while (true)
{
string nextWord = shuffled[++place % shuffled.Count];
switch (capitals) //0: force lower, 1: keep existing, 2: 50% first letter 3: first letter, 4: all caps
{
default:
break;
case 0:
nextWord = nextWord.ToLower();
break;
case 2:
if(random.NextDouble() > 0.5)
nextWord = nextWord.Substring(0, 1).ToUpper() + nextWord.Substring(1);
break;
case 3:
nextWord = nextWord.Substring(0, 1).ToUpper() + nextWord.Substring(1);
break;
case 4:
nextWord = nextWord.ToUpper();
break;
}
if(text == "")
{
text = nextWord;
}
else if (text.Length + nextWord.Length + 3 < chunkLength)
{
char pct = punctuation.ElementAtOrDefault(random.Next(punctuation.Count + 1)); //+1 causes invalid indexes so we still generate just spaces some of the time
string sep = " ";
if (pct != '\0' && pct != text.Last())
{
sep = pct == '-' ? pct.ToString() : pct + " ";
}
text = string.Join(sep, text, nextWord);
}
else
{
break;
}
}
text = text.Trim();
return text;
}
/// <summary>
/// The set of characters that the dictionary uses more than 50 times
/// </summary>
public override HashSet<char> alphabet { get; protected set; }
/// <summary>
/// Sets the options to only contain words that contain all given characters, or if that's not enough, any of the given characters.
/// Emphasizing punctuation characters means they will be randomly inserted between words by NextText().
/// Emphasizing whitespace means the generator will favor shorter words
/// </summary>
/// <param name="emphasized">Set of words to emphasize</param>
public void Emphasize(string emphasized, int minSampleSize = minSampleSize)
{
var normal = emphasized.Where(c => char.IsLetterOrDigit(c));
var options = ConcatToList<IEnumerable<string>>(
dict.Where(word => normal.All(e => word.ToUpper().Contains(e))),
dict.Where(word => normal.Any(e => word.ToUpper().Contains(e))),
dict ).OrderBy(d => d.Count()).First(d => d.Count() > minSampleSize).ToList();
punctuation = emphasized.Where(c => char.IsPunctuation(c)).ToList();
if(emphasized.Any(c => char.IsWhiteSpace(c)))
{
options = options.OrderBy(w => w.Length).Take(Math.Max(options.Count / 100, 150)).ToList();
}
random = NextRandom();
place = 0;
shuffled = options.OrderBy(x => random.Next()).ToList();
}
/// <param name="dictonary">List of words the generator can use</param>
/// <param name="maxLength">Exclusive maximum length of each generated text chunk</param>
public RandomizedLesson(IEnumerable<string> dictonary, int maxLength = 0)
{
if (maxLength == 0) maxLength = defaultLessonLength;
dict = Sanitize(dictonary);
chunkLength = maxLength;
random = NextRandom();
place = 0;
shuffled = dict.OrderBy(x=> random.Next()).ToList();
//all characters which appear at least 50 times in the dictionary
alphabet = dict.SelectMany(x => x.ToUpper()).GroupBy(x => x).Where(x => x.Count() > 50).SelectMany(x => x).ToHashSet();
characterFrequencyCount = new DefaultDict<char, int>();
foreach (char c in dictonary.SelectMany(w => w.ToUpper().ToCharArray()))
{
characterFrequencyCount[c] += 1;
}
NextText();
}
/// <param name="dictionaryFile">Path to text file that contains all the words the generator can use (1 word/phrase per line)</param>
/// <param name="maxlength">Exclusive maximum length of each generated text chunk</param>
public RandomizedLesson(string dictionaryFile = "Resources/dictionaryEN.txt", int maxlength = 0) :
this(File.ReadAllLines(dictionaryFile).ToList(), maxlength) {}
/// <param name="dictionaryFiles">List of text files that contain all the words the generator can use (1 word/phrase per line)</param>
/// <param name="maxlength">Exclusive maximum length of each generated text chunk</param>
///
public static RandomizedLesson FromDictionaryFiles(IEnumerable<string> dictionaryFiles, int maxlength = 0)
{
if (maxlength == 0) maxlength = defaultLessonLength;
return new RandomizedLesson(dictionaryFiles.SelectMany(f => File.ReadAllLines(f)).ToList(), maxlength);
}
public static RandomizedLesson FromDictionaryFiles(string dictionaryFile, int maxlength = 0)
{
if (maxlength == 0) maxlength = defaultLessonLength;
return new RandomizedLesson(File.ReadAllLines(dictionaryFile).ToList(), maxlength);
}
}
}