#!/usr/bin/perl -w
################################################################
# kapstat.pl: Friedman's Kappa 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 kappa.                 #
# Output to STDOUT: CSV file with lines corresponding to the   #
#   text pairs, each line containing the coincidence count and #
#   the coincidence index, separated by a semicolon.           #
#--------------------------------------------------------------#
# Transliteration of special characters according to English   #
#   rules                                                      #
# For special characters of other languages use preptext.pl    #
#   first.                                                     #
#--------------------------------------------------------------#
# Klaus Pommerening 2013-December-28                           #
#                                                              #
# 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, &kappa.
$rr = $ARGV[0];            # Get length from command line.
$testtext = &gettext;      # Read text from standard input.
while (length($testtext) >= 2*$rr) {      # If there is text left
  $parttext1 = substr($testtext, 0, $rr); # extract next chunk
  $testtext = substr($testtext, $rr);
  $parttext2 = substr($testtext, 0, $rr); # and next chunk
  $testtext = substr($testtext, $rr);
  $kappavalue = &kappa($parttext1, $parttext2);
  print $kappavalue*$rr, " ; ", $kappavalue, "\n";
  } # end while
