################################################################
# bicount.pl : Bigram count over the alphabet [A..Z]           #
#    Input from STDIN : Text                                   #
#    Output to STDOUT : Matrix of bigram frequencies           #
#--------------------------------------------------------------#
# Klaus Pommerening 27. September 1999, 8. Oktober 1999        #
#                                                              #
# This program is freeware. Use it as you like.                #
# Usual disclaimers apply.                                     #
#--------------------------------------------------------------#
# For frequencies >= 1000 the output looks ugly.               #
################################################################

require "auxcrypt.pl";

$text = &gettext;                   # Get text as list
@textlist = split(//,$text);        #   of characters in A..Z.
for ($i = 0; $i < @alphabet; $i++) {
  for ($j = 0; $j < @alphabet; $j++) {
    $count[$i][$j] = 0;             # Initialize counters.
    }                               #
  }

for ($k = 0; $k < @textlist - 1; $k++) { # Count Bigrams
  $count[$sa{$textlist[$k]}][$sa{$textlist[$k+1]}]++;
  }
print "     ";                      # Print table header
for ($i = 0; $i < @alphabet; $i++) {
  print "$alphabet[$i]   ";
  }
print "\n";

for ($i = 0; $i < @alphabet; $i++) {# Print table rows
  print "$alphabet[$i] ";
  for ($j = 0; $j < @alphabet; $j++) {
    printf "%4d", $count[$i][$j];
    }
  print "\n";
  }
