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