Skip to content

word-count: Add more tests with punctuation #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@
"topics": [
]
},
{
"slug": "word-count",
"difficulty": 4,
"topics": [
]
},
{
"slug": "meetup",
"difficulty": 4,
Expand Down Expand Up @@ -256,6 +250,12 @@
"topics": [
]
},
{
"slug": "word-count",
"difficulty": 5,
"topics": [
]
},
{
"slug": "binary-search-tree",
"difficulty": 5,
Expand Down
11 changes: 7 additions & 4 deletions exercises/word-count/examples/success-newtype/src/WordCount.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

module WordCount (wordCount) where

import Prelude hiding (null)
import Prelude hiding (head, init, null, tail)
import Data.Char (isAlphaNum)
import Data.Text (Text, null, split, toLower)
import Data.Text (Text, head, init, null, split, tail, toLower)
import Data.MultiSet (MultiSet, Occur, fromList, fromOccurList, toOccurList)

import qualified GHC.Exts (IsList(..))

wordCount :: Text -> Bag Text
wordCount = Bag
. fromList
. map toLower
. wordsBy (not . isAlphaNum)
. map (stripQuote . toLower)
. wordsBy (\c -> not (isAlphaNum c) && c /= '\'')

-- The `text` package misses this function that
-- exists in package `split`, but works on lists.
wordsBy :: (Char -> Bool) -> Text -> [Text]
wordsBy p = filter (not . null) . split p

stripQuote :: Text -> Text
stripQuote t = if head t == '\'' then init (tail t) else t

-- MultiSet is not an instance of `IsList`, so we create
-- a newtype to wrap it, avoiding an orphan instance.
newtype Bag a = Bag { toMultiSet :: MultiSet a }
Expand Down
8 changes: 6 additions & 2 deletions exercises/word-count/examples/success-simple/src/WordCount.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ wordCount :: String -> [(String, Int)]
wordCount = map (head &&& length)
. group
. sort
. map (map toLower)
. wordsBy (not . isAlphaNum)
. map (stripQuote . map toLower)
. wordsBy (\c -> not (isAlphaNum c) && c /= '\'')

stripQuote :: String -> String
stripQuote ('\'':t) = init t
stripQuote s = s
31 changes: 30 additions & 1 deletion exercises/word-count/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ specs = describe "word-count" $
. fromList
$ input

-- Test cases adapted from `exercism/x-common/word-count.json` on 2016-07-26.
-- Test cases adapted from `exercism/x-common/word-count` on 2016-11-06.

data Case = Case { description :: String
, input :: String
Expand All @@ -56,6 +56,18 @@ cases = [ Case { description = "count one word"
, ("red" , 1)
, ("blue", 1) ]
}
, Case { description = "handles cramped lists"
, input = "one,two,three"
, expected = [ ("one" , 1)
, ("two" , 1)
, ("three", 1) ]
}
, Case { description = "handles expanded lists"
, input = "one,\ntwo,\nthree"
, expected = [ ("one" , 1)
, ("two" , 1)
, ("three", 1) ]
}
, Case { description = "ignore punctuation"
, input = "car : carpet as java : javascript!!&@$%^&"
, expected = [ ("car" , 1)
Expand All @@ -75,4 +87,21 @@ cases = [ Case { description = "count one word"
, expected = [ ("go" , 3)
, ("stop", 2) ]
}
, Case { description = "with apostrophes"
, input = "First: don't laugh. Then: don't cry."
, expected = [ ("first", 1)
, ("don't", 2)
, ("laugh", 1)
, ("then" , 1)
, ("cry" , 1) ]
}
, Case { description = "with quotations"
, input = "Joe can't tell between 'large' and large."
, expected = [ ("joe" , 1)
, ("can't" , 1)
, ("tell" , 1)
, ("between", 1)
, ("large" , 2)
, ("and" , 1) ]
}
]