Source

Blacklist Classifier / COLING2012 / Makefile

Full commit
  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

TRAINDATA = 	../test/data/train/bhs_sr.txt.gz \
		../test/data/train/bhs_hr.txt.gz \
		../test/data/train/bhs_bs.txt.gz

TESTDATA = 	../test/data/eval/politika.rs.200.check \
		../test/data/eval/vecernji.hr.200.check \
		../test/data/eval/dnevniavaz.ba.200.check

LANGS = sr hr bs

EXPERIMENTS = default max0 score0.5 min3max3score0.5 upper notok noalpha plain
RUNS = $(patsubst %,experiments/%/run.out,${EXPERIMENTS})

CLASSIFIER = ../Lingua-Identify-Blacklists/bin/blacklist_classifier -d blacklists

all: ${RUNS}

# train the blacklists

train:
	${CLASSIFIER} -t "${TRAINDATA}" sr hr bs

# test the blacklist classifier (run train first)

test:
	${CLASSIFIER} -i sr hr bs \
		< ../test/data/eval/politika.rs.200.check \
		> experiments/politika.rs.200.check.guess
	${CLASSIFIER} -i sr hr bs \
		< ../test/data/eval/vecernji.hr.200.check \
		> experiments/vecernji.hr.200.check.guess
	${CLASSIFIER} -i sr hr bs \
		< ../test/data/eval/dnevniavaz.ba.200.check \
		> experiments/dnevniavaz.ba.200.check.guess


# test the blacklist classififer with various text sizes
# (from 10 - 300 words per test document)

test-size:
	@for w in {1..30}; do \
		echo "$${w}0 " | tr "\n" ' '; \
		${CLASSIFIER} -m $${w}0 -i $(LANGS) \
			< data/eval/politika.rs.300w |\
			sort | uniq -c | grep sr | tr "\n" ' '; \
		${CLASSIFIER} -m $${w}0 -i $(LANGS) \
			< data/eval/vecernji.hr.300w |\
			sort | uniq -c | grep hr | tr "\n" ' '; \
		${CLASSIFIER} -m $${w}0 -i $(LANGS) \
			< data/eval/dnevniavaz.ba.300w |\
			sort | uniq -c | grep bs; \
	done

# save test-size results in a file
# and compute the overall accuracy of the classifier

results.size:
	${MAKE} test-size > results.size

results.size.accuracy: results.size
	perl -e 'while(<>){chomp;@a=split(/\s+/);print $$acc=($$a[1]+$$a[3]+$$a[5])/371,"\n";}' \
	< $< > $@


# classify with margin

margin:
	${CLASSIFIER} -M 2 -i sr hr bs \
		< ../test/data/eval/politika.rs.200.check \
		> experiments/politika.rs.200.check.guess
	${CLASSIFIER} -M 2 -i sr hr bs \
		< ../test/data/eval/vecernji.hr.200.check \
		> experiments/vecernji.hr.200.check.guess
	${CLASSIFIER} -M 2 -i sr hr bs \
		< ../test/data/eval/dnevniavaz.ba.200.check \
		> experiments/dnevniavaz.ba.200.check.guess

# cascaded classification (default)

cascaded:
	${CLASSIFIER} -i sr hr bs \
		< ../test/data/eval/politika.rs.200.check \
		> experiments/politika.rs.200.check.guess
	${CLASSIFIER} -i sr hr bs \
		< ../test/data/eval/vecernji.hr.200.check \
		> experiments/vecernji.hr.200.check.guess
	${CLASSIFIER} -i sr hr bs \
		< ../test/data/eval/dnevniavaz.ba.200.check \
		> experiments/dnevniavaz.ba.200.check.guess



## verbose: same as train & test but with verbose output saved in a file ....

verbose:
	${CLASSIFIER} -t "${TRAINDATA}" sr hr bs
	${CLASSIFIER} -i -v sr hr bs \
		< ../test/data/eval/politika.rs.200.check \
		> experiments/politika.rs.200.check.guess \
		2> experiments/politika.rs.200.check.verbose
	${CLASSIFIER} -i -v hr sr bs \
		< ../test/data/eval/vecernji.hr.200.check \
		> experiments/vecernji.hr.200.check.guess \
		2> experiments/vecernji.hr.200.check.verbose
	${CLASSIFIER} -i -v bs sr hr \
		< ../test/data/eval/dnevniavaz.ba.200.check \
		> experiments/dnevniavaz.ba.200.check.guess \
		2> experiments/dnevniavaz.ba.200.check.verbose


##--------------------------------------------------------
## 3-fold cross-validation to check robustness
## --> train with different folds = cross validation
## --> train with identical (parallel) folds = baseline
##--------------------------------------------------------

FOLDS = data/folds

experiments/3fold-cross.out:
	make -C data folds
	rm -f $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold1_sr.txt ${FOLDS}/fold2_hr.txt ${FOLDS}/fold3_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold1_sr.txt ${FOLDS}/fold3_hr.txt ${FOLDS}/fold2_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold2_sr.txt ${FOLDS}/fold1_hr.txt ${FOLDS}/fold3_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold2_sr.txt ${FOLDS}/fold3_hr.txt ${FOLDS}/fold1_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold3_sr.txt ${FOLDS}/fold1_hr.txt ${FOLDS}/fold2_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold3_sr.txt ${FOLDS}/fold2_hr.txt ${FOLDS}/fold1_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@


experiments/3fold-identical.out:
	make -C data folds
	rm -f $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold1_sr.txt ${FOLDS}/fold1_hr.txt ${FOLDS}/fold1_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold2_sr.txt ${FOLDS}/fold2_hr.txt ${FOLDS}/fold2_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@
	${CLASSIFIER} \
	-t "${FOLDS}/fold3_sr.txt ${FOLDS}/fold3_hr.txt ${FOLDS}/fold3_bs.txt" \
	-e "${TESTDATA}" -i sr hr bs >> $@



# experiments with increasing amounts of training data and different parameters

experiments/default/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime

experiments/max0/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 -b 0 sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime


experiments/score0.5/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 -c 0.5 sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime


experiments/min3max3score0.5/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 \
				-a 3 -b 3 -c 0.5 sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime


experiments/upper/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 -U sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime


experiments/notok/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 -S sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime

experiments/noalpha/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 -S -A sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime


experiments/plain/run.out: ${TRAINDATA} ${TESTDATA}
	mkdir -p $(shell dirname $@)
	${CLASSIFIER} -t "${TRAINDATA}" -e "${TESTDATA}" \
				-F 1000 -T 3000000 -L 2 -U -S -A sr hr bs \
				> $@ 2>$@.err
	grep acc $@ > $@.accuracy
	grep total $@ > $@.size
	grep 'training took:' $@.err > $@.traintime
	grep 'classification took:' $@.err > $@.testtime



# generate some plots using gnuplot

plot_accuracy:
	grep 'train with' experiments/default/run.out |\
	sed 's/train with ca //;s/tokens//;' > experiments/sizes
	wc -w < ../test/data/bhs_sr.txt >> experiments/sizes
	paste 	experiments/sizes_exakt_sum \
		experiments/default/run.out.accuracy \
		experiments/max0/run.out.accuracy \
		experiments/min3max3score0.5/run.out.accuracy |\
	sed 's/accuracy://g' > experiments/accuracy.data
	echo "set title 'learning curve';\
		set term postscript eps;\
		set terminal postscript enhanced;\
		set key bottom;\
		set log x;\
		set xrange [ 3000:8000000 ];\
		set output 'accuracy.eps';\
		set xlabel 'training size (all languages together)';\
		set ylabel 'overall accuracy';\
	plot 'experiments/accuracy.data' using 1:2 \
		title 'default settings' with lines,\
	     'experiments/accuracy.data' using 1:3 \
		title '{/Symbol a}=0' with lines,\
	     'experiments/accuracy.data' using 1:4 \
		title '{/Symbol a}={/Symbol b}=0, {/Symbol g}=0.5' \
		with lines" |\
	gnuplot


plot_accuracy_lang:
	grep '^hr ' experiments/default/run.out > experiments/accuracy.hr
	grep '^sr ' experiments/default/run.out > experiments/accuracy.sr
	grep '^bs ' experiments/default/run.out > experiments/accuracy.bs
	tail -n +2 experiments/sizes_exakt > experiments/sizes_exakt.tmp
	paste 	experiments/sizes_exakt.tmp \
		experiments/accuracy.hr \
		experiments/accuracy.sr \
		experiments/accuracy.bs \
		experiments/default/run.out.accuracy \
		 > experiments/accuracy_lang.data
	echo "set title 'learning curves (Blacklist classifier)';\
		set term postscript eps;set size 0.5,0.5;\
		set terminal postscript enhanced;\
		set key bottom;\
		set log x;\
		set xrange [ 1000:3000000 ];\
		set output 'accuracy_lang.eps';\
		set xlabel 'training size in tokens';\
		set ylabel 'accuracy';\
	plot \
	'experiments/accuracy_lang.data' using 3:20 \
		title 'all languages' with lines lw 5, \
	'experiments/accuracy_lang.data' using 2:8 \
		title 'Croatian' with lines lw 2,\
	'experiments/accuracy_lang.data' using 1:13 \
		title 'Serbian' with lines lw 2,\
	'experiments/accuracy_lang.data' using 3:18 \
		title 'Bosnian' with lines lw 2;" |\
	gnuplot


