Last active
October 25, 2017 16:05
-
-
Save JakubMifek/837b9620d6877b5d2d1b1cd2ba65bf9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace MyProject | |
{ | |
class Program | |
{ | |
private static readonly string[] Set = { "for ", "loop ", "while", "begin", "in ", "end " }; | |
private static readonly string[] Alphabet = | |
{ | |
"0 0 0 0 0 0 0", // space | |
"0 0 0 0 0 0 1", // A | |
"0 0 0 0 0 1 1", // B | |
"0 0 0 0 1 0 1", // C | |
"0 0 0 0 1 1 1", // D | |
"0 0 0 1 0 0 1", // E | |
"0 0 0 1 0 1 1", // F | |
"0 0 0 1 1 0 1", // G | |
"0 0 0 1 1 1 1", // H | |
"0 0 1 0 0 0 1", // I | |
"0 0 1 0 0 1 1", // J | |
"0 0 1 0 1 0 1", // K | |
"0 0 1 0 1 1 1", // L | |
"0 0 1 1 0 0 1", // M | |
"0 0 1 1 0 1 1", // N | |
"0 0 1 1 1 0 1", // O | |
"0 0 1 1 1 1 1", // P | |
"0 1 0 0 0 0 1", // Q | |
"0 1 0 0 0 1 1", // R | |
"0 1 0 0 1 0 1", // S | |
"0 1 0 0 1 1 1", // T | |
"0 1 0 1 0 0 1", // U | |
"0 1 0 1 0 1 1", // V | |
"0 1 0 1 1 0 1", // W | |
"0 1 0 1 1 1 1", // X | |
"0 1 1 0 0 0 1", // Y | |
"0 1 1 0 0 1 1", // Z | |
"0 1 1 0 1 0 1", // [ | |
"0 1 1 0 1 1 1", // \ | |
"0 1 1 1 0 0 1", // ] | |
"0 1 1 1 0 1 1", // ^ | |
"0 1 1 1 1 0 1", // _ | |
"0 1 1 1 1 1 1", // ` | |
"1 0 0 0 0 0 1", // a | |
"1 0 0 0 0 1 1", // b | |
"1 0 0 0 1 0 1", // c | |
"1 0 0 0 1 1 1", // d | |
"1 0 0 1 0 0 1", // e | |
"1 0 0 1 0 1 1", // f | |
"1 0 0 1 1 0 1", // g | |
"1 0 0 1 1 1 1", // h | |
"1 0 1 0 0 0 1", // i | |
"1 0 1 0 0 1 1", // j | |
"1 0 1 0 1 0 1", // k | |
"1 0 1 0 1 1 1", // l | |
"1 0 1 1 0 0 1", // m | |
"1 0 1 1 0 1 1", // n | |
"1 0 1 1 1 0 1", // o | |
"1 0 1 1 1 1 1", // p | |
"1 1 0 0 0 0 1", // q | |
"1 1 0 0 0 1 1", // r | |
"1 1 0 0 1 0 1", // s | |
"1 1 0 0 1 1 1", // t | |
"1 1 0 1 0 0 1", // u | |
"1 1 0 1 0 1 1", // v | |
"1 1 0 1 1 0 1", // w | |
"1 1 0 1 1 1 1", // x | |
"1 1 1 0 0 0 1", // y | |
"1 1 1 0 0 1 1", // z | |
}; | |
private static readonly string[] Category = | |
{ | |
"0 0 0 0 0 0", // None | |
"0 0 0 0 0 1", // for | |
"0 0 0 0 1 0", // loop | |
"0 0 0 1 0 0", // while | |
"0 0 1 0 0 0", // begin | |
"0 1 0 0 0 0", // in | |
"1 0 0 0 0 0" // end | |
}; | |
private const int Iterations = 100000; | |
private const int Threshold = 30; | |
/* | |
/// <summary> | |
/// Generator | |
/// </summary> | |
/// <param name="args"></param> | |
private static void Main(string[] args) | |
{ | |
using (File.CreateText("nn_output.txt")) { } | |
using (var expectedOutput = File.CreateText("expected_output.txt")) | |
{ | |
using (var input = File.CreateText("nn_input.txt")) | |
{ | |
var rand = new Random(); | |
for (var i = 0; i < Iterations; i++) | |
{ | |
var x = rand.Next(100); | |
if (x >= Threshold) | |
{ | |
var index = rand.Next(Set.Length); | |
var word = Set[index]; | |
EncodeAsync(input, word).Wait(); | |
expectedOutput.WriteLineAsync(Category[index + 1]).Wait(); | |
} | |
else | |
{ | |
var list = new List<int> { 0, 1, 2, 3, 4 }; | |
var noise = rand.Next(5) + 1; | |
var word = Set[rand.Next(Set.Length)].ToCharArray(); | |
while (noise-- > 0) | |
{ | |
var index = list[rand.Next(list.Count)]; | |
list.Remove(index); | |
word[index] = (char)(rand.Next('z' - 'A') + 'A'); | |
} | |
EncodeAsync(input, new string(word)).Wait(); | |
expectedOutput.WriteLineAsync(Category[0]).Wait(); | |
} | |
} | |
} | |
} | |
} | |
private static async Task EncodeAsync(TextWriter input, string word) | |
{ | |
foreach (var c in word) | |
await input.WriteAsync($"{(c == ' ' ? Alphabet[0] : Alphabet[c - 'A' + 1])} "); | |
await input.WriteLineAsync(); | |
} | |
*/ | |
/// <summary> | |
/// Evaluator | |
/// </summary> | |
/// <param name="args"></param> | |
private static void Main(string[] args) | |
{ | |
using (var reader1 = File.OpenText("expected_output.txt")) | |
{ | |
using (var reader2 = File.OpenText("nn_output.txt")) | |
{ | |
string line1, line2; | |
int count = 0, match = 0, outOfCategory = 0, outOfCategoryMatch = 0; | |
while ((line1 = reader1.ReadLine()) != null && (line2 = reader2.ReadLine()) != null) | |
{ | |
if (line1.Trim().Equals("0 0 0 0 0 0")) | |
{ | |
outOfCategory++; | |
if (line1.Trim().Equals(line2.Trim())) | |
{ | |
match++; | |
outOfCategoryMatch++; | |
} | |
} | |
else | |
{ | |
if (line1.Trim().Equals(line2.Trim())) | |
match++; | |
} | |
count++; | |
} | |
double ratio = match * 100 / (double)count; | |
Console.WriteLine($"NN matched {ratio}% of examples."); | |
Console.WriteLine($"From {count} tests was {outOfCategory} damaged."); | |
Console.WriteLine($"From {outOfCategory} damaged was {outOfCategoryMatch * 100 / (double)outOfCategory}% matched."); | |
Console.WriteLine($"From {count - outOfCategory} categorizable was {(match - outOfCategoryMatch) * 100 / (double)(count - outOfCategory)}% matched."); | |
} | |
} | |
} | |
/* | |
/// <summary> | |
/// Decoder | |
/// </summary> | |
/// <param name="args"></param> | |
private static void Main(string[] args) | |
{ | |
using (var writer = File.CreateText("tmp.txt")) | |
{ | |
using (var reader = File.OpenText("nn_output.txt")) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
var parts = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); | |
foreach (var c in parts) | |
{ | |
var d = Math.Round(double.Parse(c)); | |
writer.Write($"{d} "); | |
} | |
writer.WriteLine(); | |
} | |
} | |
} | |
File.Replace("tmp.txt", "nn_output.txt", "nn_output2.txt"); | |
File.Delete("tmp.txt"); | |
} | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment