bloomin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/License.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012-2013 Alex Okolish
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Bloomin
2
+
3
+ Bloomin is a really quickly hacked together example of a bloom filter.
4
+ In fact, I'm not even sure it's written properly. So, use this at your
5
+ own risk.
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ filter = Bloomin::Filter.new
11
+ filter.is_word? "aoeu" # => false
12
+ filter.is_word? "cats" # => true
13
+ ```
14
+
15
+ Bloomin assumes that the file `/usr/share/dict/words` exists. If not,
16
+ stuff will blow up.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bloomin.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bloomin/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "bloomin"
8
+ gem.version = Bloomin::VERSION
9
+ gem.authors = ["Alex Okolish"]
10
+ gem.email = ["alex.okolish@gmail.com"]
11
+ gem.description = %q{hacked together bloom filter}
12
+ gem.summary = %q{use at your own risk}
13
+ gem.homepage = "http://github.com/aokolish/bloomin"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency("fnv", [">= 0.2.0"])
21
+ gem.add_development_dependency("rspec", [">= 2.2.0"])
22
+ end
data/lib/bloomin.rb ADDED
@@ -0,0 +1 @@
1
+ require 'bloomin/filter'
@@ -0,0 +1,62 @@
1
+ require 'fnv'
2
+
3
+ module Bloomin
4
+ class Filter
5
+ attr_reader :bloom
6
+ attr_accessor :size
7
+
8
+ def initialize(opts = {})
9
+ @size = opts[:size] || 5000000
10
+ @bloom = Array.new(self.size).fill(0)
11
+
12
+ load_words
13
+ end
14
+
15
+ def is_word?(word)
16
+ integers = hash(word)
17
+ integers.each do |i|
18
+ if self.bloom[i] == 0
19
+ puts "not a word - #{word}" if debug?
20
+ false
21
+ else
22
+ puts "probably, a word - #{word}, #{probability}" if debug?
23
+ true
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def load_words
31
+ File.open('/usr/share/dict/words','r').each_with_index do |word, i|
32
+ word.strip!
33
+ add_to_bloom(word)
34
+ @dict_size = i
35
+ end
36
+ end
37
+
38
+ # returns array of integer values
39
+ def hash(word)
40
+ hex = FNV.new.fnv1a_32(word) % self.size
41
+ [hex]
42
+ end
43
+
44
+ # flips bits in bloom if not already set
45
+ def add_to_bloom(word)
46
+ integers = hash(word)
47
+ integers.each do |i|
48
+ self.bloom[i] = 1 unless self.bloom[i] == 1
49
+ end
50
+ end
51
+
52
+ def probability
53
+ num_hashes = 1
54
+ (((1 - Math.exp((-num_hashes * @dict_size) / self.size )) ** num_hashes) * 100).round(2).to_s +
55
+ '% chance of false positive'
56
+ end
57
+
58
+ def debug?
59
+ ENV['DEBUG'] == 'true'
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Bloomin
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bloomin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Okolish
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fnv
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.2.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.2.0
46
+ description: hacked together bloom filter
47
+ email:
48
+ - alex.okolish@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - License.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bloomin.gemspec
59
+ - lib/bloomin.rb
60
+ - lib/bloomin/filter.rb
61
+ - lib/bloomin/version.rb
62
+ homepage: http://github.com/aokolish/bloomin
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.23
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: use at your own risk
86
+ test_files: []