37 lines
896 B
Plaintext
37 lines
896 B
Plaintext
MODULE Entropy;
|
|
FROM InOut IMPORT WriteString, WriteLn;
|
|
FROM RealInOut IMPORT WriteReal;
|
|
FROM Strings IMPORT Length;
|
|
FROM MathLib IMPORT ln;
|
|
|
|
PROCEDURE entropy(s: ARRAY OF CHAR): REAL;
|
|
VAR freq: ARRAY [0..255] OF CARDINAL;
|
|
i, length: CARDINAL;
|
|
h, f: REAL;
|
|
BEGIN
|
|
(* the entropy of the empty string is zero *)
|
|
length := Length(s);
|
|
IF length = 0 THEN RETURN 0.0; END;
|
|
|
|
(* find the frequency of each character *)
|
|
FOR i := 0 TO 255 DO freq[i] := 0; END;
|
|
FOR i := 0 TO length-1 DO
|
|
INC(freq[ORD(s[i])]);
|
|
END;
|
|
|
|
(* calculate the component for each character *)
|
|
h := 0.0;
|
|
FOR i := 0 TO 255 DO
|
|
IF freq[i] # 0 THEN
|
|
f := FLOAT(freq[i]) / FLOAT(length);
|
|
h := h - f * (ln(f) / ln(2.0));
|
|
END;
|
|
END;
|
|
RETURN h;
|
|
END entropy;
|
|
|
|
BEGIN
|
|
WriteReal(entropy("1223334444"), 14);
|
|
WriteLn;
|
|
END Entropy.
|