# Find frequency-ranked list of bi-words.
# 1st argument: name of input file
# 2nd argument: name of output file
# 3rd argument (optional): list of flags:
#     f            follow word by its count in corpus
#     s            sort by frequency, not alphabetically


if ( scalar( @ARGV ) >=  2 )
{ 
            $TheFile = $ARGV[0];
            $TheMainOutFile = ">" . $ARGV[1];
} 

 

else 
{           print  "Not enough arguments given to operate; goodbye."; 
            exit;
}

print "The file = $TheFile";

$Options = @ARGV[2];
if ( $Options =~ m/f/ ) 
{
           $PrintFreqs = TRUE;
} 
else 
{
           $PrintFreqs = FALSE;
}


open (OUTFILE, $TheMainOutFile);
open (INFILE, $TheFile) or die "The file $TheFile could not be found";

$WordCount=0;
$LineCount =0;
$Counter = 0;
$LongestWordlength =0;

$PreviousWord = "START";


while (<INFILE>)        
{

            $TheLine= $_;                               # unnecessarily explicit in real PERL
            chomp ($TheLine); 
            $LineCount =  $LineCount + 1;

            @Words=split(/ /, $TheLine); 
            while ($Word= lc(shift (@Words) ) )     
            {         
		$Biword = $PreviousWord . " " . $Word; 
                if ( exists $Biwords { $Biword } )
		{
		    $Biwords { $Biword } ++;
		}
		else
		{
		    $Biwords { $Biword } = 1.0;
		}
                                 
		$PreviousWord = $Word;
                                
            }
}


print "finished reading.\n";


if ( $Options =~ /s/ ) 
{
              @SortedList = sort {    $Biwords{ $b } <=> $Biwords { $a }    }  keys(%Biwords) ;
}
else
{
              @SortedList = sort  keys(%Biwords) ;
}

$NumberOfWords =  $#SortedList;
print "\nNumber of words: ", $NumberOfWords;  

$Rank = 0;
while (@SortedList)
{
	$Rank++;
	print OUTFILE $Rank, "\t";
        $Word = shift (@SortedList);
        print OUTFILE $Word;

              
	if ( $PrintFreqs eq TRUE  ) 
	{ 	print OUTFILE  "\t", $Biwords{$Word}; 
		 
	}
	print OUTFILE "\n";        

}

 

close OUTFILE;






