-
Notifications
You must be signed in to change notification settings - Fork 0
/
processDataCounts.scala
471 lines (369 loc) · 10.4 KB
/
processDataCounts.scala
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import BIDMat.{Mat, FMat, DMat, IMat, CMat, BMat, CSMat, SMat, SDMat, GMat, GIMat, GSMat, HMat}
import BIDMat.MatFunctions._
import BIDMat.SciFunctions._
import BIDMat.Solvers._
import BIDMat.Plotting._
import scala.util.Marshal
import scala.io.Source
import java.io._
import scala.collection.mutable.Map
FirstPass.main(Array())
object FirstPass {
var dictionary: Set[String] = Set[String]()
var wordBag: Map[Integer, Map[String, Integer]] = Map[Integer, Map[String, Integer]]() // ( review -> ( word -> wordCount) )
var labelBag: Map[Integer, Int] = Map[Integer, Int]() // review num -> label
var tokenIndex: Map[Integer, String] = Map[Integer, String]()
var revTokenIndex: Map[String, Integer] = Map[String, Integer]()
var stem = true
val stemmer = new Stemmer()
def main(args: Array[String]) {
firstPass()
secondPass()
thirdPass()
fourthPass()
}
def firstPass() = {
println("loading data...")
var tokens: IMat = load("/scratch/HW2/tokenized.mat", "tokens")
tokens = tokens.t(?, 2)
val words: CSMat = load("/scratch/HW2/tokenized.mat", "smap")
println("data loaded")
var ratingFlag = false
var reviewFlag = false
var review: Integer = 0
for ( i <- 0 to tokens.size-1 ) {
val index = tokens(i)-1
if ( words(index) == "</review_text>" ) {
reviewFlag = false
if (review%1000 == 0) println(review)
review += 1
}
if ( ratingFlag ) {
var l = 0
var ll = words(index)
if ( ll == "1" ) { l = 1 }
else if ( ll == "2" ) { l = 2 }
else if ( ll == "3" ) { l = 3 }
else if ( ll == "4" ) { l = 4 }
else if ( ll == "5" ) { l = 5 }
labelBag += (review -> l)
ratingFlag = false
}
if ( reviewFlag && index < 100000 ) {
var wd = words(index)
if (stem) { wd = stemmer.stem(wd) }
if ( wordBag(review) contains wd ) { wordBag(review)(wd) += 1 }
else { wordBag(review)(wd) = 1 }
dictionary = dictionary + wd
}
if ( words(index) == "<rating>" ) { ratingFlag = true }
if ( words(index) == "<review_text>" ) {
reviewFlag = true
wordBag += ( review -> Map[String, Integer]() )
}
}
}
def secondPass() = {
println("book keeping...")
var i: Integer = 0
for ( x <- dictionary ) {
tokenIndex += ( i -> x )
revTokenIndex += ( x -> i)
i += 1
}
println("built dictionary of " + dictionary.size + " tokens.")
println("collected " + wordBag.keys.size + " bags of words")
println("gathered " + labelBag.keys.size + " ratings!")
}
def thirdPass() = {
println("building Y...")
var Y:FMat = FMat(labelBag.keys.size, 1)
for (i <- 0 to Y.nrows-1) { Y(i, 0) = labelBag(i).toFloat }
println("built Y vector")
println("building X matrix...")
var X: SMat = null
for ( i <- 0 to wordBag.keys.size-1 ) {
var ii: IMat = null
for ( t <- wordBag(i).keys ) {
if ( ii == null ) { ii = icol(revTokenIndex(t)) }
else { ii = ii on revTokenIndex(t) }
}
val jj: IMat = IMat(wordBag(i).keys.size, 1)
var vv: FMat = null
for ( t <- wordBag(i).keys ) {
val v = wordBag(i)(t).toFloat
if ( vv == null ) { vv = col(v) }
else { vv = vv on col(v) }
}
var c: SMat = null
if ( ii == null ) { c = sparse(zeros(dictionary.size,1), dictionary.size,1) }
else { c = sparse(ii, jj, vv, dictionary.size, 1) }
if ( X == null ) { X = c }
else { X = X \ c }
if ( (i+1)%1000 == 0 ) {
println("Saving " + (i+1)/1000.0 + "th partial X")
if (stem) { saveAs("CountsOut/CountsStemmedX"+((i+1)/1000.0).toInt+".mat", X, ((i+1)/1000.0).toInt+"CountsStemmedX") }
//else { saveAs("out/TrimmedSparse"+((i+1)/1000.0).toInt+".mat", X, ((i+1)/1000.0).toInt+"CountsX") }
X = null
}
}
println("saving Y and last partial X")
if (stem) {
saveAs("CountsOut/CountsY.mat", Y, "CountsY")
saveAs("CountsOut/CountsStemmedLastX.mat", X, "LastCountsStemmedX")
} else {
saveAs("out/TrimmedSparseY.mat", Y, "CountsY")
saveAs("out/TrimmedSparseLastX.mat", X, "CountsLastX")
}
}
def fourthPass() = {
println("building and saving dictionary column")
val dict: CSMat = CSMat(dictionary.size, 1)
for ( t <- dictionary ) {
dict(revTokenIndex(t), 0) = t
}
saveAs("CountsOut/Dict.mat")
}
}
///Porter Stemmer
class Stemmer
{
// word to be stemmed.
var b = ""
// Just recode the existing stuff, then go through and refactor with some intelligence.
def cons( i: Int ): Boolean =
{
var ch = b( i )
// magic!
var vowels = "aeiou"
// multi return. yuck
if ( vowels.contains( ch ) )
return false
if ( ch == 'y' )
{
if ( i == 0 )
{
return true
}
else
{
// loop it!
return !cons( i - 1 )
}
}
return true
}
// Add via letter or entire word
def add( ch: Char ) =
{
b += ch
}
def add( word: String ) =
{
b = word
}
/* m() measures the number of consonant sequences between 0 and j. if c is
a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
presence,
<c><v> gives 0
<c>vc<v> gives 1
<c>vcvc<v> gives 2
<c>vcvcvc<v> gives 3
....
*
* I think this can be recoded far more neatly.
*/
def calcM(s:String): Int =
{
var l = s.length
var count = 0
var currentConst = false
for ( c <- 0 to l-1 )
{
if ( cons( c ) )
{
if (!currentConst && c != 0 )
{
count += 1
}
currentConst = true
}
else
{
currentConst = false
}
}
return count
}
// removing the suffix 's', does a vowel exist?'
def vowelInStem( s: String): Boolean =
{
for (i <- 0 to b.length -1 - s.length )
{
if ( !cons( i ) )
{
return true
}
}
return false;
}
/* doublec(j) is true <=> j,(j-1) contain a double consonant. */
def doublec( ): Boolean =
{
var l = b.length - 1
if ( l < 1 )
return false
if ( b(l) != b(l-1) )
return false
return cons( l )
}
/* cvc(i) is true <=> i-2,i-1,i has the form consonant - vowel - consonant
and also if the second c is not w,x or y. this is used when trying to
restore an e at the end of a short word. e.g.
cav(e), lov(e), hop(e), crim(e), but
snow, box, tray.
*/
def cvc( s:String): Boolean =
{
var i = b.length - 1 - s.length
if (i < 2 || !cons(i) || cons(i-1) || !cons(i-2))
return false;
var ch = b(i)
var vals = "wxy"
if ( vals.contains( ch ) )
return false
return true;
}
// returns true if it did the change.
def replacer( orig: String, replace:String, checker: Int => Boolean ): Boolean =
{
var l = b.length
var origLength = orig.length
var res = false
if ( b.endsWith( orig ) )
{
var n = b.substring( 0, l - origLength )
var m = calcM( n )
if ( checker( m ) )
{
b = n + replace
}
res = true
}
return res
}
// process the list of tuples to find which prefix matches the case.
// checker is the conditional checker for m.
def processSubList( l:List[(String, String)], checker: Int=>Boolean ): Boolean =
{
var iter = l.elements
var done = false
while (!done && iter.hasNext )
{
var v = iter.next
done = replacer( v._1, v._2, checker )
}
return done
}
def step1()
{
var l = b.length
var m = calcM( b )
// step 1a
var vals = List( ("sses", "ss"), ("ies","i"), ("ss","ss"), ("s", "") )
processSubList( vals, _ >= 0)
// step 1b
if ( !(replacer( "eed", "ee", _>0) ) )
{
if ( ( vowelInStem("ed") && replacer("ed", "", _>=0) ) || ( vowelInStem("ing") && replacer( "ing", "", _>=0) ) )
{
vals = List( ("at", "ate"), ("bl","ble"), ("iz","ize"))
if (! processSubList( vals, _>=0 ) )
{
// if this isn't done, then it gets more confusing.
m = calcM( b )
var last = b( b.length -1 )
if ( doublec() && !"lsz".contains( last ) )
{
b = b.substring( 0, b.length - 1 )
}
else
if ( m == 1 && cvc("") )
{
b = b + "e"
}
}
}
}
// step 1c
( vowelInStem("y") && replacer("y", "i", _>=0))
}
def step2( ) =
{
var vals = List( ("ational", "ate"),("tional","tion"),("enci","ence"),("anci","ance"),("izer","ize"),("bli","ble"),("alli", "al"),
("entli","ent"),("eli","e"),("ousli","ous"),("ization","ize"),("ation","ate"),("ator","ate"),("alism","al"),
("iveness","ive"),("fulness","ful"),("ousness", "ous"),("aliti", "al"),("iviti","ive"),("biliti", "ble"),("logi", "log"))
processSubList( vals, _>0 )
}
def step3( ) =
{
var vals = List( ("icate", "ic"),("ative",""),("alize","al"),("iciti","ic"),("ical","ic"),("ful",""),("ness",""))
processSubList( vals, _>0 )
}
def step4( ) =
{
// first part.
var vals = List( ("al",""),("ance",""),("ence",""),("er",""),("ic",""),("able",""),("ible",""),("ant",""),("ement",""),
("ment",""),("ent",""))
var res = processSubList( vals, _>1 )
// special part.
if ( ! res )
{
if ( b.length > 4 )
{
if ( b( b.length - 4 ) == 's' || b( b.length -4 ) == 't' )
{
res = replacer("ion", "", _>1)
}
}
}
// third part.
if ( !res )
{
var vals = List( ("ou",""),("ism",""),("ate",""),("iti",""),("ous",""),("ive",""),("ize",""))
res = processSubList( vals, _>1 )
}
}
def step5a( ) =
{
var res = false
res = replacer("e", "", _>1)
if ( !cvc("e") )
{
res = replacer("e", "", _==1)
}
}
def step5b( ) =
{
var res = false
var m = calcM( b )
if ( m > 1 && doublec() && b.endsWith("l") )
{
b = b.substring(0, b.length - 1)
}
}
def stem (str: String ): String =
{
add(str)
if ( b.length > 2 )
{
step1()
step2()
step3()
step4()
step5a()
step5b()
}
return b
}
}