-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (30 loc) · 993 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const brain = require('brain.js');
const readline = require('readline');
const trainingData = require('./training-data.js');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Create a simple feed forward neural network
const net = new brain.NeuralNetwork();
// Train based on preprocessed training data
net.train(trainingData);
// Function to convert a string to tokenized object
function tokenize(inputString) {
const words = inputString.toLowerCase().split(' ');
const tokenized = {};
words.forEach(word => {
tokenized[word] = 1;
});
return tokenized;
}
const prompt = ()=> {
rl.question('Please enter a phrase for sentiment analysis: ', function(userInput) {
const tokenizedInput = tokenize(userInput);
const output = net.run(tokenizedInput);
console.log(`Sentiment analysis for the string "${userInput}"`);
console.log(output);
prompt();
});
}
prompt();