Skip to content

Instantly share code, notes, and snippets.

@hawkerfun
Created April 18, 2018 13:19
Show Gist options
  • Save hawkerfun/49ff2e85c1d26b0aeef12e4e1220820a to your computer and use it in GitHub Desktop.
Save hawkerfun/49ff2e85c1d26b0aeef12e4e1220820a to your computer and use it in GitHub Desktop.
PredictX created by hawkerfun1 - https://repl.it/@hawkerfun1/PredictX
class LetterModel {
constructor() {
this._adj_step = 0.001;
this._x = 5;
this._y = 5;
this._tries = 3000;
this._models = [];
}
_createPlayGround(x,y) {
let playground = [];
for (let i=0; i < x; i++) {
let vector =[];
for (let j=0; j < y; j++) {
vector.push(0);
}
playground.push(vector);
}
return playground;
}
_getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
_generateRandomCase(){
//let version = JSON.parse(JSON.stringify(this.playground)); //deep copy array
let version = this._createPlayGround(this._x, this._y);
for(let i = 0; i < (this._x * this._y); i++) {
let a = this._getRndInteger(0,this._x);
let b = this._getRndInteger(0,this._y);
version[a][b] = 1;
}
return version;
}
_adjustWeights(sample, weights, caseMatrix) {
for (let i = 0; i < this._x; i++) {
for (let j = 0; j < this._y; j++) {
if (caseMatrix[i][j] && sample[i][j] && weights[i][j] < 1) {
weights[i][j] += this._adj_step;
}
}
}
return false;
}
_learn(sample, weights) {
for (let i=0; i < this._tries; i++) {
let inputRandomCaseses = this._generateRandomCase();
this._adjustWeights(sample, weights, inputRandomCaseses);
}
}
_getCountPixels(letter_mx) {
let count = 0;
letter_mx.forEach(vector => {
vector.forEach(pixel => {
count += pixel ? 1 : 0;
})
})
return count;
}
addNewModel(sample,lable) {
const model = {
"lable": lable,
"weights": this._createPlayGround(this._x, this._y),
"pixels_count": this._getCountPixels(sample)
}
this._learn(sample, model.weights);
this._models.push(model);
}
printModel(lable) {
return this._models.find(o => {
if(o.lable === lable)
return true;
})
}
predictLetter(example) {
const statistic = [];
let max_prediction = 0;
let letter = '';
this._models.forEach(model =>{
let weights = model.weights;
let p_count = model.pixels_count;
let m_weight = 0;
for (let i = 0; i < this._x; i++) {
for (let j = 0; j < this._y; j++) {
if(example[i][j] && weights[i][j]){
m_weight += weights[i][j];
}
}
}
statistic[model.lable] = m_weight/p_count;
if (max_prediction < statistic[model.lable]) {
max_prediction = statistic[model.lable];
letter = model.lable;
}
});
console.log(statistic);
return letter;
}
}
const modelSage = new LetterModel();
const p = [
[1,1,1,1,0],
[1,0,0,1,1],
[1,1,1,1,0],
[1,0,0,0,0],
[1,0,0,0,0]
];
modelSage.addNewModel(p, 'p');
const x = [
[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]
];
modelSage.addNewModel(x, 'x');
const o = [
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0]
];
modelSage.addNewModel(o, 'o');
const q = [
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,1]
];
modelSage.addNewModel(q, 'q');
const a = [
[0,0,1,0,0],
[0,1,0,1,0],
[0,1,1,1,0],
[1,1,0,1,1],
[1,0,1,0,1]
];
modelSage.addNewModel(a, 'a');
const t = [
[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0]
];
modelSage.addNewModel(t, 't');
const one = [
[0,1,0,0,0],
[1,1,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0]
];
modelSage.addNewModel(one, 'one');
const two = [
[0,1,1,0,0],
[1,0,0,1,1],
[0,1,1,1,0],
[1,1,0,0,0],
[1,1,1,1,1]
];
modelSage.addNewModel(two, 'two');
const inputCase = [
[0,0,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
];
console.log(modelSage.predictLetter(inputCase));
const inputCaseA = [
[0,0,0,0,0],
[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0]
];
/*const inputCaseA = [
[' ',' ','*',' ',' '],
[' ','*','*','*',' '],
[' ','*',' ','*',' '],
['*','*','*','*','*'],
['*',' ',' ',' ','*']
];*/
console.log(modelSage.predictLetter(inputCaseA));
//console.log(modelSage.printModel('q'));
//console.log(modelSage.printModel('o'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment