Entropy/Narcissist: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 431: | Line 431: | ||
{{out}} |
{{out}} |
||
<pre>self-entropy: 4.8684243451359706</pre> |
<pre>self-entropy: 4.8684243451359706</pre> |
||
=={{header|Java}}== |
|||
<lang java> |
|||
import java.io.BufferedReader; |
|||
import java.io.File; |
|||
import java.io.FileReader; |
|||
import java.io.IOException; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
public class EntropyNarcissist { |
|||
private static final String FILE_NAME = "src/EntropyNarcissist.java"; |
|||
public static void main(String[] args) { |
|||
System.out.printf("Entropy of file \"%s\" = %.12f.%n", FILE_NAME, getEntropy(FILE_NAME)); |
|||
} |
|||
private static double getEntropy(String fileName) { |
|||
Map<Character,Integer> characterCount = new HashMap<>(); |
|||
int length = 0; |
|||
try (BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));) { |
|||
int c = 0; |
|||
while ( (c = reader.read()) != -1 ) { |
|||
characterCount.merge((char) c, 1, (v1, v2) -> v1 + v2); |
|||
length++; |
|||
} |
|||
} |
|||
catch ( IOException e ) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
double entropy = 0; |
|||
for ( char key : characterCount.keySet() ) { |
|||
double fraction = (double) characterCount.get(key) / length; |
|||
entropy -= fraction * Math.log(fraction); |
|||
} |
|||
return entropy; |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre>Entropy of file "src/EntropyNarcissist.java" = 3.244796514218. |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |