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

# @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);
