-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakeSVMLightFile.pl
220 lines (188 loc) · 5.2 KB
/
MakeSVMLightFile.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
use Config::Simple;
use Getopt::Long;
use File::Copy;
#test command line
#blah "C:\Users\marko_krema\Documents\Visual Studio 2010\Projects\SentimentTest1\SentimentTest1\webSentenceunknownMatrix.txt" "E:\Amazon\m\fullmatrix\global5matrix.txtWordList-149954-IDFNumDoc-246631.txt" 0 "C:\Users\marko_krema\Documents\Visual Studio 2010\Projects\SentimentTest1\SentimentTest1\webSentenceunknownMatrixSVM.txt"
#blah fullmatrix\global5matrix.txt fullmatrix\global5matrix.txtWordList-149954-IDFNumDoc-246631.txt 0
$filename=shift;
$wlfilename=shift;
$idfthreshold=shift;
$outputIDF=shift;
$outputMAX=shift;
$missingword=0;
$duplicateword=0;
#No filenames or domains in svmlight label is +1 -1.
if (length($outputIDF)<2) {$outputIDF=$filename."SVMLightIDF.txt";}
if (length($outputMAX)<2) {$outputMAX=$filename."SVMLightMAX.txt";}
$output5=$filename."SVMLightNumericSubs.txt";
$output2=$filename."FilenameSVMLight.txt";
#$output3=$filename."CategorySVMLight.txt";
$output4=$filename."DomainSVMLight.txt";
$output6=$filename."WordsSVMLight.txt";
$outputaml=$output.".aml";
undef %w; #word numeric labels
undef %wm; #word max frequency
undef %widf; #word idf
$c=0;
$wordlinect=0;
$wc=1;
open(WLINPUTFILE, "< $wlfilename") or die "can't open $wlfilename: $!";
while (defined ($line = <WLINPUTFILE>)) {
chomp $line;
$wordlinect++;
if ($wordlinect==1) {
$doccount=$line;
}
else {
@f=split(/\s+/,$line);
$wline=$f[0];
$mline=$f[1];
$maxline=$f[2];
if (!($mline>$idfthreshold)) {next;} #skip words below the idf threshold
if (!($w{$wline}>0)) {
$w{$wline}=$wc;
$wc++;
}
$wm{$wline}=$maxline;
if (!(exists $widf{$wline})){
$widf{$wline}=$mline;
} else {
$duplicateword=1;
die "Duplicate word: $wline in $wlfilename";}
} #else
} #while
$wordcount=$wc;
$wcount=keys( %w );
$wmcount=keys( %wm );
$idfcount=keys( %widf );
open(INPUTFILE, "< $filename") or die "can't open $filename: $!";
open(OUTPUTFILEIDF, "> $outputIDF") or die "can't open $outputIDF: $!";
open(OUTPUTFILEMAX, "> $outputMAX") or die "can't open $outputMAX: $!";
$wc=0;
while (defined ($line = <INPUTFILE>)) {
($linenocomment,$trash)=split(/#/,$line);
@f=split(/\s+/,$linenocomment);
$file=@f[0];
$cat=lc(@f[1]);
$dom="";
if ($file=~/.*\\(.*?)\\/i) {
$dom=lc($1);
}
#$newline="file:$File Domain:$dom Category:$cat ";
#fixing label for svmlight
if (lc($cat) eq lc("positive")) {
$catlight=1;
}
else {
if (lc($cat) eq lc("negative")) {
$catlight=-1;
}
else
{
$catlight=lc($cat);
}
} #else
$newline="$catlight ";
$maxline="$catlight ";
$newold="$catlight ";
#$newline="file:$File Domain:$dom Category:$cat ";
$postlinesvmlight=" #Domain:$dom Category:$cat file:$file";
$postlinesvmlight="";
$newline5="1:$cf{$file} 2:$cd{$dom} 3:$cc{$cat} ";
$len=@f;
$numWords=($len-2.0)/2.0;
%incvalues=();
%maxvalues=();
for ($i=2;$i<$len;$i=$i+2)
{
$l=@f[$i];
if (!($w{$l}>0)) {
$missingword=1;
# die "Word $l not in $wlfilename while processing $filename and outputting $output.\n$command\n file=$file, cat=$cat, dom=$dom len=$len\n";
next;
#print AML "<attribute name =\"$l\" valuetype =\"integer\" blocktype =\"single_value\" />\n";
} #if
$v=@f[$i+1];
#$tfidfvalue =$v/$wd{$l};
#scale by total number of occurances of that word
#$sv=$v/$wf{$l};
#$sv=$v;
#$sv=$tfidfvalue;
#scale by maximum frequency of that word 0 ..1
#$lfreq=$wm{$l};
#$sv=$v/$lfreq;
$tfidfv=$widf{$l};
if ($l=~/^GLOB_/)
{
$sv=$v/$numWords;
}
else
{
$sv=$v*log(($doccount*1.0)/$tfidfv);
}
#$msv=$v*1.00/$wm{$l};
$msv=$v/$numWords;
$attindex=$w{$l};
$incvalues{$attindex}=$sv;
$maxvalues{$attindex}=$msv;
if ($attindex > $wcount) {
print $attindex . " " . $l;
}
#$newlineold=$newlineold.$attindex.":".$sv." ";
#$newline5=$newline5.($attindex+3).":".$v." ";
}
$l2norm=0.0000000001;
foreach $key (sort {$a <=> $b} (keys(%incvalues))) {
$l2norm=$l2norm+($incvalues{$key}*$incvalues{$key});
}
$l2norm=sqrt($l2norm);
foreach $key (sort {$a <=> $b} (keys(%incvalues))) {
$incvalues{$key}=$incvalues{$key}/$l2norm;
$newline=$newline.$key.":".$incvalues{$key}." ";
$maxline=$maxline.$key.":".$maxvalues{$key}." ";
}
#print ".";
chop $newline;
chop $maxline;
#chop $newline5;
#chop $newlineold;
#$newline=$newline."\n";
$c++;
print OUTPUTFILEIDF "$newline $postlinesvmlight\n";
print OUTPUTFILEMAX "$maxline $postlinesvmlight\n";
if (($c%10000)==0) { print "$c "; }
# print OUTPUTFILE5 "$newline5\n";
} #while
close (INPUTFILE);
close(OUTPUTFILEIDF);
close(OUTPUTFILEMAX);
close(OUTPUTFILE5);
close(OUTPUTFILE2);
close(OUTPUTFILE3);
close(OUTPUTFILE4);
close(OUTPUTFILE6);
close(AML);
print "$output: $c lines processed. Word Count:$wc.\n";
exit;
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;
}