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