Last active
April 29, 2019 21:51
-
-
Save spacekitcat/aba0b9a5edf7f526f1543cb102cb0ef8 to your computer and use it in GitHub Desktop.
Computes the Shannon entropy for a given input file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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