#
#	Readability - index of readability of a string
#
#			Gaston H. Gonnet (June 12, 2005)

module external Readability;

CountsTab := table();
Norm := table();

Readability := proc( s:string ;
	Counts:{array(integer,26,26),array(integer,27,37)},
	(Ctype='VowConF'):symbol )
external CountsTab, Norm;
if assigned(Counts) then
     Ctype := 'recent';
     if CountsTab[Ctype] <> Counts then
	  CountsTab[Ctype] := Counts;
	  Norm[Ctype] := ln(sum(sum("))/length(Counts)^2)
     fi
elif assigned(Ctype) then
     if not type( CountsTab[Ctype], {array(integer,26,26),
		array(integer,27,27)} ) then
	  error(Ctype,'no suitable count matrix found') fi
fi;

C := CountsTab[Ctype];
r := 1;
t := uppercase(s);
n := 0;
for i to length(t)-1 do
    i1 := AsciiToInt(t[i]) - 64;
    i2 := AsciiToInt(t[i+1]) - 64;
    if min(i1,i2) < 1 or max(i1,i2) > 26 then next fi;
    n := n+1;
    r := r * ( C[i1,i2] + 1 )
od;
ln(r) - n*Norm[Ctype]
end:

EnglishFreq := [194,32,75,91,235,52,30,124,177,6,26,71,49,186,227,43,2,140,
  179,189,43,26,30,1,33,2]:
Vowels := { AsciiToInt('A')-64, AsciiToInt('E')-64, AsciiToInt('I')-64,
    AsciiToInt('O')-64, AsciiToInt('U')-64 };
Counts_VowCon := CreateArray(1..26,1..26):
Counts_VowConF := CreateArray(1..26,1..26):
for i1 in Vowels do for i2 to 26 do if not member(i2,Vowels) then
    Counts_VowCon[i1,i2] := Counts_VowCon[i2,i1] := 1;
    Counts_VowConF[i1,i2] := Counts_VowConF[i2,i1] :=
	EnglishFreq[i1] * EnglishFreq[i2];
fi od od;

CountsTab['VowCon'] := Counts_VowCon;
Norm['VowCon'] := ln(sum(sum("))/26^2);

CountsTab['VowConF'] := Counts_VowConF;
Norm['VowConF'] := ln(sum(sum("))/26^2);

CountsTab['English'] :=
[[0,1,3,5,0,2,5,0,4,0,2,4,3,65,0,2,0,16,21,12,4,4,0,0,6,0],
 [14,1,0,0,9,0,0,0,0,0,0,3,0,0,3,0,0,0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0,7,0,2,0,0,11,0,1,0,5,9,1,0,0,0,0,0,0],
 [3,0,0,0,19,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 [2,0,0,0,2,0,0,8,14,0,0,0,0,0,2,0,0,2,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,1,0,0,0,0,0,0,9,0,0,1,0,0,0,2,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]]:
Norm['English'] := ln(sum(sum("))/26^2);

CountsTab['Spanish'] :=
[[0,30,114,93,1,6,15,2,1,10,0,152,39,116,1,26,0,142,146,53,5,11,0,0,10,1],
[36,0,0,0,11,0,0,0,15,2,0,30,0,0,3,0,0,29,3,0,6,1,0,0,0,0],
[115,0,7,0,34,0,0,11,193,0,0,14,0,2,163,0,0,14,0,39,60,0,0,0,1,0],
[59,0,0,0,302,0,0,0,65,0,0,0,3,1,100,0,1,9,4,1,10,0,1,0,0,0],
[26,7,78,40,9,7,58,0,5,1,0,132,44,214,8,11,13,165,306,35,17,11,0,19,1,1],
[12,0,0,0,12,0,0,0,14,0,0,0,0,0,13,0,0,3,0,2,12,0,0,0,0,0],
[30,0,0,0,24,0,0,0,18,0,0,9,0,7,19,0,0,31,0,0,26,0,0,0,0,0],
[16,0,0,0,9,0,0,0,2,0,0,0,0,0,15,0,0,3,0,0,2,0,0,0,0,1],
[62,21,142,42,48,10,16,0,0,2,0,19,33,137,101,24,1,26,44,24,1,28,0,0,0,30],
[8,0,0,0,3,0,0,0,0,0,0,0,0,0,9,0,0,0,0,0,2,0,0,0,0,0],
[1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[176,0,11,5,111,0,1,0,77,0,0,22,2,1,83,2,0,0,0,20,12,2,0,0,0,0],
[55,5,1,0,80,0,0,1,64,0,0,0,0,2,43,21,0,0,2,0,17,0,0,0,0,0],
[101,0,34,46,69,10,13,0,55,0,0,0,1,0,72,0,0,4,25,146,17,4,0,0,0,3],
[5,24,38,14,3,7,12,0,1,1,0,38,53,144,3,24,2,101,193,15,0,4,1,1,11,1],
[68,0,3,0,48,0,0,0,14,0,0,22,0,1,74,1,0,64,0,2,20,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,85,0,0,0,0,0],
[166,1,24,12,155,1,23,0,90,0,0,4,17,11,96,2,6,21,18,23,18,6,0,0,0,1],
[28,0,10,13,83,0,0,0,63,0,2,3,6,0,61,20,2,0,1,116,30,0,0,0,0,0],
[106,0,4,0,130,0,0,0,102,0,0,11,0,0,74,0,0,88,0,0,34,0,0,0,0,0],
[27,5,21,12,111,1,12,0,22,0,0,36,12,63,1,8,0,36,18,9,8,2,0,0,10,0],
[29,0,0,0,38,0,0,0,23,0,0,0,0,0,12,1,0,0,0,0,1,0,0,0,0,0],
[1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,6,0,0,0],
[0,0,2,0,0,0,0,0,8,0,0,0,0,0,0,5,0,0,0,6,0,0,0,0,0,0],
[2,0,0,0,10,0,0,0,0,0,0,0,0,0,15,0,0,0,0,1,0,0,0,0,0,0],
[32,0,1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]]:
Norm['Spanish'] := ln(sum(sum("))/26^2);

end: # end module
