This method computes the entropy of the letters. To compute the entropy, for each letter, compute the ratio (say it p) of the frequency count of the letter to the total frequency count of all letters. Then, compute the following value: p * Math.log (1 / p). Return the sum of the values, rounded to the nearest integer value.
public int computeEntropy() {
int FreqCount = 0;
double sum = 0;
char[] O = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for(int k = 0; k < O.length; k++) {
if(FreqCount > 0) {
FreqCount = 0;
}
for(int j = 0; j < N.size(); j++) {
for(int i = 0; i < N.get(j).length(); i++) {
if(O[k] == N.get(j).charAt(i) || Character.toUpperCase(O[k]) == N.get(j).charAt(i)) {
FreqCount++;
}
else
FreqCount += 0;
}
}
p = FreqCount/RawCount;
sum += p * Math.log (1 / p);
return (int)sum;
}
I already get an ArrayList (N) of String which is the word separated by space. I test this method but still cannot get the correct answer. The description of this method is on the top. Can someone help me?