This repository has been archived by the owner on Jan 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naiveBayesClassifier.js
164 lines (129 loc) · 5.32 KB
/
naiveBayesClassifier.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var utilities = require('./utilities'),
Tokenizer = require('./tokenizer'),
TfIdf = require('./tfidf'),
fs = require('fs');
var calculateTfIdfProbabilities = function(words, documentTfIdfs, smoothingParameter) {
var wordTfIdfTotals = new Map(),
totalTfIdf = 0,
probabilities = new Map(),
totalWords = words.size;
documentTfIdfs.forEach(function(tfidfs) {
words.forEach(function(word) {
var tfidf = tfidfs.has(word) ? tfidfs.get(word) : 0;
totalTfIdf += tfidf;
if (wordTfIdfTotals.has(word)) {
wordTfIdfTotals.set(word, wordTfIdfTotals.get(word) + tfidf);
} else {
wordTfIdfTotals.set(word, tfidf);
}
});
});
wordTfIdfTotals.forEach(function(tfidf, word) {
var smoothedEstimate =
(wordTfIdfTotals.get(word) + smoothingParameter) /
(totalTfIdf + smoothingParameter * totalWords);
probabilities.set(word, smoothedEstimate);
});
return probabilities;
};
var calculateProbabilityFromWords = function(potentialTfIdfs, tfidfProbabilities) {
var probabilityFromWords = 0;
potentialTfIdfs.forEach(function(tfidf, word) {
var tfidfProbability,
logTfIdfProbability;
if (tfidfProbabilities.has(word)) {
tfidfProbability = tfidfProbabilities.get(word);
logTfIdfProbability = Math.log(tfidfProbability);
probabilityFromWords += tfidf * logTfIdfProbability;
}
});
return probabilityFromWords;
};
var NaiveBayesClassifier = function() {
this.tfidf = new TfIdf(new Tokenizer());
this.classes = new Map();
this.totalDocuments = 0;
};
NaiveBayesClassifier.prototype.addDocument = function(text, classification) {
this.tfidf.addDocument(text, classification);
++this.totalDocuments;
if (this.classes.has(classification)) {
this.classes.set(classification, this.classes.get(classification) + 1);
} else {
this.classes.set(classification, 1);
}
};
NaiveBayesClassifier.prototype.train = function() {
var words = this.tfidf.words;
this.tfidf.calculateTfIdfs();
this.classificationProbabilities = new Map();
this.classes.forEach(function(documentCount, group) {
var groupTfIdfs = this.tfidf.getTfIdfsForGroup(group);
var groupProbability = {
classification: Math.log(documentCount / this.totalDocuments),
tfidfProbabilities: calculateTfIdfProbabilities(words, groupTfIdfs, 1)
};
this.classificationProbabilities.set(group, groupProbability);
}, this);
};
NaiveBayesClassifier.prototype.classify = function(text) {
var potentialTfIdf = this.tfidf.getPotentialTfIdfs(text),
highestProbability = {
classification: '',
probability: Number.MIN_SAFE_INTEGER
};
this.classes.forEach(function(documentCount, classification) {
var probabilities = this.classificationProbabilities.get(classification),
wordProbability =
calculateProbabilityFromWords(
potentialTfIdf,
probabilities.tfidfProbabilities),
probability = probabilities.classification + wordProbability;
if (highestProbability.probability < probability) {
highestProbability.classification = classification;
highestProbability.probability = probability;
}
}, this);
return highestProbability.classification;
};
var classificationProbabilitiesToObject = function(classificationProbabilities) {
var theObject = {};
classificationProbabilities.forEach(function(probabilities, classification) {
theObject[classification] = {
classification: probabilities.classification,
tfidfProbabilities: utilities.mapToObject(probabilities.tfidfProbabilities)
};
});
return theObject;
};
var classificationProbabilitiesFromObject = function(theObject) {
var classificationProbabilities = new Map();
Object.getOwnPropertyNames(theObject).forEach(function(classification) {
var probabilities = theObject[classification];
classificationProbabilities.set(
classification,
{
classification: probabilities.classification,
tfidfProbabilities: utilities.mapFromObject(probabilities.tfidfProbabilities)
});
});
return classificationProbabilities;
};
NaiveBayesClassifier.prototype.toJSON = function() {
return {
tfidf: this.tfidf,
classes: utilities.mapToObject(this.classes),
classificationProbabilities: classificationProbabilitiesToObject(this.classificationProbabilities),
totalDocuments: this.totalDocuments
};
};
NaiveBayesClassifier.load = function(json) {
var loadedState = new NaiveBayesClassifier();
loadedState.tfidf = TfIdf.load(json.tfidf);
loadedState.classes = utilities.mapFromObject(json.classes);
loadedState.classificationProbabilities =
classificationProbabilitiesFromObject(json.classificationProbabilities);
loadedState.totalDocuments = json.totalDocuments;
return loadedState;
};
module.exports = NaiveBayesClassifier;