I will add a wordInDoc object (word: num) if the word is in the object vocab [positive], I try with equal to but fail. Why?
this is my code
var nbayes = function(_class, docs) {
var vocab = {
positif: {
wd: ['baik', 'pintar']
},
negatif: {
wd: ['buruk', 'jelek']
}
}
var wordInDoc = {}
var sumDocs = 0;
docs = docs.split(' ')
var wd = 'wd'
for (var word of docs) {
if (word in vocab[_class][wd]) {
var delta = 1
wordInDoc[word] = 0
wordInDoc[word] += delta
sumDocs++
}
console.log(wordInDoc, sumDocs)
}
};
nbayes('positif', 'baik dan rajin')
nbayes('negatif', 'nakal dan bodoh')
Is this a solution you were looking for?
Loop through the array 'docs' then check for the index of matching in 'vocab[_class][wd]'.
Some other validation should be done for non existent classes'_class'.
var nbayes = function(_class, docs) {
var wordInDoc = {};
var sumDocs = 0;
var wd = 'wd';
var word;
var vocab = {
positif: {
wd: ['baik', 'pintar']
},
negatif: {
wd: ['buruk', 'jelek']
}
}
docs = docs.split(' ');
for (var i = 0;i < docs.length; i++) {
word = docs[i];
if (vocab[_class] && vocab[_class][wd].indexOf(word) !== -1) {
var delta = 1;
wordInDoc[word] = 0;
wordInDoc[word] += delta;
sumDocs++;
}
console.log(wordInDoc, sumDocs)
}
};
nbayes('positif', 'baik dan rajin')
nbayes('negatif', 'nakal dan bodoh')