Skip to content

Commit

Permalink
Add preferences for included character types
Browse files Browse the repository at this point in the history
  • Loading branch information
kashparty committed Jul 26, 2021
1 parent db9323e commit eed8b10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
25 changes: 25 additions & 0 deletions Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Preferences {
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 Preferences() {
// Use the default preferences.
Expand All @@ -25,7 +29,12 @@ public Preferences() {
numRounds = 10;
learningRate = 0.5;
discount = 0.9;

color = ConsoleColor.Green;
includeUppercase = true;
includeLowercase = true;
includeDigits = false;
includePunctuation = false;
}

public Preferences(string fileName) : this() {
Expand Down Expand Up @@ -77,6 +86,22 @@ public Preferences(string fileName) : this() {
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();
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ private static string PreProcess() {
trainingData = streamReader.ReadToEnd();
}

trainingData = Regex.Replace(trainingData, "[^a-zA-Z ]", "");
string regexString = "[^";
if (preferences.includeLowercase) regexString += "a-z";
if (preferences.includeUppercase) regexString += "A-Z";
if (preferences.includeDigits) regexString += "0-9";
if (preferences.includePunctuation) regexString += ".,!:;\"\'-()";
regexString += " ]";
trainingData = Regex.Replace(trainingData, regexString, "");
return trainingData;
}

Expand Down
10 changes: 7 additions & 3 deletions QLearn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class QLearn {
double discount;

static Random random = new Random();

Dictionary<char, int> charToInt;
Dictionary<int, char> intToChar;

Expand Down Expand Up @@ -53,7 +53,9 @@ public QLearn(string trainingData, double explorationLow, double explorationHigh
for (int i = 0; i < trainingData.Length; i++) {
if (i >= 3) current = current.Substring(1);
if (current.Length == 2) {
occurrences[charToInt[current[0]], charToInt[current[1]], charToInt[trainingData[i]]]++;
if (charToInt.ContainsKey(current[0]) && charToInt.ContainsKey(current[1]) && charToInt.ContainsKey(trainingData[i])) {
occurrences[charToInt[current[0]], charToInt[current[1]], charToInt[trainingData[i]]]++;
}
}
current += trainingData[i];
}
Expand All @@ -72,6 +74,8 @@ private void LoadModelIfExists() {
string nGram = line.Substring(0, 3);
double qValue = double.Parse(line.Substring(4));

if (!charToInt.ContainsKey(nGram[0]) || !charToInt.ContainsKey(nGram[1]) || !charToInt.ContainsKey(nGram[2])) continue;

int charIndex1 = charToInt[nGram[0]];
int charIndex2 = charToInt[nGram[1]];
int charIndex3 = charToInt[nGram[2]];
Expand Down Expand Up @@ -181,7 +185,7 @@ public void FinishRound() {
currentRound++;

if (currentRound >= numRounds) currentRound = 0;

double roundCompletion = (double)currentRound / (double)numRounds;
explorationRate = roundCompletion * explorationLow + (1 - roundCompletion) * explorationHigh;

Expand Down

0 comments on commit eed8b10

Please sign in to comment.