Skip to content

Commit

Permalink
Improve statistics page
Browse files Browse the repository at this point in the history
  • Loading branch information
kashparty committed Aug 2, 2021
1 parent faecf93 commit 5e614b1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ bin
obj
training.txt
model.txt
keyai.json
keyai.json
keyai_stats
60 changes: 40 additions & 20 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace KeyAI {
class Program {
static Preferences preferences;
static Statistics statistics;
const string versionNumber = "0.2.0";

static string[] menuOptions = { "Exit", "Typing tutor", "Statistics", "Help", "Reset progress" };
Expand Down Expand Up @@ -128,6 +129,13 @@ private static bool RunRound(QLearn qLearn) {
double accuracy = (double)(keyTimes.Count - mistakes) * 100.0 / keyTimes.Count;
Console.Write($" Accuracy: {Math.Round(accuracy)}".PadRight(17));

statistics.maxWPM = Math.Max(statistics.maxWPM, wordsPerMinute);
statistics.maxAccuracy = Math.Max(statistics.maxAccuracy, accuracy);
statistics.numLines++;
statistics.numChars += keyTimes.Count;
statistics.numMistakes += mistakes;
statistics.TryWrite();

qLearn.UpdateModel(keyTimes);
qLearn.FinishRound();
}
Expand Down Expand Up @@ -215,49 +223,61 @@ private static void ShowHelp() {
}

private static void ShowStatistics() {
if (!File.Exists("model.txt")) {
Console.WriteLine("No data to show yet. Come back after using the typing tutor.");
return;
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Lines completed: {statistics.numLines}");
Console.WriteLine($"Keys correctly typed: {statistics.numChars}");
Console.WriteLine($"Mistakes made: {statistics.numMistakes}");
Console.WriteLine($"Max WPM: {Math.Round(statistics.maxWPM, 2)}");
Console.WriteLine($"Max Accuracy: {Math.Round(statistics.maxAccuracy, 2)}");

using (StreamReader streamReader = new StreamReader("model.txt")) {
List<Tuple<string, double>> modelData = new List<Tuple<string, double>>();
string[] lines = streamReader.ReadToEnd().Split("\n");
if (File.Exists("model.txt")) {
using (StreamReader streamReader = new StreamReader("model.txt")) {
List<Tuple<string, double>> modelData = new List<Tuple<string, double>>();
string[] lines = streamReader.ReadToEnd().Split("\n");

foreach (string line in lines) {
if (line.Length <= 4) continue;
foreach (string line in lines) {
if (line.Length <= 4) continue;

string nGram = line.Substring(0, 3);
double qValue = double.Parse(line.Substring(4));
string nGram = line.Substring(0, 3);
double qValue = double.Parse(line.Substring(4));

modelData.Add(new Tuple<string, double>(nGram, qValue));
}
modelData.Add(new Tuple<string, double>(nGram, qValue));
}

modelData = modelData.OrderByDescending(d => d.Item2).ToList();
modelData = modelData.OrderByDescending(d => d.Item2).ToList();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("These are your slowest patterns:");
for (int i = 0; i < Math.Min(modelData.Count, 5); i++) {
Console.WriteLine($"{i + 1}. {modelData[i].Item1}");
Console.WriteLine("\nThese are your slowest patterns:");
for (int i = 0; i < Math.Min(modelData.Count, 5); i++) {
Console.WriteLine($"{i + 1}. {modelData[i].Item1}");
}
}
Console.ResetColor();
}

Console.ResetColor();
}

private static void ResetProgress() {
Console.Write("Are you sure you want to reset your progress? (Y/N): ");
Console.Write("Are you sure you want to DELETE all of your progress? (Y/N): ");

if (Console.ReadLine().ToUpper().Trim() == "Y") {
if (File.Exists("model.txt")) {
File.Delete("model.txt");
}
if (File.Exists("keyai_stats")) {
File.Delete("keyai_stats");
}

statistics = new Statistics();

Console.WriteLine("Progress has been reset.");
}
}

static void Main(string[] args) {
preferences = new Preferences("keyai.json");
statistics = new Statistics();
statistics.TryRead();

Console.WriteLine($"KeyAI {versionNumber}.");

bool done = false;
Expand Down
64 changes: 64 additions & 0 deletions Statistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace KeyAI {
[Serializable]
class Statistics {
public long numLines { get; set; }
public long numChars { get; set; }
public long numMistakes { get; set; }
public double maxWPM { get; set; }
public double maxAccuracy { get; set; }


public Statistics() {
numLines = 0;
numChars = 0;
numMistakes = 0;
maxWPM = 0;
maxAccuracy = 0;
}

public bool TryRead() {
bool success;

if (!File.Exists("keyai_stats")) return false;
using (FileStream fileStream = new FileStream("keyai_stats", FileMode.Open)) {
try {
BinaryFormatter binaryFormatter = new BinaryFormatter();
Statistics data = (Statistics)binaryFormatter.Deserialize(fileStream);

numLines = data.numLines;
numChars = data.numChars;
numMistakes = data.numMistakes;
maxWPM = data.maxWPM;
maxAccuracy = data.maxAccuracy;

success = true;
} catch (SerializationException e) {
Console.WriteLine(e.Message);
success = false;
}
}

return success;
}

public bool TryWrite() {
bool success;
using (FileStream fileStream = new FileStream("keyai_stats", FileMode.Create)) {
try {
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, this);
success = true;
} catch (SerializationException) {
success = false;
}
}

return success;
}
}
}

0 comments on commit 5e614b1

Please sign in to comment.