Skip to content

Instantly share code, notes, and snippets.

@jdrew1303
Last active August 5, 2018 00:31
Show Gist options
  • Save jdrew1303/5bc90efd112004b7c8267da269d24455 to your computer and use it in GitHub Desktop.
Save jdrew1303/5bc90efd112004b7c8267da269d24455 to your computer and use it in GitHub Desktop.
using a neural network for text classification in javascript
const mimir = require('mimir');
const brain = require('brain.js');
/* few utils for the example */
// our output is recorded as a vector, where the index in the vector
// is the enum (we create it with a zero index). We fill the index in
// the array with a 1 and the rest of the items as a 0 (true and false).
const vec_result = (res, num_classes) => {
const outputVector = new Array(num_classes).fill(0);
outputVector[res] = 1;
return outputVector;
}
const maxarg = (array) => array.indexOf(Math.max.apply(Math, array));
// train data
const CLASSIFICATION_CLASSES = {
HISTORY: 0,
PROGRAMMING: 1,
MUSIC: 2
};
const {HISTORY, PROGRAMMING, MUSIC} = CLASSIFICATION_CLASSES;
const classes_array = Object.keys(CLASSIFICATION_CLASSES); //['HISTORY', 'PROGRAMMING', 'MUSIC'],
const texts = [
// history
"The end of the Viking-era in Norway is marked by the Battle of Stiklestad in 1030",
"The end of the Viking Age is traditionally marked in England by the failed invasion attempted by the Norwegian king Harald III ",
"The earliest date given for a Viking raid is 787 AD when, according to the Anglo-Saxon Chronicle, a group of men from Norway sailed to the Isle of Portland in Dorset",
// programming
"A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs to control the behavior of a machine or to express algorithms.",
"Thousands of different programming languages have been created, mainly in the computer field, and many more still are being created every year.",
"The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning). Some languages are defined by a specification document (for example, the C programming language is specified by an ISO Standard), while other languages (such as Perl) have a dominant implementation that is treated as a reference",
// music
"Classical music is art music produced or rooted in the traditions of Western music (both liturgical and secular)",
"European music is largely distinguished from many other non-European and popular musical forms by its system of staff notation, in use since about the 16th century",
"classical music has been noted for its development of highly sophisticated forms of instrumental music."
];
const dict = mimir.dict(texts);
const traindata = [
[mimir.bow(texts[0], dict), HISTORY],
[mimir.bow(texts[1], dict), HISTORY],
[mimir.bow(texts[2], dict), HISTORY],
[mimir.bow(texts[3], dict), PROGRAMMING],
[mimir.bow(texts[4], dict), PROGRAMMING],
[mimir.bow(texts[5], dict), PROGRAMMING],
[mimir.bow(texts[6], dict), MUSIC],
[mimir.bow(texts[7], dict), MUSIC],
[mimir.bow(texts[8], dict), MUSIC]
];
const test_history = "The beginning of the Viking Age in the British Isles is, however, often given as 793.";
const test_music = "Baroque music is a style of Western art music composed from approximately 1600 to 1750";
const test_bow_history = mimir.bow(test_history, dict);
const test_bow_music = mimir.bow(test_music, dict);
const net = new brain.NeuralNetwork();
const ann_train = traindata.map((pair) => ({
input: pair[0],
output: vec_result(pair[1], 3)
}));
net.train(ann_train);
console.log('------------------- ANN (brain) ----------------------');
const predict = net.run(test_bow_history);
console.log(predict);
console.log(net.run(test_bow_music));
console.log(classes_array[maxarg(predict)]); // prints HISTORY
console.log(classes_array[maxarg(net.run(test_bow_music))]); // prints MUSIC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment