-
Notifications
You must be signed in to change notification settings - Fork 1
/
makeMatricesXMLPNoTagging.pl
872 lines (735 loc) · 22.8 KB
/
makeMatricesXMLPNoTagging.pl
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
use File::Path;
use Lingua::Stem::En;
#use Lingua::EN::Tagger;
#use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);
#use Lingua::EN::Sentence qw( get_sentences add_acronyms );
use File::Basename;
use Getopt::Long;
# use XML::SAX::Expat;
# use XML::SAX::MyFooHandler;
# use MyHandler;
#use XML::SAX::XYZHandler;
if (!defined($PPos)) {
$PPos="NoPOS";
}
if (!defined($PDict)) {
$PDict="Dict";
}
if (!defined($PNeg)) {
$PNeg="BothNEG";
}
if (!defined($PBigrams)) {
$PBigrams="BiGram";
}
if (!defined($PStem)) {
$PStem="NoStem";
}
if (!defined($PMode)) {
$PMode="train";
}
if (!defined($dffilter)) {
$dffilter=0;
}
if (!defined($categoryAttribute)) {
$categoryAttribute="sentiment";
}
if (!(defined($dictionarydir))) {
$dictionarydir = "dictionaries";
}
if (!(defined($stopdir))) {
$stopdir = "$dictionarydir\\stoplists";
}
if (!(defined($model))) {
$model = "Electronics-.ConcatPOS.Dict.BothNEG.BiGram.NoStem-DocModel.txt";
}
if (!(defined($testmatrix))) {
$testmatrix = "test\\ElectronicsCameraAndPhotoB-.ConcatPOS.Dict.BothNEG.BiGram.NoStem-DocMatrix.txt";
}
#E:\Amazon\testxml\TestInput.xml.txt
#E:\Amazon\testxml\TestInput.xml.txt
#E:\Amazon\GroceriesTest
$PBigramsFlag=1;
$PDictFlag=1;
$sentenceMatrixFlag=0;
$maximumNegationWindow=4;
$stoplistFlag = 1;
$saveCleanFiles=0;
$numFolds=10;
$randomSeed=1000;
$result = GetOptions (
"dir=s" => \$dir, # directory or xmlfile
"Pos=s" => \$PPos, # part of speech
"Dict!" => \$PDictFlag,
"Neg=s" => \$PNeg,
"Bigrams!" => \$PBigramsFlag,
"Stem!" => \$PStemFlag,
"SentenceMatrix!" => \$sentenceMatrixFlag,
"Stoplist!" => \$stoplistFlag,
"SaveCleanSentenceFiles!" => \$saveCleanFiles,
"Mode=s" => \$PMode,
"DFFilter=i" => \$dffilter,
"CategoryAttribute=s" => \$categoryAttribute,
"Model=s" => \$model,
"NegWindow=i" => \$maximumNegationWindow,
"StoplistDirectory=s" => \$stopdir,
"DictionaryDirectory=s" => \$dictionarydir,
"NumFolds=i" => \$numFolds,
"RandomSeed=i" => \$randomSeed,
"Testmatrix=s" => \$testmatrix
);
if ($saveCleanFiles) {
$sentenceMatrixFlag=1;
}
if ($PBigramsFlag) {
$PBigrams = "BiGram"
}
else {
$PBigrams = "UniGram"
}
if ($PDictFlag) {
$PDict = "Dict"
}
else {
$PDict = "NoDict"
}
if ($PStemFlag) {
$PStem = "Stem"
}
else {
$PStem = "NoStem"
}
#$matrixdir = $dir . "\\matrices\\";
# my $tag = Lingua::EN::Tagger->new(longest_noun_phrase => 5,
# weight_noun_phrases => 0);
# start timer
$start = time();
$relaxHMMFlag = 0
; #"Relax the Hidden Markov Model: this may improve accuracy for uncommon words, particularly words used polysemously"
$paramSuffix="-";
$PPos="NoPOS";
if ($PPos eq "NoPOS") {
$POSFlag=0;
$concatenatePOSFlag=0;
$paramSuffix=$paramSuffix.".".$PPos;
}
elsif ($PPos eq "POS") {
$POSFlag=1;
$concatenatePOSFlag=0;
$paramSuffix=$paramSuffix.".".$PPos;
}
elsif ($PPos eq "ConcatPOS") {
$POSFlag=1;
$concatenatePOSFlag=1;
$paramSuffix=$paramSuffix.".".$PPos;
}
if ($PDict eq "NoDict") {
$lexicalFlag = 0;
$paramSuffix=$paramSuffix.".".$PDict;
}
elsif ($PDict eq "Dict") {
$lexicalFlag = 1;
$paramSuffix=$paramSuffix.".".$PDict;
}
if ($PNeg eq "NoNEG") {
$negationPropagationFlag = 0;
$negationFeatureFlag = 0;
$removeNegatedFlag=0;
$paramSuffix=$paramSuffix.".".$PNeg;
}
elsif ($PNeg eq "NEG") {
$negationPropagationFlag = 0;
$negationFeatureFlag = 1;
$removeNegatedFlag=0;
$paramSuffix=$paramSuffix.".".$PNeg;
}
elsif ($PNeg eq "PropagNEG") {
$negationPropagationFlag = 1;
$negationFeatureFlag = 0;
$removeNegatedFlag=0;
$paramSuffix=$paramSuffix.".".$PNeg;
}
elsif ($PNeg eq "BothNEG") {
$negationPropagationFlag = 1;
$negationFeatureFlag = 1;
$removeNegatedFlag=0;
$paramSuffix=$paramSuffix.".".$PNeg;
}
elsif ($PNeg eq "RemoveNEG") {
$negationPropagationFlag = 1;
$negationFeatureFlag = 1;
$removeNegatedFlag=1;
$paramSuffix=$paramSuffix.".".$PNeg;
}
if ($PBigrams eq "UniGram") {
$PBigramsFlag = 0;
$paramSuffix=$paramSuffix.".".$PBigrams;
}
elsif ($PBigrams eq "BiGram") {
$PBigramsFlag = 1;
$paramSuffix=$paramSuffix.".".$PBigrams;
}
# Works now $PStem="NoStem"; #Stemmer does not work!!!
if ($PStem eq "NoStem") {
$stemFlag = 0;
$paramSuffix=$paramSuffix.".".$PStem;
}
elsif ($PStem eq "Stem") {
$stemFlag = 1;
$paramSuffix=$paramSuffix.".".$PStem;
}
$paramSuffix=$paramSuffix."-";
if ($PMode eq "generic") {
$paramSuffix="";
$saveCleanFiles=1;
$sentenceMatrixFlag=1;
}
if ($PMode eq "train") {
$trainModelsFlag=1; } else {
$trainModelsFlag=0;
}
if ($PMode eq "test") {
$testModelsFlag=1; } else {
$testModelsFlag=0;
}
if ($PMode eq "traintest") {
$traintestModelsFlag=1; } else {
$traintestModelsFlag=0;
}
if ($PMode eq "score") {
$scoreModelsFlag=1; } else {
$scoreModelsFlag=0;
}
if ($PMode eq "xvalidation") {
$xvalidationModelsFlag=1; } else {
$xvalidationModelsFlag=0;
}
if ( $dir =~ /^(.+)\\(.+?)$/ig ) {
#$outputdir=$1."\\";
$outputdir = $1;
$domain = $2;
}
#my $p = Lingua::EN::Tagger->new( stem => $stemFlag, relax => $relaxHMMFlag );
if ($stoplistFlag) {
$stopdir = "$dictionarydir\\stoplists";
%stopwords = getHashFromDirFilenameStemFlag($stopdir, $stemFlag);
}
if ($lexicalFlag) {
%lookup = getHashFromDirFilenameStemFlag("$dictionarydir\\dictionaries", $stemFlag);
}
%negation = getHashFromDirFilenameStemFlag("$dictionarydir\\negation", $stemFlag);
if (-d $dir) {
$matrixdir = $dir . "\\matrices";
mkdir ($matrixdir,1);
if ($trainModelsFlag) {
$modeldir = $dir . "\\models";
mkdir ($modeldir,1);
}
$fcleanDoc = "$dir\\$domain" . "Doc" . "clean.txt";
#$fcleanPgraph = "$dir\\$domain" . "Pgraph" . "clean.txt";
$fcleanSentence = "$dir\\$domain" . "Sentence" . "clean.txt";
if ($saveCleanFiles) {
open( CLEANDOC, ">$fcleanDoc" ) or die "can't open $fcleanDoc: $!";
#open( CLEANPGRAPH, ">$fcleanPgraph" ) or die "can't open $fcleanPgraph: $!";
if ($sentenceMatrixFlag) {
open( CLEANSENTENCE, ">$fcleanSentence" )
or die "can't open $fcleanSentence: $!";
}
}
$fmatrixDoc = "$matrixdir\\$domain" .$paramSuffix. "Doc" . "Matrix.txt";
#$fmatrixPgraph = "$outputdir\\$domain" .$paramSuffix . "Pgraph" . "Matrix.txt";
$fmatrixSentence = "$matrixdir\\$domain" .$paramSuffix . "Sentence" . "Matrix.txt";
if ($trainModelsFlag) {
$fmodelDoc = "$modeldir\\$domain" .$paramSuffix. "Doc" . "Model.txt";
$fmodelSentence = "$modeldir\\$domain" .$paramSuffix . "Sentence" . "Model.txt";
}
#print $fmatrixDoc, "\n";
#print $fmatrixSentence, "\n";
#print $fmatrixDoc, "\n";
open( MATDOC, ">$fmatrixDoc" ) or die "can't open $fmatrixDoc: $!";
#open( MATPGRAPH, ">$fmatrixPgraph" ) or die "can't open $fmatrixPgraph: $!";
if ($sentenceMatrixFlag) {
open( MATSENTENCE, ">$fmatrixSentence" ) or die "can't open $fmatrixSentence: $!";
}
@classes = ( "positive", "negative", "neutral", "unknown" );
opendir( DIR, "$dir" );
@files2 = readdir(DIR);
closedir(DIR);
foreach $f (@files2) {
next if $f =~ /^\.\.?$/;
next if $f =~ /\.txt$/;
$missing = 1;
foreach $c (@classes) {
#print $f .":".$c."\n";
if ( $f eq $c ) { $missing = 0; }
} #foreach $c
if ($missing) { next; }
$class = $f;
# print $class ."\n";
$inputdir= $dir . "\\" . $class;
opendir( DIR, "$inputdir" );
@files = readdir(DIR);
closedir(DIR);
foreach $f (@files) {
next if $f =~ /^\.\.?$/;
next if $f =~ /^\.svn$/;
$file=$inputdir."\\".$f;
$text="";
open( INPUTFILE, "<$file" ) or die "can't open $file: $!";
$file=~s/\s+/\_\_\_/g; #replace spaces in file id with ___ to avoid problems later when splitting
while ( defined( $line = <INPUTFILE> ) ) {
$text=$text.$line;
}
close INPUTFILE;
processDoc();
} #file
} #classes
} #kind of file
elsif (-e $dir) {
my($filename, $directory) = fileparse($dir);
chop $directory;
$xmlfile=$dir;
$dir=$directory;
$domain=$filename;
$fcleanDoc = "$dir\\$domain" . "Doc" . "clean.txt";
#$fcleanPgraph = "$dir\\$domain" . "Pgraph" . "clean.txt";
$fcleanSentence = "$dir\\$domain" . "Sentence" . "clean.txt";
if ($saveCleanFiles) {
open( CLEANDOC, ">$fcleanDoc" ) or die "can't open $fcleanDoc: $!";
#open( CLEANPGRAPH, ">$fcleanPgraph" ) or die "can't open $fcleanPgraph: $!";
if ($sentenceMatrixFlag) {
open( CLEANSENTENCE, ">$fcleanSentence" )
or die "can't open $fcleanSentence: $!";
}
}
$matrixdir = $dir . "\\matrices";
mkdir ($matrixdir,1);
if ($trainModelsFlag) {
$modeldir = $dir . "\\models";
mkdir ($modeldir,1);
}
$fmatrixDoc = "$matrixdir\\$domain" .$paramSuffix. "Doc" . "Matrix.txt";
#$fmatrixPgraph = "$outputdir\\$domain" .$paramSuffix . "Pgraph" . "Matrix.txt";
$fmatrixSentence = "$matrixdir\\$domain" .$paramSuffix . "Sentence" . "Matrix.txt";
if ($trainModelsFlag) {
$fmodelDoc = "$modeldir\\$domain" .$paramSuffix. "Doc" . "Model.txt";
$fmodelSentence = "$modeldir\\$domain" .$paramSuffix . "Sentence" . "Model.txt";
}
#print $fmatrixDoc, "\n";
#print $fmatrixSentence, "\n";
#print $fmatrixDoc, "\n";
open( MATDOC, ">$fmatrixDoc" ) or die "can't open $fmatrixDoc: $!";
#open( MATPGRAPH, ">$fmatrixPgraph" ) or die "can't open $fmatrixPgraph: $!";
if ($sentenceMatrixFlag) {
open( MATSENTENCE, ">$fmatrixSentence" ) or die "can't open $fmatrixSentence: $!";
}
#my $handler = XML::SAX::XYZHandler->new();
# my $p = XML::SAX::ParserFactory->parser(Handler => $handler);
# $p->parse_uri("foo.xml");
# or $p->parse_string("<foo/>") or $p->parse_file($fh);
#my $h = XML::SAX::MyFooHandler->new;
# my $h = start_element;
# my $p = XML::SAX::Expat->new(Handler => $h);
# $p->parse_file($xmlfile);
#Logic for handling events goes into the handler class (MyHandler in this example), which you write:
# in MyHandler.pm
# $data is hash with keys like Name and Attributes
# ...
open( XML, "<$xmlfile" ) or die "can't open $xmlfile: $!";
$inDocumentFlag=0;
$xmltext="";
while( $line=<XML> ) {
if ($line=~/\<document\>(.*?)/ig) {
$xmltext=$1;
$inDocumentFlag=1;
}elsif ($line=~/(.*?)\<\/document\>/ig) {
$xmltext=$xmltext.$1;
$inDocumentFlag=0;
parseDoc();
processDoc();
$xmltext="";
}elsif ($inDocumentFlag) {
$xmltext=$xmltext.$line;
}
}
} #xmlfile
else {
generateHelp();
print $help;
exit;
}
if ($trainModelsFlag) {
$modelcommand="start /b /realtime perl trainmodel.pl \"$fmatrixDoc\" \"$fmodelDoc\" svmperftfidf $dffilter";
print $modelcommand."\n";
$modelresult=runCommand($modelcommand);
#print $modelresult."\n";
if ($sentenceMatrixFlag) {
$modelcommand="start /b /realtime perl trainmodel.pl \"$fmatrixSentence\" \"$fmodelSentence\" svmperftfidf $dffilter";
#print $modelcommand."\n";
$modelresult=runCommand($modelcommand);
#print $modelresult."\n";
}
}
if ($testModelsFlag) {
$testcommand="start /wait /b /high perl testmodel.pl \"$fmatrixDoc\" \"$model\" ";
$testresult=runCommand($testcommand);
if ($sentenceMatrixFlag) {
$testcommand="start /wait /b /high perl testmodel.pl \"$fmatrixSentence\" \"$model\" ";
$testresult=runCommand($testcommand);
}
}
if ($traintestModelsFlag) {
$testcommand="start /wait /b /high perl traintest.pl \"$fmatrixDoc\" \"$testmatrix\" \"$model\" ";
$testresult=runCommand($testcommand);
if ($sentenceMatrixFlag) {
$testcommand="start /wait /b /high perl traintest.pl \"$fmatrixSentence\" \"$testmatrix\" \"$model\" ";
$testresult=runCommand($testcommand);
}
}
if ($scoreModelsFlag) {
$testcommand="start /wait /b /high perl runmodel.pl \"$fmatrixDoc\" \"$model\" ";
$testresult=runCommand($testcommand);
if ($sentenceMatrixFlag) {
$testcommand="start /wait /b /high perl runmodel.pl \"$fmatrixSentence\" \"$model\" ";
$testresult=runCommand($testcommand);
}
}
if ($xvalidationModelsFlag) {
$testcommand="start /wait /b /high perl xvalidationOnMatrix.pl \"$fmatrixDoc\" $numFolds $randomSeed ";
$testresult=runCommand($testcommand);
if ($sentenceMatrixFlag) {
$testcommand="start /wait /b /high perl xvalidationOnMatrix.pl \"$fmatrixSentence\" $numFolds $randomSeed ";
$testresult=runCommand($testcommand);
}
}
# end timer
$end = time();
# report
#print "\nTime taken was ", ( $end - $start ), " seconds";
print $fmatrixDoc;
exit;
sub processDoc {
undef %doccount;
@docfeatures=();
$sentencecount=0;
# get the sentences
#$sentences=get_sentences($text); #slow oh so slow...
#foreach my $text (@$sentences) {
@sentences=split(/[\!\?\.]\s+/g,$text); #fast
foreach $text (@sentences) {
if ($saveCleanFiles) {
$sentencetext=$text;
$sentencetext=~s/\n+/ /g;
if ($sentenceMatrixFlag) {
print CLEANSENTENCE "$sentencetext\n";
}
}
if ($negationPropagationFlag) {
$negcounter=99999;
}
undef %count;
undef @features;
undef $bigram;
$text =~ s/<[^>]*>/ /gs; #remove tags
$text =~ s/&.*?\s/ /gs; #remove escapes
$text=~s/[^a-zA-Z0-9'-]/ /ig; #keep only alphanumeric, dash and apostrophe
$text=~s/\s+/ /ig;
$text=lc($text);
$prev="";
if ($POSFlag) {
#my $tagged_text = $p->add_tags($text);
#my %word_list = $p->get_words( $text );
$readable = $tagged_text;
$readable =~ s/<\p{IsLower}+>([^<]+)<\/(\p{IsLower}+)>/$1\/\U$2/go;
@wordspos=split (/\s+/, $readable);
@words=();
@pos=();
foreach $word (@wordspos) {
( $word, $pos ) = split( /\//, $word );
push (@words, $word);
push (@poss, $pos);
}
}
else
{
@words=split (/\s+/, $text);
}
if ($stemFlag) {
#$words=[\@words];
my $wordsForStem=[\@words];
$stemmed_words = Lingua::Stem::En::stem({ -words => $wordsForStem,
-locale => 'en',
-exceptions => \%exceptions,
});
}
foreach ($in=0;$in<scalar(@words);$in++) {
if ($POSFlag) {
#( $word, $pos ) = split( /\//, $word );
$pos=@poss[$in]
}
else {$pos="";}
$word=@words[$in];
if ($word=~/^[-']$/i) {next;}
if ( ($stoplistFlag) && ( exists( $stopwords{ lc($word) } ) ) )
{
next;
}
$addWordFlag=1;
if ($PBigramsFlag) {
if (!($prev eq "")) {
$bigram=$prev."_".$word;
#push (@features,$bigram); #Added later so that it can be removed by negation
}
$prev=$word;
}
if ( $negationFeatureFlag) {
if ( ( exists( $negation{$word} ) ) ) {
push( @features, "D_NEGATION" );
}
} #$negationFeatureFlag
if ($negationPropagationFlag) {
if ( ( exists( $negation{$word} ) ) ) {
$negcounter=0;
}
if (($negcounter<$maximumNegationWindow)&& ($negcounter>0)) {
push (@features,"NEG_".$word);
if ($PBigramsFlag) {
push (@features,"NEG_".$bigram);
}
if (($POSFlag==1)&&($concatenatePOSFlag==0)) {
push (@features,"NEGPOS_".$pos);
}
if (($POSFlag)&&($concatenatePOSFlag)) {
push (@features,"NEGCONCATPOS_".$word."_".$pos);
}
if ($lexicalFlag){
if ( ( exists( $lookup{$word} ) ) ) {
@tmp6 = split( ",", $lookup{$word} );
for ($i=0;$i<scalar(@tmp6);$i++){
$tmp6[$i]="NEG_".$tmp6[$i];
}
push( @features, @tmp6 );
}
} #$lexicalFlag
if ($removeNegatedFlag) {
$addWordFlag=0;
}
}
$negcounter++;
}
if ($addWordFlag) {
push (@features,$word);
if (($PBigramsFlag)&&(!($prev eq ""))&& ($bigram)){
push (@features,$bigram);
}
if (($POSFlag==1)&&($concatenatePOSFlag==0)) {
push (@features,"POS_".$pos);
}
if (($POSFlag)&&($concatenatePOSFlag)) {
push (@features,"CONCATPOS_".$word."_".$pos);
}
if ($lexicalFlag){
if ( ( exists( $lookup{$word} ) ) ) {
@tmp6 = split( ",", $lookup{$word} );
push( @features, @tmp6 );
}
} #$lexicalFlag
}
} #words
@features = grep { !($_ =~/^\s*$/g)} @features;
foreach $f (@features) {
$count{$f}++;
}
$sentencecount++;
if ($sentenceMatrixFlag) {
print MATSENTENCE "$file-$sentencecount $class ";
print MATSENTENCE join(" ", map{"$_ $count{$_}"} keys(%count))."\n";
}
push (@docfeatures, @features);
} #$sentences
$text=$text;
foreach $f (@docfeatures) {
$doccount{$f}++;
}
print MATDOC "$file $class ";
print MATDOC join(" ", map{"$_ $doccount{$_}"} keys(%doccount))."\n";
}
sub getHashFromDirFilenameOLD {
my $dir = shift;
my %ret = ();
my $value;
my $key;
opendir( DIR, $dir );
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
next if $file =~ /^\.\.?$/; # skip . and ..
next if $file =~ /^\.svn$/; # skip . and ..
# print "$file\n";
my $filename = "$dir\\$file";
open( INPUTFILE2, "<$filename" ) or die "can't open $filename: $!";
while ( defined( $line = <INPUTFILE2> ) ) {
chomp $line;
my @words = split( /\s+/, $line );
if ( scalar(@words) > 0 ) {
$key = @words[0];
}
else {
die "Empty line in $filename.";
}
$value = $file;
if ( $value =~ /(.*?)\..*?/i ) {
$value = $1;
}
# if ($key eq "this") {
# print "this";
# }
if ( !( exists $ret{$key} ) ) {
$ret{$key} = "D_" . $value;
}
else {
unless ( $ret{$key} eq "D_" . $value ) {
$ret{$key} = $ret{$key} . "," . "D_" . $value;
}
}
}
}
return %ret;
}
sub getHashFromDirFilenameStemFlag {
my $dir = shift;
my $stemFlag=shift;
my %ret = ();
my $value;
my $key;
my @keys;
opendir( DIR, $dir );
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
next if $file =~ /^\.\.?$/; # skip . and ..
next if $file =~ /^\.svn$/; # skip . and ..
# print "$file\n";
my $filename = "$dir\\$file";
undef @keys;
open( INPUTFILE2, "<$filename" ) or die "can't open $filename: $!";
while ( defined( $line = <INPUTFILE2> ) ) {
chomp $line;
my @words = split( /\s+/, $line );
if ( scalar(@words) > 0 ) {
$key = @words[0];
push (@keys, $key);
}
else {
die "Empty line in $filename.";
}
}
$value = $file;
if ( $value =~ /(.*?)\..*?/i ) {
$value = $1;
}
if ($stemFlag) {
#$words=[\@words];
my $wordsForStem=[\@keys];
$stemmed_words = Lingua::Stem::En::stem({ -words => $wordsForStem,
-locale => 'en',
-exceptions => \%exceptions,
});
}
foreach $key (@keys){
# if ($key eq "this") {
# print "this";
# }
if ( !( exists $ret{$key} ) ) {
$ret{$key} = "D_" . $value;
}
else {
unless ( $ret{$key} eq "D_" . $value ) {
$ret{$key} = $ret{$key} . "," . "D_" . $value;
}
}
}
}
return %ret;
}
sub runRulesOnDocument {
my $text=shift;
$text=~s/\,/ [comma]/ig;
$text=~s/\s*\:\s*/ [colon] /ig;
$text=~s/\s*\;\s*/ [semicolon] /ig;
$text=~s/\s*\"\s*/ [quote] /ig;
$text=~s/\s+\d+.*?\s+/ [num] /ig;
return $text;
}
sub parseDoc { # method names are specified by SAX
# print $xmltext;
$text="error";
$id="error";
$class="error";
if ($xmltext=~/\>(.*?)\</m){
$body=$1;
}
if ($xmltext=~/\<body\>(.*?)\<\/body\>/ism){
$body=$1;
}
$text=$body;
if ($xmltext=~/\<id\>(.*?)\<\/id\>/ism) {
$id=$1;
}
$file=$id;
my $regex="\\<category\\s+attribute=\\s*[\\'\\\"]\\s*".$categoryAttribute."s*[\\'\\\"]\\s*.*?\\>(.*?)\\<\\/category\\>";
if ($xmltext=~/$regex/ism){
$category=$1;
}
$class=$category;
}
sub start_element { # method names are specified by SAX
my ($self, $data) = @_;
}
sub generateHelp {
$help="";
$help=$help."This script takes a directory with documents or an xml file and generates a feature matrix."."\n";
$help=$help."Standard output returns the filename and the full path to the generated feature matrix."."\n";
$help=$help."SVM models and evaluation files are optionally generated in the models and matrices directories."."\n";
$help=$help."Parameters:"."\n";
$help=$help."-Dir directory or xmlfile (The only mandatory parameter, all others have defaults.)"."\n";
$help=$help."-Pos part of speech (ConcatPOS, POS, NoPOS). ConcatPos"."\n";
$help=$help."-Dict use of dictionary (flag). Dict"."\n";
$help=$help."-DictionaryDirectory directory containing dictionary files. dictionaries"."\n";
$help=$help."-Neg negation handling (BothNEG PropagNEG NEG NoNEG). BothNEG"."\n";
$help=$help."-NegWindow negation propagation window (integer). 4"."\n";
$help=$help."-Bigrams bigrams (flag). Bigrams"."\n";
$help=$help."-Stem stem (flag). NoStem"."\n";
$help=$help."-SentenceMatrix generate sentence matrix in addition to the document matrix (flag). NoSentenceMatrix"."\n";
$help=$help."-Stoplist use of stoplist (flag). Stoplist"."\n";
$help=$help."-StoplistDirectory directory containing stoplist files. dictionaries\\stoplists"."\n";
$help=$help."-SaveCleanSentenceFiles save sentences in a single file (flag). NoSaveCleanSentenceFiles"."\n";
$help=$help."-Mode train or test mode (train, test, traintest, score, xvalidation, none). train"."\n";
$help=$help."-DFFilter use only features that occur in at least as many documents (integer). 0"."\n";
$help=$help."-CategoryAttribute category attribute that represents the category if input is XML. sentiment"."\n";
$help=$help."-Model model for testing. test\\Electronics-.ConcatPOS.Dict.BothNEG.BiGram.NoStem-DocModel.txt"."\n";
$help=$help."-Testmatrix matrix for testing. test\\ElectronicsCameraAndPhotoB-.ConcatPOS.Dict.BothNEG.BiGram.NoStem-DocMatrix.txt"."\n";
$help=$help."-numFolds number of cross validation folds (integer). 10"."\n";
$help=$help."-randomSeed random seed for cross validation (integer). 1000"."\n";
}
sub runCommand {
my $command=shift;
my $exe;
my $subst;
my $pl;
my $ret;
if ($command=~/(perl\s+((.*?).pl))/ig) {
$subst=$1;
$pl=$2;
$exe=$3.".exe";
if (!(-e $exe)) {
if (-e "perl2exe\\perl2exe.exe") {
$ret=`perl2exe\\perl2exe.exe $pl`;
}
}
if (-e $exe) {
$command=~s/$subst/$exe/ig;
}
}
$ret=`$command`;
return $ret;
}