ncd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Zjg2OGRkZDUwNmMxOGJhMjY0NzFhOWI2MjQ4OTE3NGExMTc3YjY4Yg==
5
+ data.tar.gz: !binary |-
6
+ NmY4MDU0ZjNlMjE0NDQ4ZmUzNTE1OWUxYjM5Y2I2MWZkNjdmOWQ1Yg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZDhlZWUyZjhmNzFjMjY3Mjk2MTY1NzQ0ZmE3YzBmMmJhMWRkMzY3YmNmYWJm
10
+ MjE1ZGYzZjZjNDhjN2I4ZTk5MmM3MDJhYzVmZWI1ZWVhZjZkOGNlMzdmZTNl
11
+ OTUwNTcwYzYzODhiYjE3OTI0NGM2Mzk5NjY1MjBjZGI4Y2ZkZDI=
12
+ data.tar.gz: !binary |-
13
+ ZDM5MmQ0MWE1N2I5OTEyMTI4NjhjM2Y5NzZmNjBhODNmNmU3MTg3M2U4YzZm
14
+ OTlkZTI3NGEyNDk4NzFmZDc5NjNhZWU4MTZhNzZiY2I3YTExMTg5YjE4NGY4
15
+ NmUxMGJjNDdhMzVkYzMzY2E2MTNjYTM1MzZlYzQ0MTVjNDNjZmQ=
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ncd.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 mnishida
2
+
3
+ MIT License
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.
@@ -0,0 +1,38 @@
1
+ # NCD
2
+ Ruby gem for calculating Normalized Compression Distance (NCD).
3
+
4
+ It's inspired by [RubyForge code snippet](http://rubyforge.org/snippet/detail.php?type=snippet&id=3).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'ncd'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ncd
19
+
20
+ ## Usage
21
+ ```ruby
22
+ require 'ncd'
23
+
24
+ a = File.read('somefile.a')
25
+ b = File.read('somefile.b')
26
+ distance = NCD.distance(a, b)
27
+ # or
28
+ distance = a.ncd(b)
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
38
+
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,16 @@
1
+ require 'zlib'
2
+ require_relative 'ncd/version'
3
+
4
+ module NCD
5
+ def self.distance(a, b)
6
+ min, max = [a, b].map{|d| Zlib::Deflate.deflate(d, Zlib::BEST_COMPRESSION).size }.minmax
7
+ cab = Zlib::Deflate.deflate(a+b, Zlib::BEST_COMPRESSION).size
8
+ (cab.to_f - min.to_f) / max.to_f
9
+ end
10
+ end
11
+
12
+ class String
13
+ def ncd(b)
14
+ NCD.distance(self, b)
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Ncd
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ncd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ncd"
8
+ spec.version = Ncd::VERSION
9
+ spec.authors = ["Masata Nishida"]
10
+ spec.email = ["masatanish@gmail.com"]
11
+ spec.description = %q{gem for calculating Normalized Compression Distance.}
12
+ spec.summary = %q{gem for calculating Normalized Compression Distance.}
13
+ spec.homepage = "https://github.com/masatanish/ncd"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", ">=2.13.0"
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ def random_sting(len)
4
+ len.times.map{ '%c' % rand(0..255) }.join.force_encoding('ascii-8bit')
5
+ end
6
+
7
+ describe NCD do
8
+ let(:a) { random_sting(10000) }
9
+ let(:b) { random_sting(10000) }
10
+
11
+ describe '.distance' do
12
+ context 'when assigned values are same.' do
13
+ subject { NCD.distance(a, a) }
14
+ it { expect(subject).to be < 0.1 }
15
+ end
16
+ context 'when assign 2 random strings.' do
17
+ subject { NCD.distance(a, b) }
18
+ it { expect(subject).to be > 0.9 }
19
+ end
20
+ end
21
+
22
+ describe 'String#ncd' do
23
+ context 'when assigned values are same.' do
24
+ it { expect(a.ncd(a)).to be < 0.1 }
25
+ end
26
+ context 'when assign 2 random strings.' do
27
+ it { expect(a.ncd(b)).to be > 0.9 }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'ncd'
6
+
7
+ RSpec.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masata Nishida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.13.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.13.0
55
+ description: gem for calculating Normalized Compression Distance.
56
+ email:
57
+ - masatanish@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ncd.rb
68
+ - lib/ncd/version.rb
69
+ - ncd.gemspec
70
+ - spec/ncd_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/masatanish/ncd
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: gem for calculating Normalized Compression Distance.
96
+ test_files:
97
+ - spec/ncd_spec.rb
98
+ - spec/spec_helper.rb