shannon 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/LICENSE +22 -0
- data/README.md +5 -3
- data/lib/shannon.rb +14 -24
- data/lib/shannon/version.rb +1 -1
- data/shannon.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fe3e4bfd4d9dc1ccb4176e8ecd3c9c59ec95c9c
|
4
|
+
data.tar.gz: c0376d1e4fa8a782e822c91c14d0de62d6b64377
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16ce694d891fabf947080099c25860d4182529c2b8855c3d72c1a9af8d24b3326c00ecddbca24777cf747a79795753094ee8ba1f35b8e63319cf310a4ec7aa05
|
7
|
+
data.tar.gz: 88883fb94270e8106fed261fcfa20edcba11546e4c68e2aa7ce50ad4d262bc053e7089a71da27de484bc79ce1f45ea3b16807980fa5420f56ac3b40028633680
|
data/.gitignore
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ryan Moore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -23,11 +23,13 @@ Or install it yourself as:
|
|
23
23
|
```ruby
|
24
24
|
require "shannon"
|
25
25
|
|
26
|
-
Shannon
|
27
|
-
Shannon
|
28
|
-
Shannon
|
26
|
+
Shannon::entropy "" #=> 0
|
27
|
+
Shannon::entropy "aaaa" #=> 0
|
28
|
+
Shannon::entropy("abcde").round 4 #=> 2.3219
|
29
29
|
```
|
30
30
|
|
31
|
+
It uses base 2 for the log by default, so `Shannon::entropy "abcde"` and `Shannon::entropy "abcde", 2` will give the same answer.
|
32
|
+
|
31
33
|
## Development
|
32
34
|
|
33
35
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/shannon.rb
CHANGED
@@ -1,31 +1,21 @@
|
|
1
1
|
require "shannon/version"
|
2
2
|
|
3
3
|
module Shannon
|
4
|
-
def self.char_counts str
|
5
|
-
counts = Hash.new 0
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
# Calculate Shannon entropy for a string.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# Shannon::entropy("abcde", 2).round(4) #=> 2.3219
|
9
|
+
#
|
10
|
+
# @param str [String] string to calculate entropy of
|
11
|
+
# @param base [Fixnum] base of the log for calculation
|
12
|
+
def self.entropy str, base=2
|
13
|
+
len = str.length.to_f
|
10
14
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
counts.values.map { |count| count / str.length.to_f }
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.probs_to_entropy probs
|
21
|
-
- probs.reduce(0.0) { |sum, prob| sum += prob * Math.log2(prob) }
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.entropy str
|
25
|
-
if str.empty?
|
26
|
-
nil
|
27
|
-
else
|
28
|
-
self.probs_to_entropy self.char_probs(str)
|
29
|
-
end
|
15
|
+
str.each_char.
|
16
|
+
group_by(&:itself).
|
17
|
+
values.
|
18
|
+
map { |ary| ary.length / len }.
|
19
|
+
reduce(0) { |entropy, freq| entropy - freq * Math.log(freq, base) }
|
30
20
|
end
|
31
21
|
end
|
data/lib/shannon/version.rb
CHANGED
data/shannon.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shannon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Moore
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.9
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.9.9
|
55
69
|
description: Simple gem to calculate Shannon entropy.
|
56
70
|
email:
|
57
71
|
- moorer@udel.edu
|
@@ -64,6 +78,7 @@ files:
|
|
64
78
|
- ".travis.yml"
|
65
79
|
- CODE_OF_CONDUCT.md
|
66
80
|
- Gemfile
|
81
|
+
- LICENSE
|
67
82
|
- README.md
|
68
83
|
- Rakefile
|
69
84
|
- bin/console
|