This is a straightforward implementation of a basic Shannon Entropy calculator.
It's written in stock OCaml and uses a mutable Hashtbl for binning.
Wikipedia entry on Shannon Entropy
Run from command line, passing in a string or, with the -f flag, a filename.
-h or --help for help.
Returns the basic Shannon Entropy in bits.
Uses Streams and is fairly fast; file IO will probably account for most of the run time.
###a few examples
Shannon Entropy of the string "1223334444" (also contained in the demo.txt file)
character count: 10
alphabet size: 4
value: 1.84643934467
the text of "The Time Machine", by H.G. Welles
character count: 182153
value: 4.4722636685
"Alice in Wonderland", by Lewis Carroll
character count: 167518
value: 4.64214241863
executable binary file of the shannon program itself (linux x86_64)
character count: 440231
value: 5.48398710282
After a couple of variations using persistent data structures data structures I just went ahead and settled on the mutable Hashtbl for in-place binning operations.
In a similar nod to speed the core ops of the primary folds are pre-baked as much as possible. Tested with files up to a few hundred MBs but should handle more nominally.
Also: the core operation code is similar to the simple string-only Shannon Entropy OCaml example I previously wrote for the Rosetta Code site
Written with standard OCaml 4.01, no extra libs required
Copyright © 2014-2015 jm ervin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.