#!/usr/bin/perl -w
################################################################
# chistat.pl : Kullback's Chi Test for two texts               #
#--------------------------------------------------------------#
# Simulation to characterize the distribution                  #
#                                                              #
# Inputtext from STDIN                                         #
# Command line parameter: $rr = length of text chunks          #
#   The inputtext is divided into chunks of length $rr.        #
#   The next two chunks are compared by chi as long as there   #
#     are any left.                                            #
#--------------------------------------------------------------#
# Transliteration of special characters according to English   #
#   rules                                                      #
# For special characters of other languages use preptext.pl    #
#   first.                                                     #
#--------------------------------------------------------------#
# Klaus Pommerening 2013-December-26                           #
#                                                              #
# This program is freeware. Use it as you like.                #
# Usual disclaimers apply.                                     #
################################################################

require "auxcrypt.pl";  # Use standard alphabet
                           #   as global variables $alphabet,
                           #   @alphabet, and %sa.
                           # Use procedures &gettext.
$rr = $ARGV[0];            # Get length from command line.
$testtext = &gettext;      # Read text from standard input.
while (length($testtext) >= 2*$rr) {      # Enough text left?
  $parttext1 = substr($testtext, 0, $rr); # Then extract
  $testtext = substr($testtext, $rr);     #   next 2 chunks.
  $parttext2 = substr($testtext, 0, $rr);
  $testtext = substr($testtext, $rr);
  $chivalue = &chi($parttext1, $parttext2); # Calculate Kullback's Chi
  print $chivalue*$rr*$rr, " ; ", $chivalue, "\n"; # Output raw value and
                                                   # normalized value.
  } # end while
