#!/usr/bin/perl -w
################################################################
# cBLWscore.pl : Sinkov's Log-Weight score for Bigrams         #
#   Version with conditional probabilities                     #
#--------------------------------------------------------------#
# Compute scores for independent bigrams                       #
#   English version                                            #
#                                                              #
# Two lines from STDIN                                         #
#   The corresponding letters of the two lines are combined    #
#   to bigrams.                                                #
# Output: The conditional Bigram-Log-Weight score.             #
#--------------------------------------------------------------#
# No error handling for incorrect input!                       #
#--------------------------------------------------------------#
# Klaus Pommerening 2014-Jun-02, 2014-Jul-22                   #
#                                                              #
# This program is freeware. Use it as you like.                #
# Usual disclaimers apply.                                     #
################################################################

# Hash table with conditional bigram weights
%sctable =(AA=>0.1,AB=>1.6,AC=>1.7,AD=>1.3,AE=>0.0,AF=>1.1,AG=>1.4,AH=>0.0,AI=>1.3,AJ=>0.0,AK=>1.1,AL=>2.0,AM=>1.4,AN=>2.3,AO=>0.4,AP=>1.6,AQ=>0.1,AR=>2.1,AS=>1.9,AT=>2.2,AU=>1.2,AV=>1.5,AW=>0.9,AX=>0.0,AY=>1.5,AZ=>0.1,
BA=>1.7,BB=>0.0,BC=>0.0,BD=>0.0,BE=>2.6,BF=>0.0,BG=>0.0,BH=>0.0,BI=>1.6,BJ=>1.1,BK=>0.0,BL=>2.1,BM=>0.8,BN=>0.0,BO=>1.8,BP=>0.0,BQ=>0.0,BR=>1.6,BS=>1.5,BT=>0.0,BU=>2.2,BV=>0.0,BW=>0.0,BX=>0.0,BY=>2.1,BZ=>0.0,
CA=>2.1,CB=>0.0,CC=>1.6,CD=>0.0,CE=>2.2,CF=>0.5,CG=>0.0,CH=>2.2,CI=>1.7,CJ=>0.0,CK=>1.4,CL=>1.7,CM=>0.0,CN=>0.0,CO=>2.3,CP=>0.5,CQ=>0.0,CR=>1.3,CS=>0.5,CT=>2.1,CU=>1.7,CV=>0.0,CW=>0.5,CX=>0.0,CY=>0.0,CZ=>0.0,
DA=>2.1,DB=>1.7,DC=>1.0,DD=>1.4,DE=>2.0,DF=>1.5,DG=>0.7,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.0,WQ=>0.0,WR=>0.7,WS=>1.2,WT=>1.5,WU=>0.7,WV=>0.7,WW=>1.0,WX=>0.0,WY=>0.0,WZ=>0.0,
XA=>2.2,XB=>0.0,XC=>2.4,XD=>0.0,XE=>1.7,XF=>0.0,XG=>0.0,XH=>0.0,XI=>2.3,XJ=>0.0,XK=>0.0,XL=>0.0,XM=>0.0,XN=>0.0,XO=>1.7,XP=>2.3,XQ=>0.0,XR=>0.0,XS=>0.0,XT=>1.7,XU=>1.7,XV=>0.0,XW=>0.0,XX=>0.0,XY=>0.0,XZ=>0.0,
YA=>1.8,YB=>1.8,YC=>1.7,YD=>1.3,YE=>1.8,YF=>1.2,YG=>1.4,YH=>1.4,YI=>2.0,YJ=>0.0,YK=>0.0,YL=>1.5,YM=>1.3,YN=>1.2,YO=>2.2,YP=>1.6,YQ=>0.0,YR=>1.4,YS=>2.0,YT=>2.0,YU=>0.7,YV=>1.2,YW=>1.9,YX=>0.0,YY=>0.0,YZ=>0.0,
ZA=>0.0,ZB=>0.0,ZC=>0.0,ZD=>0.0,ZE=>2.7,ZF=>0.0,ZG=>0.0,ZH=>0.0,ZI=>2.3,ZJ=>0.0,ZK=>0.0,ZL=>2.0,ZM=>0.0,ZN=>0.0,ZO=>0.0,ZP=>0.0,ZQ=>0.0,ZR=>0.0,ZS=>0.0,ZT=>0.0,ZU=>0.0,ZV=>0.0,ZW=>0.0,ZX=>0.0,ZY=>0.0,ZZ=>2.0);

# @line = ("","");
@line = <>;
# $a = <>;
# print $a;
# $line[0] = chomp($a);
chomp($line[0]);
print $line[0], "\n";
$l0 = length($line[0]);
print "Length: ", $l0, "\n";

# $a = <>;
# $line[1] = chomp($a);
chomp($line[1]);
print $line[1], "\n";
$l1 = length($line[1]);
print "Length: ", $l1, "\n";

$ll = $l0; if ($l1 < $l0) {$ll = $l1;}

$score = 0;
for ($i = 0; $i < $ll; $i++) {
  $bigram = substr($line[0],$i,1) . substr($line[1],$i,1);
  print($bigram, ": ", $sctable{$bigram}, "\n");
  $score += $sctable{$bigram};
  }
printf ("cBLW score: %.1f\n", $score);
printf ("cBLW rate: %.2f\n", $score/$ll);
