# Counts number of syllables of words written in ARPAbet.
# 1st argument: name of input file
# 2nd argument: name of output file
# 3rd argument (optional): list of flags:
 
if ( scalar( @ARGV ) >=  2 )
{ 
            $TheFile = $ARGV[0];
            $TheMainOutFile = ">" . $ARGV[1];
} 

 

else 
{           print  "Not enough arguments given to operate; goodbye."; 
            exit;
}

 
open (INFILE, $TheFile) or die "The file $TheFile could not be found";
open (OUTFILE, $TheMainOutFile);

$LineCount =0;
$Counter = 0;
while (<INFILE>)	{
	$TheLine= $_;
	chomp ($TheLine);

	$LineCount =  $LineCount + 1;
	$NumSylls=0;
	@Letters=split(/ /, $TheLine); 
	while ($Word= lc(pop(@Letters) ) )	
	{	if ($Word eq '$') 
			{ $NumSylls++;	}
 	}
	$SyllCount[$NumSylls]++;
}
 
	$Entropy = 0;
  for ($i=1; $i<12; $i++) {
	$Freq=$SyllCount[$i]/$LineCount;
	if ($Freq > 0) {
		$Entropy += $Freq* log ($Freq) * -1.0;	
		print "$i \t $SyllCount[$i] \n"; 
		print OUTFILE   "$i \t $SyllCount[$i] \t $Freq \t \n";
	}
  }
  print $Entropy;
  print OUTFILE "\n\n Entropy:  $Entropy";