plot_accuracy_hr:
	echo "set title 'learning curves';\
		set term postscript eps;set size 0.7,0.7;\
		set terminal postscript enhanced;\
		set key bottom;\
		set log x;\
		set xrange [ 3000:8000000 ];\
		set output 'accuracy_hr.eps';\
		set xlabel 'training size in tokens';\
		set ylabel 'accuracy';\
	plot \
	'experiments/accuracy_lang.data' using 1:4 \
		title 'labeled as Croatian' with lines,\
	'experiments/accuracy_lang.data' using 1:3 \
		title 'labeled as Serbian' with lines,\
	'experiments/accuracy_lang.data' using 1:5 \
		title 'labeled as Bosnian' with lines;" |\
	gnuplot


plot_accuracy_sr:
	echo "set title 'learning curves';\
		set term postscript eps;set size 0.7,0.7;\
		set terminal postscript enhanced;\
		set key bottom;\
		set log x;\
		set xrange [ 3000:8000000 ];\
		set output 'accuracy_sr.eps';\
		set xlabel 'training size in tokens';\
		set ylabel 'accuracy';\
	plot \
	'experiments/accuracy_lang.data' using 1:8 \
		title 'labeled as Serbian' with lines,\
	'experiments/accuracy_lang.data' using 1:9 \
		title 'labeled as Croatian' with lines,\
	'experiments/accuracy_lang.data' using 1:10 \
		title 'labeled as Bosnian' with lines;" |\
	gnuplot


plot_accuracy_bs:
	echo "set title 'learning curves';\
		set term postscript eps;set size 0.7,0.7;\
		set terminal postscript enhanced;\
		set key top;\
		set key left;\
		set log x;\
		set xrange [ 3000:8000000 ];\
		set output 'accuracy_bs.eps';\
		set xlabel 'training size in tokens';\
		set ylabel 'accuracy';\
	plot \
	'experiments/accuracy_lang.data' using 1:15 \
		title 'labeled as Bosnian' with lines, \
	'experiments/accuracy_lang.data' using 1:13 \
		title 'labeled as Serbian' with lines,\
	'experiments/accuracy_lang.data' using 1:14 \
		title 'labeled as Croatian' with lines;" |\
	gnuplot



plot_accuracy_compare:
	tail -n +2 experiments/sizes_exakt > experiments/sizes_exakt.tmp
	paste 	experiments/sizes_exakt.tmp \
		experiments/default/run.out.accuracy |\
	sed 's/accuracy://g' > experiments/accuracy2.data
	echo "set title 'learning curves';\
		set term postscript eps;\
		set size 0.5,0.5;\
		set terminal postscript enhanced;\
		set key bottom;\
		set log x;\
		set xrange [ 1000:3000000 ];\
		set output 'accuracy_compare.eps';\
		set xlabel 'training size in tokens (avg per language)';\
		set ylabel 'accuracy';\
	plot 'experiments/accuracy2.data' using 3:4 \
		title 'Blacklist classifier' with lines lw 5,\
	     'experiments/nikola_bayes' using 1:2 \
		title 'Naive Bayes classifier' with lines lw 5;" |\
	gnuplot


plot_size: results.size.accuracy
	cut -f1 -d ' ' results.size > input.size
	paste 	input.size \
		experiments/MarkovChain.size \
		experiments/NaiveBayes.size \
		$< > experiments/accuracy.size
	echo "	set term postscript eps;\
		set size 0.5,0.5;\
		set terminal postscript enhanced;\
		set key bottom;\
		set output 'accuracy_size.eps';\
		set xlabel 'document size in tokens';\
		set ylabel 'accuracy';\
	plot 'experiments/accuracy.size' using 1:4 \
		title 'Blacklists' with lines lw 5,\
	     'experiments/accuracy.size' using 1:3 \
		title 'Naive Bayes' with lines lw 5, \
	     'experiments/accuracy.size' using 1:2 \
		title 'Markov Chain' with lines lw 5;" |\
	gnuplot