3679
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
TextClassifier subclass:#BayesClassifier
|
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Collections-Text-Support'
|
|
10 |
!
|
|
11 |
|
3683
|
12 |
!BayesClassifier class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
|
16 |
an initial experiment in bayes text classification.
|
|
17 |
see BayesClassifierTest
|
|
18 |
This is possibly unfinished and may need more work.
|
|
19 |
|
|
20 |
[author:]
|
|
21 |
cg
|
|
22 |
"
|
|
23 |
!
|
|
24 |
|
|
25 |
examples
|
|
26 |
"
|
|
27 |
|b|
|
|
28 |
|
|
29 |
b := BayesClassifier new.
|
|
30 |
'teach it positive phrases'.
|
|
31 |
b classify:'amazing, awesome movie!!!! Yeah!!!!' asCategory: 'positive'.
|
|
32 |
b classify:'Sweet, this is incredibly, amazing, perfect, great!!!!' asCategory: 'positive'.
|
|
33 |
|
|
34 |
'teach it a negative phrase'.
|
|
35 |
b classify:'terrible, shitty thing. Damn. Sucks!!!!' asCategory: 'negative'.
|
|
36 |
|
|
37 |
'teach it a neutral phrase'.
|
|
38 |
b classify:'I dont really know what to make of this.' asCategory: 'neutral'.
|
|
39 |
|
|
40 |
'now test it to see that it correctly categorizes a new document'.
|
|
41 |
self assert:(b classify:'awesome, cool, amazing!!!! Yay.')= 'positive'.
|
|
42 |
"
|
|
43 |
! !
|
|
44 |
|
|
45 |
!BayesClassifier methodsFor:'text handling'!
|
|
46 |
|
|
47 |
classify:string
|
|
48 |
"assume that it is a regular text.
|
|
49 |
split first into lines..."
|
|
50 |
|
|
51 |
|tokens frequencyTable maxProbability chosenCategory|
|
|
52 |
|
|
53 |
maxProbability := Infinity negative.
|
|
54 |
|
|
55 |
tokens := self tokenize:string.
|
|
56 |
frequencyTable := tokens asBag.
|
|
57 |
|
|
58 |
categories do:[:categoryName |
|
|
59 |
|categoryProbability logProbability|
|
|
60 |
|
|
61 |
categoryProbability := (docCounts at:categoryName) / docCounts size.
|
|
62 |
logProbability := categoryProbability log.
|
|
63 |
frequencyTable valuesAndCountsDo:[:token :frequencyInText |
|
|
64 |
| tokenProbability|
|
|
65 |
|
|
66 |
tokenProbability := self tokenProbabilityOf:token inCategory:categoryName.
|
|
67 |
logProbability := logProbability + (frequencyInText * tokenProbability log).
|
|
68 |
].
|
|
69 |
Transcript show:'P(',categoryName,') = '; showCR:logProbability.
|
|
70 |
|
|
71 |
logProbability > maxProbability ifTrue:[
|
|
72 |
maxProbability := logProbability.
|
|
73 |
chosenCategory := categoryName.
|
|
74 |
].
|
|
75 |
].
|
|
76 |
^ chosenCategory
|
|
77 |
!
|
|
78 |
|
|
79 |
classify:string asCategory:categoryName
|
|
80 |
|tokens frequencyTable sumWordCount|
|
|
81 |
|
|
82 |
self initializeCategory:categoryName.
|
|
83 |
docCounts incrementAt:categoryName.
|
|
84 |
tokens := self tokenize:string.
|
|
85 |
frequencyTable := tokens asBag.
|
|
86 |
sumWordCount := 0.
|
|
87 |
frequencyTable valuesAndCountsDo:[:token :count |
|
|
88 |
vocabulary add:token.
|
|
89 |
(wordFrequencyCounts at:categoryName) incrementAt:token by:count.
|
|
90 |
sumWordCount := sumWordCount + count.
|
|
91 |
].
|
|
92 |
wordCounts incrementAt:categoryName by:sumWordCount
|
|
93 |
!
|
|
94 |
|
|
95 |
tokenProbabilityOf:token inCategory:category
|
|
96 |
"Calculate probability that a `token` belongs to a `category`"
|
|
97 |
|
|
98 |
|wordFrequencyCount wordCount prob|
|
|
99 |
|
|
100 |
wordFrequencyCount := (wordFrequencyCounts at:category) at:token ifAbsent:0.
|
|
101 |
wordCount := wordCounts at:category.
|
|
102 |
|
|
103 |
"/use laplace Add-1 Smoothing equation
|
|
104 |
prob :=( wordFrequencyCount + 1 ) / ( wordCount + vocabulary size ).
|
|
105 |
prob := prob asFloat.
|
|
106 |
Transcript showCR:(' P(%1, %2) = %3' bindWith:token with:category with:prob).
|
|
107 |
^ prob
|
|
108 |
! !
|
3679
|
109 |
|
|
110 |
!BayesClassifier class methodsFor:'documentation'!
|
|
111 |
|
|
112 |
version
|
|
113 |
^ '$Header$'
|
|
114 |
!
|
|
115 |
|
|
116 |
version_CVS
|
|
117 |
^ '$Header$'
|
|
118 |
! !
|
|
119 |
|