Skip to content

Instantly share code, notes, and snippets.

@spacekitcat
Last active April 29, 2019 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacekitcat/aba0b9a5edf7f526f1543cb102cb0ef8 to your computer and use it in GitHub Desktop.
Save spacekitcat/aba0b9a5edf7f526f1543cb102cb0ef8 to your computer and use it in GitHub Desktop.
Computes the Shannon entropy for a given input file
const fs = require('fs');
const updateOccurenceTable = (byteOccurenceTable, inputBuffer) => {
inputBuffer.forEach(character => {
const byteOccurenceAccumulator = byteOccurenceTable[character];
byteOccurenceTable[character] = byteOccurenceAccumulator !== undefined ? byteOccurenceAccumulator + 1 : 1;
});
const { total } = byteOccurenceTable;
byteOccurenceTable.total = total ? total + inputBuffer.length : inputBuffer.length;
}
let filePath = process.argv[2];
if (!filePath) {
console.log('No input file path specified. Please provide a file path.');
process.exit(-1);
}
fs.readFile(filePath, {}, (err, chunk) => {
let occurenceTable = { total: 0 };
updateOccurenceTable(occurenceTable, chunk);
let entropy = 0.0;
const { total } = occurenceTable;
Object.keys(occurenceTable).forEach(item => {
if (item !== 'total') {
const itemCount = occurenceTable[item];
const probability = itemCount / total;
if (probability > 0) {
const entropyComponent = probability * Math.log(probability, 2);
entropy = entropy + entropyComponent;
}
}
})
console.log(-entropy);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment