words_counted 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/words_counted/counter.rb +38 -0
- data/lib/words_counted/version.rb +1 -1
- data/words_counted.gemspec +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 190a90d67fb727203b2956402d95b3d830947ba9
|
4
|
+
data.tar.gz: 3af82b30917c67c6d8e0460461a87eccd311080d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9becd4e96f51c2cd5b4e2b2aff9a0a08c6807ef9d3d7556d1017ce74540b9fe918de7b2ba01fe460546f214d3994467d9e91d60d6e183a0fbff3c4c23e525ed7
|
7
|
+
data.tar.gz: 8265fdb5cb49b37d1f2adaa8490ad0cba31497a93addd2f1aefa6ac1742b130877e95ad96b247f49f9d47991e873ee604aac44119105b2071a38eb8a707f6e5c
|
@@ -1,35 +1,73 @@
|
|
1
1
|
module WordsCounted
|
2
2
|
class Counter
|
3
|
+
# @!attribute [Array] an array of words resulting for the string to act on.
|
3
4
|
attr_reader :words
|
4
5
|
|
6
|
+
# This is the criteria for defining words.
|
7
|
+
#
|
8
|
+
# Words are alpha characters and can include hyphens and apostrophes.
|
5
9
|
WORD_REGEX = /[^\p{Alpha}\-']+/
|
6
10
|
|
11
|
+
# Initializes an instance of Counter and splits a given string into an array of words.
|
12
|
+
#
|
13
|
+
# Counter.new("Bad, bad, piggy!")
|
14
|
+
# => #<WordsCounted::Counter:0x007fd49429bfb0 @words=["Bad", "bad", "piggy"]>
|
15
|
+
#
|
16
|
+
# @param string [String] the string to act on.
|
17
|
+
# @param filter [String] a string of words to filter from the string to act on.
|
18
|
+
#
|
7
19
|
def initialize(string, filter = "")
|
8
20
|
@words = string.split(WORD_REGEX).reject { |word| filter.split.include? word.downcase }
|
9
21
|
end
|
10
22
|
|
23
|
+
# Returns the total word count.
|
24
|
+
#
|
11
25
|
def word_count
|
12
26
|
words.size
|
13
27
|
end
|
14
28
|
|
29
|
+
# Returns a hash of words and their occurrences.
|
30
|
+
# Occurrences count is not case sensitive:
|
31
|
+
#
|
32
|
+
# `"Hello hello" #=> { "hello" => 2 }`
|
33
|
+
#
|
34
|
+
# @return [Hash] the resulting hash of words (keys) and their occurrences (values).
|
35
|
+
#
|
15
36
|
def word_occurrences
|
16
37
|
@occurrences ||= words.each_with_object(Hash.new(0)) { |word, result| result[word.downcase] += 1 }
|
17
38
|
end
|
18
39
|
|
40
|
+
# Returns a hash of words and their lengths.
|
41
|
+
#
|
19
42
|
def word_lengths
|
20
43
|
@lengths ||= words.each_with_object({}) { |word, result| result[word] ||= word.length }
|
21
44
|
end
|
22
45
|
|
46
|
+
# Returns a two dimensional array of the most occuring word(s)
|
47
|
+
# and its number of occurrences.
|
48
|
+
#
|
49
|
+
# In the event of a tie, all tied words are returned.
|
50
|
+
#
|
23
51
|
def most_occurring_words
|
24
52
|
highest_ranking word_occurrences
|
25
53
|
end
|
26
54
|
|
55
|
+
# Returns a two dimensional array of the longest word(s) and
|
56
|
+
# its length.
|
57
|
+
#
|
58
|
+
# In the event of a tie, all tied words are returned.
|
59
|
+
#
|
27
60
|
def longest_words
|
28
61
|
highest_ranking word_lengths
|
29
62
|
end
|
30
63
|
|
31
64
|
private
|
32
65
|
|
66
|
+
# Takes a hashmap of the form {"foo" => 1, "bar" => 2} and returns an array
|
67
|
+
# containing the entries (as an array) with the highest number as a value.
|
68
|
+
#
|
69
|
+
# {http://codereview.stackexchange.com/a/47515/1563 See here}.
|
70
|
+
#
|
33
71
|
def highest_ranking(entries)
|
34
72
|
entries.group_by { |word, occurrence| occurrence }.sort.last.last
|
35
73
|
end
|
data/words_counted.gemspec
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
4
5
|
require 'words_counted/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
@@ -10,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
10
11
|
spec.email = ["husseini.mel@gmail.com"]
|
11
12
|
spec.description = %q{A Ruby word counter with helpful utility methods.}
|
12
13
|
spec.summary = %q{See README.}
|
13
|
-
spec.homepage = ""
|
14
|
+
spec.homepage = "https://github.com/abitdodgy/words_counted"
|
14
15
|
spec.license = "MIT"
|
15
16
|
|
16
17
|
spec.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: words_counted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mohamad El-Husseini
|
@@ -85,7 +85,7 @@ files:
|
|
85
85
|
- spec/spec_helper.rb
|
86
86
|
- spec/words_counted/counter_spec.rb
|
87
87
|
- words_counted.gemspec
|
88
|
-
homepage:
|
88
|
+
homepage: https://github.com/abitdodgy/words_counted
|
89
89
|
licenses:
|
90
90
|
- MIT
|
91
91
|
metadata: {}
|