3678
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
Object subclass:#TextClassifier
|
|
6 |
instanceVariableNames:'wordBag sentences docCounts wordCounts wordFrequencyCounts
|
|
7 |
categories vocabulary'
|
|
8 |
classVariableNames:''
|
|
9 |
poolDictionaries:''
|
|
10 |
category:'Collections-Text-Support'
|
|
11 |
!
|
|
12 |
|
|
13 |
!TextClassifier class methodsFor:'documentation'!
|
|
14 |
|
|
15 |
documentation
|
|
16 |
"
|
|
17 |
an initial experiment in bayes text classification.
|
|
18 |
see BayesClassifierTest
|
|
19 |
This is possibly unfinished and may need more work.
|
|
20 |
|
|
21 |
[author:]
|
|
22 |
cg
|
|
23 |
|
|
24 |
[instance variables:]
|
|
25 |
|
|
26 |
[class variables:]
|
|
27 |
|
|
28 |
[see also:]
|
|
29 |
|
|
30 |
"
|
|
31 |
! !
|
|
32 |
|
|
33 |
!TextClassifier class methodsFor:'instance creation'!
|
|
34 |
|
|
35 |
new
|
|
36 |
"return an initialized instance"
|
|
37 |
|
|
38 |
^ self basicNew initialize.
|
|
39 |
! !
|
|
40 |
|
|
41 |
!TextClassifier methodsFor:'initialization'!
|
|
42 |
|
|
43 |
initialize
|
|
44 |
"Invoked when a new instance is created."
|
|
45 |
|
|
46 |
wordBag := Bag new.
|
|
47 |
"/ sentences := nil.
|
|
48 |
docCounts := Dictionary new.
|
|
49 |
wordCounts := Dictionary new.
|
|
50 |
wordFrequencyCounts := Dictionary new.
|
|
51 |
categories := Set new.
|
|
52 |
vocabulary := Set new.
|
|
53 |
|
|
54 |
"/ super initialize. -- commented since inherited method does nothing
|
|
55 |
!
|
|
56 |
|
|
57 |
initializeCategory:categoryName
|
|
58 |
(categories includes:categoryName) ifFalse:[
|
|
59 |
docCounts at:categoryName put:0.
|
|
60 |
wordCounts at:categoryName put:0.
|
|
61 |
wordFrequencyCounts at:categoryName put:(Dictionary new).
|
|
62 |
categories add:categoryName
|
|
63 |
].
|
|
64 |
! !
|
|
65 |
|
|
66 |
!TextClassifier methodsFor:'text handling'!
|
|
67 |
|
|
68 |
classify:string
|
|
69 |
"assume that it is a regular text.
|
|
70 |
split first into lines..."
|
|
71 |
|
|
72 |
|tokens frequencyTable maxProbability chosenCategory|
|
|
73 |
|
|
74 |
maxProbability := Infinity negative.
|
|
75 |
|
|
76 |
tokens := self tokenize:string.
|
|
77 |
frequencyTable := tokens asBag.
|
|
78 |
|
|
79 |
categories do:[:categoryName |
|
|
80 |
|categoryProbability logProbability|
|
|
81 |
|
|
82 |
categoryProbability := (docCounts at:categoryName) / docCounts size.
|
|
83 |
logProbability := categoryProbability log.
|
|
84 |
frequencyTable valuesAndCountsDo:[:token :frequencyInText |
|
|
85 |
| tokenProbability|
|
|
86 |
|
|
87 |
tokenProbability := self tokenProbabilityOf:token inCategory:categoryName.
|
|
88 |
logProbability := logProbability + (frequencyInText * tokenProbability log).
|
|
89 |
].
|
|
90 |
Transcript show:'P(',categoryName,') = '; showCR:logProbability.
|
|
91 |
|
|
92 |
logProbability > maxProbability ifTrue:[
|
|
93 |
maxProbability := logProbability.
|
|
94 |
chosenCategory := categoryName.
|
|
95 |
].
|
|
96 |
].
|
|
97 |
^ chosenCategory
|
|
98 |
!
|
|
99 |
|
|
100 |
classify:string asCategory:categoryName
|
|
101 |
|tokens frequencyTable sumWordCount|
|
|
102 |
|
|
103 |
self initializeCategory:categoryName.
|
|
104 |
docCounts incrementAt:categoryName.
|
|
105 |
tokens := self tokenize:string.
|
|
106 |
frequencyTable := tokens asBag.
|
|
107 |
sumWordCount := 0.
|
|
108 |
frequencyTable valuesAndCountsDo:[:token :count |
|
|
109 |
vocabulary add:token.
|
|
110 |
(wordFrequencyCounts at:categoryName) incrementAt:token by:count.
|
|
111 |
sumWordCount := sumWordCount + count.
|
|
112 |
].
|
|
113 |
wordCounts incrementAt:categoryName by:sumWordCount
|
|
114 |
!
|
|
115 |
|
|
116 |
collectWords:lines
|
|
117 |
"computes words from lines"
|
|
118 |
|
|
119 |
|words|
|
|
120 |
|
|
121 |
words := lines collectAll:[:l |
|
|
122 |
l asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isLetterOrDigit not]
|
|
123 |
].
|
|
124 |
^ words
|
|
125 |
!
|
|
126 |
|
|
127 |
dehyphenate:linesCollection
|
|
128 |
"join hypens"
|
|
129 |
|
|
130 |
|lines partialLine|
|
|
131 |
|
|
132 |
lines := OrderedCollection new.
|
|
133 |
linesCollection do:[:eachLine |
|
|
134 |
|l isHyphenated|
|
|
135 |
|
|
136 |
l := eachLine withoutSeparators.
|
|
137 |
l notEmptyOrNil ifTrue:[
|
|
138 |
isHyphenated := (l endsWith:'-')
|
|
139 |
and:[ l size > 1
|
|
140 |
and:[ (l at:(l size-1)) isLetter ]].
|
|
141 |
isHyphenated ifFalse:[
|
|
142 |
partialLine := (partialLine ? '') , l.
|
|
143 |
lines add:partialLine.
|
|
144 |
partialLine := nil.
|
|
145 |
] ifTrue:[
|
|
146 |
l := l copyButLast.
|
|
147 |
partialLine := (partialLine ? '') , l.
|
|
148 |
].
|
|
149 |
].
|
|
150 |
].
|
|
151 |
partialLine notEmptyOrNil ifTrue:[
|
|
152 |
lines add:partialLine
|
|
153 |
].
|
|
154 |
^ lines
|
|
155 |
!
|
|
156 |
|
|
157 |
tokenProbabilityOf:token inCategory:category
|
|
158 |
"Calculate probability that a `token` belongs to a `category`"
|
|
159 |
|
|
160 |
|wordFrequencyCount wordCount prob|
|
|
161 |
|
|
162 |
wordFrequencyCount := (wordFrequencyCounts at:category) at:token ifAbsent:0.
|
|
163 |
wordCount := wordCounts at:category.
|
|
164 |
|
|
165 |
"/use laplace Add-1 Smoothing equation
|
|
166 |
prob :=( wordFrequencyCount + 1 ) / ( wordCount + vocabulary size ).
|
|
167 |
prob := prob asFloat.
|
|
168 |
Transcript showCR:(' P(%1, %2) = %3' bindWith:token with:category with:prob).
|
|
169 |
^ prob
|
|
170 |
!
|
|
171 |
|
|
172 |
tokenize:string
|
|
173 |
|rawLines lines allWords|
|
|
174 |
|
|
175 |
rawLines := string asCollectionOfLines.
|
|
176 |
lines := self dehyphenate:rawLines.
|
|
177 |
allWords := self collectWords:lines.
|
|
178 |
^ allWords
|
|
179 |
! !
|
|
180 |
|
|
181 |
!TextClassifier class methodsFor:'documentation'!
|
|
182 |
|
|
183 |
version
|
|
184 |
^ '$Header$'
|
|
185 |
!
|
|
186 |
|
|
187 |
version_CVS
|
|
188 |
^ '$Header$'
|
|
189 |
! !
|
|
190 |
|