#! /usr/bin/perl


$\ = "\n";
$| = 1;


open (IN, "query");   ### open the file containing the query terms
open (OUT, ">results");

while ($queryterm=<IN>){
 chop $queryterm;
 push @qterms, $queryterm;
 };

close(IN);

### Get the number of postings for each query term

for ($q=0; $q<@qterms; $q++){

open (BSS, "| i1+ -silent > t1"); ### Open communication with BSS

 print BSS "ch ft_96";  
 print BSS "f t=$qterms[$q]";
close (BSS);

open (TEMP, "t1");
 while ($in = <TEMP>){
  chop $in;
  $in =~ s/^S\d+\snp=(\d+).+$/$1/;
  push @nopos, $in;
 };
close(TEMP);
};

### Weight query terms

open (BSS, "| i1+ -silent > t2");

print BSS "ch ft_96";  
for ($k=0; $k<@nopos; $k++){
print BSS "w fn=0 n=$nopos[$k]";
};

close(BSS);

open (TEMP, "t2");
 while ($in = <TEMP>){
 chop $in;
 push @weights, $in;
};
close(TEMP);


### Search using weighted operator bm2500

open (BSS, "| i1+ -silent > t3");

print BSS "ch ft_96";  
for ($l=0; $l<@weights; $l++){
 $query = $query . " s=" . $l . " w=" . $weights[$l];
 print BSS "f t=$qterms[$l]";
};

print BSS "f $query op=bm2500 k1=1.2 bm25b=0.75";
print BSS "s n=1000";

close(BSS);

open (TEMP, "t3");
while ($in = <TEMP>){
 chop $in;
 print OUT $in;
};
close(TEMP);
close(OUT);

$rm = `rm t1 t2 t3`;

