frequency_analyser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ ## Frequency Analyser
2
+
3
+ Compose a hash containing the frequencies of characters in text.
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ gem install frequency_analyser
9
+ ```
10
+
11
+ ```ruby
12
+ require 'frequency_analyser'
13
+
14
+ file = File.new('a_tale_of_two_cities.txt')
15
+ FrequencyAnalyser.analyse(file)
16
+ #=> {
17
+ 'a'=>47063, 'b'=>8139, 'c'=>13224, 'd'=>27484, 'e'=>72877, 'f'=>13152,
18
+ 'g'=>12121, 'h'=>38358, 'i'=>39784, 'j'=>622, 'k'=>4634, 'l'=>21533,
19
+ 'm'=>14922, 'n'=>41310, 'o'=>45115, 'p'=>9453, 'q'=>655, 'r'=>35956,
20
+ 's'=>36771, 't'=>52393, 'u'=>16216, 'v'=>5065, 'w'=>13835, 'x'=>666,
21
+ 'y'=>11849, 'z'=>213
22
+ }
23
+ ```
@@ -0,0 +1,13 @@
1
+ class FrequencyAnalyser::Aggregation < Hash
2
+
3
+ def initialize
4
+ self.default = 0
5
+ end
6
+
7
+ def <<(other)
8
+ other.each do |key, value|
9
+ self[key] += value
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,14 @@
1
+ class FrequencyAnalyser < Struct.new(:counter, :aggregation)
2
+
3
+ def self.analyse(file)
4
+ new(Counter, Aggregation.new).analyse(file)
5
+ end
6
+
7
+ def analyse(file)
8
+ file.each_line do |line|
9
+ aggregation << counter.count(line)
10
+ end
11
+ aggregation
12
+ end
13
+
14
+ end
@@ -0,0 +1,16 @@
1
+ class FrequencyAnalyser::Counter
2
+
3
+ def self.count(text)
4
+ new.count(text.dup)
5
+ end
6
+
7
+ def count(text)
8
+ text.downcase!
9
+ ('a'..'z').inject(Hash.new(0)) do |hash, c|
10
+ count = text.count(c)
11
+ hash[c] = count unless count.zero?
12
+ hash
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,3 @@
1
+ require 'frequency_analyser/base'
2
+ require 'frequency_analyser/counter'
3
+ require 'frequency_analyser/aggregation'
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frequency_analyser
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Patuzzo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-15 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Compose a hash containing the frequencies of characters in text
35
+ email: chris@patuzzo.co.uk
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - README.md
44
+ - lib/frequency_analyser/aggregation.rb
45
+ - lib/frequency_analyser/base.rb
46
+ - lib/frequency_analyser/counter.rb
47
+ - lib/frequency_analyser.rb
48
+ homepage: https://github.com/cpatuzzo/frequency_analyser
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.24
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Frequency Analyser
81
+ test_files: []
82
+
83
+ has_rdoc: