#!/usr/bin/perl -w

# -----------------------------------------------------------------------
#  Classify article abstracts as belonging to one class or another by
#   word frequencies
# -----------------------------------------------------------------------
# Edward Marcotte                   UCLA                        6/21/2000
# -----------------------------------------------------------------------
# score_abstracts.pl Abstract_file Discriminating_word_file > outputfile
# -----------------------------------------------------------------------

# program files
# -------------
$abstracts = $ARGV[0];       # pass a file of abstract lines from Medline entries
$wordlist = $ARGV[1];        # pass a file of discriminating words

# preferred Abstract_file format:
# for each entry:
# MEDLINE_PMID Text of abstract all on one line...
# -------------------------------------------------------------------------

# various program variables/hashes/arrays/etc...
# ----------------------------------------------
$abstractcnt = 0;  # index for abstracts
$temp = "";
$testwordcnt=0;

# Read in dsicriminating words & freqs
# ------------------------------------

open (WORDS,$wordlist) or die "Can't open discriminating word list $wordlist:$!\n";
LINEW:while (<WORDS>)
  {
    $line = $_;
    chomp($line);
    (@A) = split(" ",$line);
    $testwordfreq_class1{$A[0]} = $A[4];
    $testwordfreq_class2{$A[0]} = $A[6];
    $testwordcnt++;
  }
close (WORDS);
print "$testwordcnt words with which to test abstracts\n";

# Read in Medline abstracts & scan for discriminating words
# ---------------------------------------------------------

print "Now parsing abstracts...\n";
open (ABSTRACTS,$abstracts) or die "Can't open abstracts file:$!\n";
LINE2:while (<ABSTRACTS>)
   {
    $line = $_;
    chomp($line);
    $line = uc($line);
    (@A) = split("\t",$line);
    next LINE2 if ($#A == 0);
    # remove all punctuation; replace by spaces
    # -----------------------------------------
    $length = length($line);
    $temp = "";
    for ($i=0; $i<$length; $i++)
     {
       $tempi = substr($line,$i,1);
       if (($tempi eq ".") or ($tempi eq ",") or ($tempi eq ";") or ($tempi eq "(") or ($tempi eq ")") or ($tempi eq ":") or ($tempi eq "?") or (
$tempi eq "/") or ($tempi eq "|") or ($tempi eq "[") or ($tempi eq "]") or ($tempi eq "\"") or ($tempi eq "<") or ($tempi eq ">") or ($tempi eq "
%") or ($tempi eq "\$") or ($tempi eq "#") or ($tempi eq "*") or ($tempi eq "&") or ($tempi eq "^"))
        {$temp .= " ";}
       else
        {$temp .= $tempi;}
     }
    $edited_abstract = $temp;
    (@A) = split (" ", $temp);
    
    $temp = shift(@A);
    $score[$abstractcnt] = 0;
    %abswordfreq = ();
    $abswordcnt=0;
    
      LINE3A:foreach $A (@A)
      {
	  next LINE3A if (($A eq "JOHN") or ($A eq "WILEY") or ($A eq "SONS") or ($A eq "COPYRIGHT") or ($A eq "LTD") or ($A eq "1999"));         
          # tally word usage for specific frequency calculation
          # ---------------------------------------------------
          $abswordcnt++;
          if (!exists $abswordfreq{$A})
	  {
	      $abswordfreq{$A} = 0;
	  }
          $abswordfreq{$A}++;
      }

     # Now score abstract
     # ------------------
     $score[$abstractcnt] = 0;
     SCORE:foreach $word (keys %testwordfreq_class1)
       { 
          $abswordfreq{$word} = 0 if (!exists $abswordfreq{$word});
#          print "Testing $word ab_f= $abswordfreq{$word} cl1_f: $testwordfreq_class1{$word} cl2_f: $testwordfreq_class2{$word} ";
          $tempscore = $abswordfreq{$word} * log($testwordfreq_class1{$word} / $testwordfreq_class2{$word});
          $tempscore = $tempscore - ($abswordcnt * ($testwordfreq_class1{$word} - $testwordfreq_class2{$word}));
#          print "s: $tempscore\n";
          $score[$abstractcnt] = $score[$abstractcnt] + $tempscore; 
          
       }
     print "Abstract $abstractcnt Score $score[$abstractcnt] ...",substr($edited_abstract,0,50),"\n";
#     printf "%s %i %s %4.3f %s %s\n", "Abstract", $abstractcnt, "Score", $score[$abstractcnt], "...", $edited_abstract if ($score[$abstractcnt] > 1);
    $abstractcnt++;
   }

$mean = mean(\@score);
$sd = standard_deviation_data(\@score); 
print "Mean score = $mean +/- $sd\n";


# $mean = mean(\@array) computes the mean of an array of numbers.
sub mean {
    my ($arrayref) = @_;
    my $result;
    foreach (@$arrayref) { $result += $_ }
    return $result / @$arrayref;
}

# $sd = standard_deviation_data(\@array) computes the standard
# deviation of an array of numbers.
#
sub standard_deviation_data {
    my $arrayref = shift;
    my $mean = mean($arrayref);
    return sqrt( mean( [map $_ ** 2, @$arrayref] ) - ($mean ** 2) );
}
