🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of katrinleinweber

katrinleinweber's solution

to Word Count in the R Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

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

Installation

See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.R containing a function that returns a NULL value. Place your implementation inside the body of the function.

How to run tests

Inside of RStudio, simply execute the test_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. Because exercism code and tests are in the same folder, use this same path for both code_path and test_path parameters. On the command-line, you can also run Rscript test_<exercise_name>.R.

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.

test_word-count.R

source("./word-count.R")
library(testthat)

context("word count")

# When comparing lists, all.equal expects the objects to be in the same order
# This expectation instead checks that a) the set of names are the same and
# b) each named object is equal
expect_equal_pairs <- function(object, expected) {
  expect_equal(sort(names(object)),
               sort(names(expected)),
               info = "names in lists differ")
  for (name in names(expected)) {
    expect_equal(object[name], expected[name], info = "list element missing")
  }
}

test_that("count one word", {
  expect_equal_pairs(word_count("word"),
                     list("word" = 1))
})

test_that("count one of each word", {
  expect_equal_pairs(word_count("one of each"),
                     list(
                       "one" = 1,
                       "of" = 1,
                       "each" = 1
                     ))
})

test_that("multiple occurrences of a word", {
  expect_equal_pairs(
    word_count("one fish two fish red fish blue fish"),
    list(
      "one" = 1,
      "fish" = 4,
      "two" = 1,
      "red" = 1,
      "blue" = 1
    )
  )
})

test_that("ignore punctuation", {
  expect_equal_pairs(
    word_count("car : carpet as java : javascript!!&@$%^&"),
    list(
      "car" = 1,
      "carpet" = 1,
      "as" = 1,
      "java" = 1,
      "javascript" = 1
    )
  )
})

test_that("include numbers", {
  expect_equal_pairs(word_count("testing, 1, 2 testing"),
                     list(
                       "testing" = 2,
                       "1" = 1,
                       "2" = 1
                     ))
})

test_that("normalize case", {
  expect_equal_pairs(word_count("go Go GO Stop stop"),
                     list("go" = 3, "stop" = 2))
})

test_that("multiple whitespaces", {
  expect_equal_pairs(word_count(" multiple   whitespaces "),
                     list("multiple" = 1, "whitespaces" = 1))
})

message("All tests passed for exercise: word-count")
library(stringi)
library(magrittr)

word_count <- function(input) {
  
  string <- tolower(input)
  
  # extract unique words, so that counting works without duplicates
  string %>% 
    stri_extract_all_words() %>% 
    unlist() %>% 
    unique() -> 
    words
  
  # count each word in string by utilising word boundary reg-ex
  count <- function(w) {
    string %>% 
      stri_count_regex(paste0(w, "\\b"))
  }
  
  # construct results list
  counts <- purrr::map_int(words, count)
  names(counts) <- words
  as.list(counts)
  
}

Community comments

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

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?