-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preferences.cs
131 lines (110 loc) · 4.83 KB
/
Preferences.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
namespace KeyAI {
class Preferences {
public string trainingFileUrl { get; set; }
public string trainingFilePath { get; set; }
public int lineLength { get; set; }
public double explorationHigh { get; set; }
public double explorationLow { get; set; }
public int numRounds { get; set; }
public double learningRate { get; set; }
public double discount { get; set; }
public ConsoleColor color { get; set; }
public bool includeUppercase { get; set; }
public bool includeLowercase { get; set; }
public bool includeDigits { get; set; }
public bool includePunctuation { get; set; }
public bool allowSkip { get; set; }
public int skipLength { get; set; }
public Preferences() {
// Use the default preferences.
trainingFileUrl = "https://www.gutenberg.org/files/11/11-0.txt";
trainingFilePath = "training.txt";
lineLength = 80;
explorationHigh = 1.0;
explorationLow = 0.2;
numRounds = 10;
learningRate = 0.5;
discount = 0.9;
color = ConsoleColor.Green;
includeUppercase = true;
includeLowercase = true;
includeDigits = false;
includePunctuation = false;
allowSkip = true;
skipLength = 3;
}
public Preferences(string fileName) : this() {
if (!File.Exists(fileName)) return;
using (StreamReader streamReader = new StreamReader(fileName)) {
string fileString = streamReader.ReadToEnd();
Dictionary<string, dynamic> data;
try {
data = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(fileString);
} catch (JsonException) {
return;
}
if (data.ContainsKey("trainingFileUrl")) {
trainingFileUrl = data["trainingFileUrl"].GetString();
}
if (data.ContainsKey("trainingFilePath")) {
trainingFilePath = data["trainingFilePath"].GetString();
}
if (data.ContainsKey("lineLength")) {
lineLength = data["lineLength"].GetInt32();
}
if (data.ContainsKey("explorationHigh")) {
explorationHigh = data["explorationHigh"].GetDouble();
}
if (data.ContainsKey("explorationLow")) {
explorationLow = data["explorationLow"].GetDouble();
}
if (data.ContainsKey("numRounds")) {
numRounds = data["numRounds"].GetInt32();
}
if (data.ContainsKey("learningRate")) {
learningRate = data["learningRate"].GetDouble();
}
if (data.ContainsKey("discount")) {
discount = data["discount"].GetDouble();
}
if (data.ContainsKey("color")) {
object chosenColor;
if (Enum.TryParse(typeof(ConsoleColor), data["color"].GetString(), out chosenColor)) {
color = (ConsoleColor)chosenColor;
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{data["color"].GetString()} is not an available color.");
Console.ResetColor();
}
}
if (data.ContainsKey("includeUppercase")) {
includeUppercase = data["includeUppercase"].GetBoolean();
}
if (data.ContainsKey("includeLowercase")) {
includeLowercase = data["includeLowercase"].GetBoolean();
}
if (data.ContainsKey("includeDigits")) {
includeDigits = data["includeDigits"].GetBoolean();
}
if (data.ContainsKey("includePunctuation")) {
includePunctuation = data["includePunctuation"].GetBoolean();
}
if (data.ContainsKey("allowSkip")) {
allowSkip = data["allowSkip"].GetBoolean();
}
if (data.ContainsKey("skipLength")) {
skipLength = data["skipLength"].GetInt32();
}
}
if (!includeLowercase && !includeUppercase && !includeDigits && !includePunctuation) {
Console.WriteLine("You have included no characters. Defaulting to lowercase and uppercase characters.");
includeLowercase = true;
includeUppercase = true;
}
}
}
}