This post will cover - how to create a simple text analyser to classify text based on a training set data into one or another profile.
We will use 'brain.js' for the neural network used to train on the data.
This can be done in the following steps -
We will use 'brain.js' for the neural network used to train on the data.
This can be done in the following steps -
- Create a profile for a particular type of text - provide adequate training data.
- Create training data with input, output structure.
- Write a script to injest data splitting in the input & output format & process training data.
- Train the network to iterate with input & outputs.
- Execute the algorithm with new input on the trained neural net.
- Create an html to host the scripts which would be run on the browser console.
Sample training data
const trainingData = [
{
input: "Very well, thank you!",
output: { at: 1 }
},{
input: "Inside my baby's room",
output: { a1: 1 }
},
.
.
.
]
Html page - will host a call to script - can be named text-analyser.js - this will run the 'execute function and take an input text - for e.g. - 'my daily activity'.
-- import js with training data
-- import js with text-analyser
-- import brain.js for the neural net.
Text-Analyser.js - this will host the code for -
1. processing training data
function processTrainingData(data) {
return data.map(d => {
return {
input: encode(d.input),
output: d.output
}
})
}
2. train using the training data
function train(data) {
let net = new brain.NeuralNetwork();
net.train(processTrainingData(data));
trainedNet = net.toFunction();
};
3. execute the code with the training completed on new input
function execute(input) {
let results = trainedNet(encode(input));
console.log(results)
let output;
let certainty;
if (results.at > results.a1) {
output = 'Type A'
certainty = Math.floor(results.at * 100)
} else {
output = 'Type B'
certainty = Math.floor(results.a1 * 100)
}
return "I'm " + certainty + "% sure that text was of " + output;
}
All together below statements will execute the code when the - html page is opened up in browser and output will be visible in console.
train(trainingData);
console.log(execute("This is my sweet child!"));
Reference : my text..ai.
No comments:
Post a Comment