occurrence-counter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Endel Dreyer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'rubygems/package_task'
3
+ require 'rake'
4
+ require 'rake/clean'
5
+
6
+ VERSION = '0.0.1'
7
+
8
+ spec = Gem::Specification.new do |s|
9
+ s.name = 'occurrence-counter'
10
+ s.version = VERSION
11
+ s.summary = 'Ruby utility to count occurrences from standard data types.'
12
+ s.description = 'Utility to count occurrences from standard data types.'
13
+ s.author = 'Endel Dreyer'
14
+ s.homepage = "http://github.com/endel/occurrence-counter"
15
+ s.email = 'endel.dreyer@gmail.com'
16
+ s.files = %w(LICENSE Rakefile) + Dir.glob("{bin,lib,test}/**/*")
17
+ s.require_path = "lib"
18
+ s.bindir = "bin"
19
+ end
20
+
21
+ Gem::PackageTask.new(spec) do |p|
22
+ p.need_tar = true
23
+ p.need_zip = true
24
+ end
25
+
26
+ task :install do
27
+ rm_rf "pkg/*.gem"
28
+ `rake gem`
29
+ puts `gem install pkg/occurrence-counter-#{VERSION}.gem`
30
+ end
@@ -0,0 +1,5 @@
1
+ dir = File.dirname(__FILE__)
2
+
3
+ require "#{dir}/occurrence_counter/occurrence_counter.rb"
4
+ require "#{dir}/occurrence_counter/ext/string.rb"
5
+ require "#{dir}/occurrence_counter/ext/array.rb"
@@ -0,0 +1,6 @@
1
+
2
+ class Array
3
+ def count_occurrences(*pre_process)
4
+ OccurrenceCounter.count(self, *pre_process)
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ class String
4
+ def word_occurrences(*pre_process)
5
+ text = self.gsub(/[\.,;:&*@!\(\)\"\'%]/, "")
6
+ OccurrenceCounter.count(text.split(" "), *pre_process)
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+
2
+ class OccurrenceCounter
3
+ attr_reader :pre_process, :source
4
+
5
+ def self.count(source, *pre_process)
6
+ occurrece_counter = OccurrenceCounter.new(source)
7
+ occurrece_counter.pre_process(*pre_process) if pre_process.length > 0
8
+ occurrece_counter.count
9
+ end
10
+
11
+ def initialize(source)
12
+ @source = source
13
+ @iteration_method = :each
14
+ @pre_process = []
15
+ end
16
+
17
+ def pre_process(*methods)
18
+ methods.each {|method| @pre_process << method }
19
+ end
20
+
21
+ def count
22
+ occurrences = Hash.new(0)
23
+ @source.send(@iteration_method) do |value|
24
+ @pre_process.each {|hook| value = (hook.is_a? Proc) ? hook.call(value) : value.send(hook)}
25
+ # Skip invalid occurrences
26
+ next if value.nil?
27
+
28
+ occurrences[value] += 1
29
+ end
30
+ occurrences
31
+ end
32
+
33
+ end
@@ -0,0 +1,39 @@
1
+ require './test_helper'
2
+
3
+ class OcurrenceCounterTest < Test::Unit::TestCase
4
+
5
+ def test_array
6
+ fixture = [1,1,1,1,1,3,3,1,3,3,3,5,5]
7
+ array_count = fixture.count_ocurrences
8
+
9
+ assert array_count.is_a? Hash
10
+ assert_equal array_count[1], 6
11
+ assert_equal array_count[3], 5
12
+ assert_equal array_count[5], 2
13
+ end
14
+
15
+ def test_string
16
+ fixture = "Hello I am a little string and I am very happy counting myself. Because myself is cool and I am myself..."
17
+ word_count = fixture.word_ocurrences(:downcase)
18
+
19
+ assert word_count.is_a? Hash
20
+ assert_equal word_count["hello"], 1
21
+ assert_equal word_count["i"], 3
22
+ assert_equal word_count["am"], 3
23
+ assert_equal word_count["a"], 1
24
+ assert_equal word_count["and"], 2
25
+ assert_equal word_count["myself"], 3
26
+ end
27
+
28
+ def test_custom_pre_processor
29
+ fixture = "Hello I am a little string and I am very happy counting myself. Because myself is cool and I am myself..."
30
+ word_count = fixture.word_ocurrences(lambda {|str|
31
+ str = str.downcase
32
+ str.length > 4 ? str : nil
33
+ })
34
+
35
+ assert word_count.is_a? Hash
36
+ assert_equal word_count.length, 7
37
+ end
38
+
39
+ end
@@ -0,0 +1,7 @@
1
+ dir = File.dirname(File.expand_path(__FILE__))
2
+ $LOAD_PATH.unshift dir + '/../lib'
3
+ $TESTING = true
4
+
5
+ require 'test/unit'
6
+ require 'rubygems'
7
+ require 'ocurrence_counter'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: occurrence-counter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Endel Dreyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-24 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Utility to count occurrences from standard data types.
18
+ email: endel.dreyer@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - LICENSE
27
+ - Rakefile
28
+ - lib/occurrence_counter/ext/array.rb
29
+ - lib/occurrence_counter/ext/string.rb
30
+ - lib/occurrence_counter/occurrence_counter.rb
31
+ - lib/occurrence_counter.rb
32
+ - test/ocurrence_counter_test.rb
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/endel/occurrence-counter
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.6.2
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Ruby utility to count occurrences from standard data types.
62
+ test_files: []
63
+