Avatar of w1zeman1p

w1zeman1p's solution

to Word Count in the Ruby Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby word_count_test.rb

To include color from the command line:

ruby -r minitest/pride word_count_test.rb

Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

word_count_test.rb

require 'minitest/autorun'
require_relative 'word_count'

# Common test data version: 1.0.0 cd26d49
class WordCountTest < Minitest::Test
  def test_count_one_word
    # skip
    phrase = Phrase.new("word")
    counts = {"word"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_count_one_of_each_word
    skip
    phrase = Phrase.new("one of each")
    counts = {"one"=>1, "of"=>1, "each"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_multiple_occurrences_of_a_word
    skip
    phrase = Phrase.new("one fish two fish red fish blue fish")
    counts = {"one"=>1, "fish"=>4, "two"=>1, "red"=>1, "blue"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_handles_cramped_lists
    skip
    phrase = Phrase.new("one,two,three")
    counts = {"one"=>1, "two"=>1, "three"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_handles_expanded_lists
    skip
    phrase = Phrase.new("one,\ntwo,\nthree")
    counts = {"one"=>1, "two"=>1, "three"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_ignore_punctuation
    skip
    phrase = Phrase.new("car: carpet as java: javascript!!&@$%^&")
    counts = {"car"=>1, "carpet"=>1, "as"=>1, "java"=>1, "javascript"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_include_numbers
    skip
    phrase = Phrase.new("testing, 1, 2 testing")
    counts = {"testing"=>2, "1"=>1, "2"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_normalize_case
    skip
    phrase = Phrase.new("go Go GO Stop stop")
    counts = {"go"=>3, "stop"=>2}
    assert_equal counts, phrase.word_count
  end

  def test_with_apostrophes
    skip
    phrase = Phrase.new("First: don't laugh. Then: don't cry.")
    counts = {"first"=>1, "don't"=>2, "laugh"=>1, "then"=>1, "cry"=>1}
    assert_equal counts, phrase.word_count
  end

  def test_with_quotations
    skip
    phrase = Phrase.new("Joe can't tell between 'large' and large.")
    counts = {"joe"=>1, "can't"=>1, "tell"=>1, "between"=>1, "large"=>2, "and"=>1}
    assert_equal counts, phrase.word_count
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 1, BookKeeping::VERSION
  end
end
class Phrase
  VERSION = 1
  attr_reader :sentence

  def initialize(sentence)
    @sentence = sentence.gsub(/[^a-zA-Z0-9,' ]/, '').downcase
  end

  def word_count
    sentence
      .split(/'?\s+'?|,/)
      .inject(Hash.new(0)) do |acc, word|
        next acc if word.empty?
        acc[word] += 1; acc
      end
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of NobbZ

Have you considered foreign languages in your solution that do either use an extended latin alphabet (german, french, spain, etc) or even something that is not related (kyrillic, chinese, japanase, etc)?

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?