words_counted 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9834aaae70272cc358a8bd7f846b56077c94c322
4
- data.tar.gz: 80a6bd1a780307da11adc85a267ae8ad77b60be3
3
+ metadata.gz: 190a90d67fb727203b2956402d95b3d830947ba9
4
+ data.tar.gz: 3af82b30917c67c6d8e0460461a87eccd311080d
5
5
  SHA512:
6
- metadata.gz: e0089b2b4ef682901c65c01641fb6643e7d85c3bf13b2b14dbb1a6d99b4ddaf1c5c74f8e40c7fd1a3ba3af8d4229cec229df98c826499d690b8a7db012279fd9
7
- data.tar.gz: 227c554bdd420091afdd134a753563d7cf4ec1443e8bfeea2dc22c214df098da2f9dceee4f6fe5101c2258859ddc95a3c80898e2f5d0d75d3e5baaf0dfaf7e33
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
@@ -1,3 +1,3 @@
1
1
  module WordsCounted
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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.1
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: {}