-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive-bayes.ts
171 lines (138 loc) · 4.6 KB
/
naive-bayes.ts
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
165
166
167
168
169
170
171
const fs = require('fs')
const stemmer = require('stemmer')
const uniq = require('lodash.uniq')
interface DocumentLabelInfo {
label: string
documentCount: number
wordCount: number
wordInfo: {
word: string;
occurrence: number;
}[]
}
interface ClassifierOptions {
presence?: boolean
bigrams?: boolean
useNegative?: boolean
}
export class NaiveBayesClassifier {
private _labelsAndInfo: DocumentLabelInfo[]
private _totalDocumentCount: number
private _vocabulary: Set<string>
private _presence: boolean
private _bigrams: boolean
private _useNegative: boolean
public constructor(options?: ClassifierOptions) {
const opt = options || {}
const presence = opt.presence || false
const bigrams = opt.bigrams || false
const useNegative = (!bigrams && opt.bigrams) || false
this._labelsAndInfo = []
this._totalDocumentCount = 0
this._vocabulary = new Set()
this._presence = presence
this._bigrams = bigrams
this._useNegative = useNegative
}
public toFile(path: string) {
fs.writeFileSync(path, JSON.stringify(this), 'utf-8')
}
public parseFile(path: string) {
if (fs.existsSync(path)) {
const properties: NaiveBayesClassifier = JSON.parse(fs.readFileSync(path, 'utf-8').toString())
this._totalDocumentCount = properties._totalDocumentCount
this._labelsAndInfo = properties._labelsAndInfo
this._labelsAndInfo.forEach(doc => doc.wordInfo.forEach(wordInfo => this._vocabulary.add(wordInfo.word)))
this._presence = properties._presence
this._bigrams = properties._bigrams
this._useNegative = properties._useNegative
}
}
public train(document: string, label: string) {
const words = this.extractWordsFromDocument(document)
const labelIndex = this.addLabel(label)
this._totalDocumentCount++
for (let i = 0; i < words.length; i++) {
this.addWordInLabel(words[i], labelIndex)
}
}
private addWordInLabel(word: string, labelIndex: number): number {
let wordIndex = this._labelsAndInfo[labelIndex]
.wordInfo
.findIndex(tokenInfo => tokenInfo.word == word)
if (wordIndex == -1) {
this._labelsAndInfo[labelIndex]
.wordInfo
.push({word: word, occurrence: 1})
wordIndex = this._labelsAndInfo[labelIndex].wordInfo.length - 1
this._vocabulary.add(word)
this._labelsAndInfo[labelIndex].wordCount++
} else {
this._labelsAndInfo[labelIndex].wordInfo[wordIndex].occurrence++
this._labelsAndInfo[labelIndex].wordCount++
}
return wordIndex
}
private addLabel(label: string) {
let labelIndex: number = this._labelsAndInfo.findIndex(el => el.label == label)
if (labelIndex == -1) {
this._labelsAndInfo.push({
label,
documentCount: 1,
wordInfo: [],
wordCount: 0,
})
labelIndex = this._labelsAndInfo.length - 1
} else {
this._labelsAndInfo[labelIndex].documentCount++
}
return labelIndex
}
public classify(document: string): string {
const words = this.extractWordsFromDocument(document)
let maxProbability = -Infinity
let maxLabel = ''
this._labelsAndInfo.forEach(labelAndInfo => {
let probability: number = Math.log(labelAndInfo.documentCount / this._totalDocumentCount)
const wordCount = labelAndInfo.wordCount
words.forEach(word => {
const wordInClass = labelAndInfo.wordInfo.find(ti => ti.word == word)
const wordFrequencyInClass = wordInClass ? wordInClass.occurrence : 0
const wordProbability = (wordFrequencyInClass + 1) / (wordCount + this._vocabulary.size)
probability += Math.log(wordProbability)
})
if (probability > maxProbability) {
maxProbability = probability
maxLabel = labelAndInfo.label
}
})
return maxLabel
}
private extractWordsFromDocument(document: string): string[] {
let words: string[] = document.split(' ')
if (this._presence) {
words = uniq(words)
}
for (let i = 0; i < words.length; i++) {
words[i] = words[i].toLowerCase()
words[i] = words[i].trim()
if (this._useNegative && i > 0 && words[i-1].match(/(n't|not)$/) != null) {
if (words[i].match(/\W$/) != null) {
words[i] = `NOT_${words[i]}`
}
}
words[i] = words[i].replace(/\W/g, '')
}
if (this._bigrams) {
let bigramWords = []
for (let i = 0; i < words.length; i += 2) {
bigramWords.push(`${words[i]} ${words[i+1]}`)
}
if (!(words.length % 2)) {
bigramWords.push(words[words.length - 1])
}
words = bigramWords
}
return words
}
}