-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstig
executable file
·168 lines (139 loc) · 3.74 KB
/
stig
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
#!/usr/bin/perl
use strict;
use Getopt::Long;
use Pod::Usage;
use lib "/Users/kgaulton/stig/old/Lingua-Stem-0.84/lib/";
use PDL;
use Carp;
use Lingua::Stem qw(stem stem_in_place);
use vars qw(%Opt);
GetOptions(\%Opt, qw(q=s db=s o=s)) || pod2usage(1);
pod2usage(-verbose => 1, -message => "STIG - semantic text indexing of genes\n\nArguments:\n--q query document\n--db document database\n--o output file\n") if (!($Opt{db}&&$Opt{q}));
open(WO, "> query.words.out");
open(DO, "> corpus.words.out");
open(M, "> words.matrix");
my @db = split(/\n/,`ls $Opt{db}`);
my $i=0; while($i<scalar(@db)) { $db[$i]=~s/\s+//; $i++; }
my %sw; open(S, "< res/pubmed.stop"); while(<S>) { chomp($_); $sw{$_}=1; }
my %wdb; my $dc=0; my %wdf; my %ddb;
foreach my $doc (@db) {
open(D, "< $Opt{db}/$doc");
my $l=<D>;
my %w = doc_words($l,1);
foreach my $word (keys %w) { $wdb{$word}+=$w{$word}; $wdf{$word}++; }
%{$ddb{$doc}}= %w;
$dc++;
}
my $wc=0; my %index; my %rindex;
foreach my $word (keys %wdb) { $index{$word}=$wc; $rindex{$wc}=$word; $wc++; }
my @vd; my %di; $i=0;
foreach my $doc (@db) {
my $v = zeroes $wc;
foreach my $word (keys %{$ddb{$doc}}) {
my $idf = log($dc/$wdf{$word});
my $tw = $ddb{$doc}{$word}*$idf;
index ($v,$index{$word}).=$tw;
}
push @vd,norm $v;
my @pv = split(/\s+/,$v);
my $tv = join("\t",@pv);
$tv=~s/\[//; $tv=~s/\]//;
$tv=~s/^\s+//;
print M "$doc\t$tv\n";
$di{$i}=$doc; $i++;
}
open(Q, "< $Opt{q}");
my $ql; while(<Q>) { $ql.=$_; }
my %qw = doc_words($ql,2);
my $qv = zeroes $wc;
foreach my $word (keys %qw) {
if (exists $index{$word}) {
my $idf = log($dc/$wdf{$word});
my $tw = $qw{$word}*$idf;
$qw{$word}=$tw;
index ($qv,$index{$word}).=$tw;
}
}
push @vd,norm $qv;
my @pv = split(/\s+/,$qv);
my $tv = join("\t",@pv);
$tv=~s/\[//; $tv=~s/\]//;
print M "query\t$tv\n";
#my $n=scalar(@pv)+1;
foreach my $word (sort { $qw{$b} <=> $qw{$a} } keys %qw) { if (exists $wdb{$word}) { print WO "$word\t$qw{$word}\n"; } }
foreach my $word (sort { $wdb{$b} <=> $wdb{$a} } keys %wdb) { if (exists $wdb{$word}) { print DO "$word\t$wdb{$word}\n"; } }
open(R, "> run_lsa.R");
print R "w <- read.table(\"words.matrix\",sep=\"\\t\")\n";
print R "wm <- as.matrix(w[,2:ncol(w)])\n";
print R "library(lsa)\n";
print R "l <- lsa(wm)\n";
my $x=1;
while($x<19689) {
print R "cosine(l\$tk[$x,],l\$tk[19689,])\n";
$x++;
}
open(O, "> $Opt{o}");
my $g = `R --vanilla < run_lsa.R`;
my @gl = split(/\n/,$g);
my $y=0;
foreach my $ge (@gl) {
if ($ge=~/^\[/) { my @sp=split(/\s+/,$ge); print O "$di{$y}\t$sp[1]\n"; $y++; }
}
sub get_cos {
my %cos;
my $i=0;
foreach my $vector (@vd) {
my $cosine = cosine ( $vector, $_[0] );
$cos{$i}=$cosine;
$i++;
}
return %cos;
}
sub cosine {
my ($v1, $v2) = @_;
my $cos = inner ($v1, $v2);
return $cos->sclr();
}
sub doc_words {
my $d = $_[0];
my $t = $_[1];
my %dw; my $tw;
$d=~s/\// /g;
$d=~s/\(/ /g;
$d=~s/\)/ /g;
$d=~s/\./ /g;
$d=~s/-/ /g;
$d=~s/_/ /g;
$d=~s/#/ /g;
$d=~s/,/ /g;
$d=~s/:/ /g;
my @dwa = split(/\s+/, $d);
foreach my $word (@dwa) {
my $lcword = lc($word);
if (swords($lcword) && length($lcword)>2) {
if ($lcword =~ /^[a-z0-9]+$/) {
my @ta;
push(@ta,$lcword);
stem_in_place(@ta);
$dw{$ta[0]}++; $tw++;
}
}
}
my $max=0;
foreach my $word (keys %dw) { if ($dw{$word}>$max) { $max=$dw{$word}; } }
foreach my $word (keys %dw) {
#my $tf = $dw{$word}/$tw;
my $tf = 1+log($dw{$word});
#my $tf=1;
#my $tf;
#if ($t==1) { $tf = $dw{$word}; }
#if ($t==2) { $tf = .5 + (.5*$dw{$word}/$max); }
$dw{$word} = $tf;
}
return %dw;
}
sub swords {
if (exists $sw{$_[0]}) { return 0; }
if (length($_[0])<3) { return 0; }
else { return 1; }
}