Last active
June 24, 2019 21:30
-
-
Save lucydjo/1a4c7c848f18074ccd195eb8d7828b6a 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
const brain = require('brain.js'); | |
const fs = require('fs') | |
var validator = require('validator'); | |
var csv = require('csv-parser') | |
const net = new brain.NeuralNetwork({ | |
binaryThresh: 0.5, | |
hiddenLayers: [3], // array of ints for the sizes of the hidden layers in the network | |
activation: 'sigmoid', // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'], | |
leakyReluAlpha: 0.01 // supported for activation type 'leaky-relu' | |
}); | |
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'é', 'è', 'à', 'ç'] | |
const alphabet_num = []; | |
for (k in alphabet) { alphabet_num[alphabet[k]] = k; } | |
let trainingData = []; | |
fs.createReadStream('data/dataset.txt') | |
.pipe(csv()) | |
.on('data', (data) => { | |
if (data.langage == 'french') { | |
if (validator.isAlpha(data.prenom, 'fr-FR') == true) { | |
if (data.genre == 'f') { | |
trainingData.push({ input: textToArray(data.prenom), output: [0] }) | |
} | |
if (data.genre == 'm') { | |
trainingData.push({ input: textToArray(data.prenom), output: [0] }) | |
} | |
} | |
} | |
}) | |
.on('end', () => { | |
const config = { | |
iterations: 1000, | |
log: true, | |
logPeriod: 50, | |
layers: [3] | |
}; | |
let fixedData = fixLengths(trainingData); | |
console.log(fixedData); | |
net.train(fixedData, config); | |
}); | |
function fixLengths(data) { | |
let maxLengthInput = -1; | |
for (let i = 0; i < data.length; i++) { | |
if (data[i].input.length > maxLengthInput) { | |
maxLengthInput = data[i].input.length; | |
} | |
} | |
for (let i = 0; i < data.length; i++) { | |
while (data[i].input.length < maxLengthInput) { | |
data[i].input.push(0); | |
} | |
} | |
return data; | |
} | |
function textToArray(text) { | |
let textArray = []; | |
for (var i = 0; i < text.length; i++) { | |
const letter = text[i]; | |
if (letter != null) { | |
textArray.push(parseInt(alphabet_num[letter])); | |
} else { | |
textArray.push(parseInt(-1)); | |
} | |
} | |
return textArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment