#!/usr/bin/perl -w
################################################################
# kappa.pl: Friedman's Kappa Test for two texts                #
#   Test version                                               #
#--------------------------------------------------------------#
# Test if two texts over the alphabet [A..Z] belong to the     #
#   same language -- i. e. have the same distribution of       #
#   letters.                                                   #
#--------------------------------------------------------------#
# Input from Text1.txt : first text                            #
# Input from Text2.txt : second text                           #
# Output to STDOUT : kappa statistic (coincidence number)      #
#   If the texts have different lengths, the longer one is     #
#   trimmed to the smaller length.                             #
#--------------------------------------------------------------#
# Transliteration of special characters according to English   #
#   rules                                                      #
# For special characters of other languages use preptext.pl    #
#   first.                                                     #
#--------------------------------------------------------------#
# Klaus Pommerening 2013-Dec-28, 2014-Jan-04                   #
#                                                              #
# This program is freeware. Use it as you like.                #
# Usual disclaimers apply.                                     #
################################################################

require "auxcrypt.pl";  # Use procedures &to26eng, &kappa.

open(FILE1,"Text1.txt");          # Use Text1.txt.
chomp(@inlist1 = <FILE1>);        # Read input line by line,
                                  #   chop trailing CR.
foreach $line (@inlist1) {        # Change each line
  $line = &to26eng($line); }      #   to Alphabet [A..Z].
$text1 = join("",@inlist1);       # Join lines as 1 string.
close(FILE1);
$len1 = length($text1);

open(FILE2,"Text2.txt");          # Use Text2.txt.
chomp(@inlist2 = <FILE2>);        # Read input line by line,
                                  #   chop trailing CR.
foreach $line (@inlist2) {        # Change each line
  $line = &to26eng($line); }      #   to Alphabet [A..Z].
$text2 = join("",@inlist2);       # Join lines as 1 string.
close(FILE2);
$len2 = length($text2);
$ll = $len2;                      # Take the minimum of
if ($len1 < $len2) {$ll = $len1;} #   the lengths.

$kapvalue = &kappa($text1,$text2);# Coincicence index
$cnt = $kapvalue * $ll;           # Number of coincidences

print "Length: ", $len1, " | ", $len2, " | ", $ll, "\n";
print "Number of coincidences = ", $cnt, "\n";
print "Coincidence index = ", $kapvalue, "\n";
