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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a785eb641f59864a30cbe70af13fed8973aa15e8
4
- data.tar.gz: d046287d885f8ebd7b1955e13106d972768161bd
3
+ metadata.gz: 0fe3e4bfd4d9dc1ccb4176e8ecd3c9c59ec95c9c
4
+ data.tar.gz: c0376d1e4fa8a782e822c91c14d0de62d6b64377
5
5
  SHA512:
6
- metadata.gz: a7b3ec3d2a94a77c45e2f6066f81fe32d9cede96571955666a62ddb92632895cc40d05c8c807e7dff40306e75eb2e9c51a26d60f7d3ae7b505191f6790540e84
7
- data.tar.gz: 742743849f8980bdf5dd5ceb6f03d11ee088dcccc04141691c02eb22326f084fabdcb8a93caf0ac6b423fd7117bdc7d9cfb276edad6ca8d8d06e0f56967c6f01
6
+ metadata.gz: 16ce694d891fabf947080099c25860d4182529c2b8855c3d72c1a9af8d24b3326c00ecddbca24777cf747a79795753094ee8ba1f35b8e63319cf310a4ec7aa05
7
+ data.tar.gz: 88883fb94270e8106fed261fcfa20edcba11546e4c68e2aa7ce50ad4d262bc053e7089a71da27de484bc79ce1f45ea3b16807980fa5420f56ac3b40028633680
data/.gitignore CHANGED
@@ -8,5 +8,7 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
 
11
+ profile
12
+
11
13
  # rspec failure tracking
12
14
  .rspec_status
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.entropy "aaaa" #=> 0
27
- Shannon.entropy("abcde").round 4 #=> 2.3219
28
- Shannon.entropy "" #=> nil
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.
@@ -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
- str.each_char do |char|
8
- counts[char] += 1
9
- end
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
- counts
12
- end
13
-
14
- def self.char_probs str
15
- counts = self.char_counts str
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
@@ -1,3 +1,3 @@
1
1
  module Shannon
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "bundler", "~> 1.14"
33
33
  spec.add_development_dependency "rake", "~> 10.0"
34
34
  spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "yard", "~> 0.9.9"
35
36
  end
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.0
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-04-14 00:00:00.000000000 Z
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