# Counts coda types of words written in ARPAbet.
# 1st argument: name of dictionary file with ARPABET entries and syllable boundaries.
# 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);

################################################################################# 
 
$WordCount =0;
$Counter = 0;
while (<INFILE>)	
	{
	$TheLine= $_;
	chomp ($TheLine);
	

	@Coda=(); 
	@Letters=split(/ /, $TheLine); 
#	$Number = shift(@Letters);			# This may be unnecessary
	$Spelling = shift (@Letters);

	while ($Letter=  (pop(@Letters) ) )	
	{	if ($Letter eq '$') {
			$Letter =pop(@Letters);
		}

		$StrippedLetter = $Letter;
		$StrippedLetter =~ s/\d//;		 

		if ($StrippedLetter =~ /(AY|AH|ER|AW|EY|IY|OW|UW|OY|AE|AO|AA|EH|IH|OH|UH)/ )	 
		{ 
		   last;
		}
		if ($Letter eq '') { next;} 
		unshift(@Coda, $Letter);

 	} 
	
	$Coda = join ("-", @Coda);
	if ($Coda eq '') {
		$Coda = "NULL";
		@TallyBySize[0]++;
	}
	$NumberOfWords++;
	$Tally{$Coda} ++;	
	@TallyBySize[scalar(@Coda)]++;
 
	}
 
	$Entropy = 0;
	@SortedList = sort { $Tally{$b} <=> $Tally{$a} } keys(%Tally);
	 
	
	while ($ThisCoda = shift (@SortedList)) 
	{
		$CodaTally = $Tally{$ThisCoda};
		$RunningTally += $CodaTally/$NumberOfWords;
		$CodaFreq = $CodaTally/$NumberOfWords;
		print OUTFILE "\n $ThisCoda \t $CodaTally \t $CodaFreq \t $RunningTally"; 
	}
 
	print OUTFILE "\n\n","#"x40, "\nNumber of Codas by length";
	for ($i=0;$i<9; $i++) 
	{
		if ($TallyBySize[$i] > 0 ) 
		{
			$TallyFreq = $TallyBySize[$i]/$NumberOfWords;
			print OUTFILE "\n $i \t $TallyBySize[$i] \t  $TallyFreq";
			print         "\n $i \t $TallyBySize[$i] \t  $TallyFreq";
		}
	}

close OUTFILE;

	
 
 