4273
|
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 |
"
|
3678
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
|
16 |
Object subclass:#TextClassifier
|
|
17 |
instanceVariableNames:'wordBag sentences docCounts wordCounts wordFrequencyCounts
|
|
18 |
categories vocabulary'
|
|
19 |
classVariableNames:''
|
|
20 |
poolDictionaries:''
|
|
21 |
category:'Collections-Text-Support'
|
|
22 |
!
|
|
23 |
|
|
24 |
!TextClassifier class methodsFor:'documentation'!
|
|
25 |
|
4273
|
26 |
copyright
|
|
27 |
"
|
|
28 |
COPYRIGHT (c) 2016 by eXept Software AG
|
|
29 |
All Rights Reserved
|
|
30 |
|
|
31 |
This software is furnished under a license and may be used
|
|
32 |
only in accordance with the terms of that license and with the
|
|
33 |
inclusion of the above copyright notice. This software may not
|
|
34 |
be provided or otherwise made available to, or used by, any
|
|
35 |
other person. No title to or ownership of the software is
|
|
36 |
hereby transferred.
|
|
37 |
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
3678
|
41 |
documentation
|
|
42 |
"
|
3682
|
43 |
an initial experiment in text classification.
|
3678
|
44 |
see BayesClassifierTest
|
|
45 |
|
|
46 |
[author:]
|
|
47 |
cg
|
|
48 |
"
|
|
49 |
! !
|
|
50 |
|
|
51 |
!TextClassifier class methodsFor:'instance creation'!
|
|
52 |
|
|
53 |
new
|
|
54 |
"return an initialized instance"
|
|
55 |
|
|
56 |
^ self basicNew initialize.
|
|
57 |
! !
|
|
58 |
|
|
59 |
!TextClassifier methodsFor:'initialization'!
|
|
60 |
|
|
61 |
initialize
|
|
62 |
"Invoked when a new instance is created."
|
|
63 |
|
|
64 |
wordBag := Bag new.
|
|
65 |
"/ sentences := nil.
|
|
66 |
docCounts := Dictionary new.
|
|
67 |
wordCounts := Dictionary new.
|
|
68 |
wordFrequencyCounts := Dictionary new.
|
|
69 |
categories := Set new.
|
|
70 |
vocabulary := Set new.
|
|
71 |
|
|
72 |
"/ super initialize. -- commented since inherited method does nothing
|
|
73 |
!
|
|
74 |
|
|
75 |
initializeCategory:categoryName
|
|
76 |
(categories includes:categoryName) ifFalse:[
|
|
77 |
docCounts at:categoryName put:0.
|
|
78 |
wordCounts at:categoryName put:0.
|
|
79 |
wordFrequencyCounts at:categoryName put:(Dictionary new).
|
|
80 |
categories add:categoryName
|
|
81 |
].
|
|
82 |
! !
|
|
83 |
|
|
84 |
!TextClassifier methodsFor:'text handling'!
|
|
85 |
|
|
86 |
collectWords:lines
|
|
87 |
"computes words from lines"
|
|
88 |
|
|
89 |
|words|
|
|
90 |
|
|
91 |
words := lines collectAll:[:l |
|
|
92 |
l asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isLetterOrDigit not]
|
|
93 |
].
|
|
94 |
^ words
|
|
95 |
!
|
|
96 |
|
|
97 |
dehyphenate:linesCollection
|
|
98 |
"join hypens"
|
|
99 |
|
|
100 |
|lines partialLine|
|
|
101 |
|
|
102 |
lines := OrderedCollection new.
|
|
103 |
linesCollection do:[:eachLine |
|
|
104 |
|l isHyphenated|
|
|
105 |
|
|
106 |
l := eachLine withoutSeparators.
|
|
107 |
l notEmptyOrNil ifTrue:[
|
|
108 |
isHyphenated := (l endsWith:'-')
|
|
109 |
and:[ l size > 1
|
|
110 |
and:[ (l at:(l size-1)) isLetter ]].
|
|
111 |
isHyphenated ifFalse:[
|
|
112 |
partialLine := (partialLine ? '') , l.
|
|
113 |
lines add:partialLine.
|
|
114 |
partialLine := nil.
|
|
115 |
] ifTrue:[
|
|
116 |
l := l copyButLast.
|
|
117 |
partialLine := (partialLine ? '') , l.
|
|
118 |
].
|
|
119 |
].
|
|
120 |
].
|
|
121 |
partialLine notEmptyOrNil ifTrue:[
|
|
122 |
lines add:partialLine
|
|
123 |
].
|
|
124 |
^ lines
|
|
125 |
!
|
|
126 |
|
|
127 |
tokenize:string
|
|
128 |
|rawLines lines allWords|
|
|
129 |
|
|
130 |
rawLines := string asCollectionOfLines.
|
|
131 |
lines := self dehyphenate:rawLines.
|
|
132 |
allWords := self collectWords:lines.
|
|
133 |
^ allWords
|
|
134 |
! !
|
|
135 |
|
|
136 |
!TextClassifier class methodsFor:'documentation'!
|
|
137 |
|
|
138 |
version
|
|
139 |
^ '$Header$'
|
|
140 |
!
|
|
141 |
|
|
142 |
version_CVS
|
|
143 |
^ '$Header$'
|
|
144 |
! !
|
|
145 |
|