Skip to content

Commit

Permalink
Sync Word Count with problem specifications
Browse files Browse the repository at this point in the history
Fixes #200
  • Loading branch information
kahgoh committed Feb 23, 2024
1 parent 70c2852 commit 080589f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 14 deletions.
32 changes: 22 additions & 10 deletions exercises/practice/word-count/.meta/example.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@

(defun count (string)
(lists:foldl
(lambda (k acc)
(dict:update_counter k 1 acc))
(lambda (word acc)
(dict:update_counter word 1 acc))
(dict:new)
(tokenize (string:to_lower string))))

(defun alnum? (char) (orelse (=< #\a char #\z) (=< #\0 char #\9)))
(defun unquote
(((cons #\' remaining))
(case (lists:last remaining)
(#\' (lists:sublist remaining 1 (- (length remaining) 1)))
(_ (cons #\' remaining))))
((word) word))

(defun sep? (char) (not (alnum? char)))
(defun tokenize-loop
; End of sentence with no more words
(('() '() acc) acc)
; End of sentence with one more word
(('() word-acc acc) (cons (unquote (lists:reverse word-acc)) acc))
; Lower case character, number or apostrophe
(((cons char remaining) word-acc acc)
(when (or (and (>= char #\a) (=< char #\z)) (and (>= char #\0) (=< char #\9)) (== char #\')))
(tokenize-loop remaining (cons char word-acc) acc))
; Boundary or junk with no word in word accumulator (skip it)
(((cons _ remaining) '() acc) (tokenize-loop remaining '() acc))
; Boundary or junk with something in word accumulator (add the word to the accumulator)
(((cons _ remaining) word-acc acc) (tokenize-loop remaining '() (cons (unquote (lists:reverse word-acc)) acc))))

(defun tokenize
([()] ())
([string]
(case (lists:splitwith #'alnum?/1 (lists:dropwhile #'sep?/1 string))
(`#([] ,rest) (tokenize rest))
(`#(,word ,rest) (cons word (tokenize rest))))))
(defun tokenize (text) (tokenize-loop text '() '()))
3 changes: 2 additions & 1 deletion exercises/practice/word-count/src/word-count.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%% -*- erlang -*-
{application, 'word-count',
[{description, ""},
[{description, "Given a phrase, count the occurrences of each word in that phrase."
},
{vsn, "0.0.1"},
{modules,
['word-count']},
Expand Down
4 changes: 4 additions & 0 deletions exercises/practice/word-count/src/word-count.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(defmodule word-count
(export (count 1)))

; Please implement the `count` function
83 changes: 80 additions & 3 deletions exercises/practice/word-count/test/word-count-tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,23 @@
#("red" 1)
#("blue" 1))))

(deftest handles-cramped-lists
(assert-count
"one,two,three"
'(#("one" 1)
#("two" 1)
#("three" 1))))

(deftest handles-expanded-lists
(assert-count
"one,\ntwo,\nthree"
'(#("one" 1)
#("two" 1)
#("three" 1))))

(deftest ignore-punctuation
(assert-count
"car : carpet as java : javascript!!&@$%^&"
"car: carpet as java: javascript!!&@$%^&"
'(#("car" 1)
#("carpet" 1)
#("as" 1)
Expand All @@ -45,8 +59,71 @@

(deftest normalize-case
(assert-count
"go Go GO"
'(#("go" 3))))
"go Go GO Stop stop"
'(#("go" 3)
#("stop" 2))))

(deftest with-apostrophes
(assert-count
"First: don't laugh. Then: don't cry."
'(#("first" 1)
#("don't" 2)
#("laugh" 1)
#("then" 1)
#("cry" 1))))

(deftest with-apostrophes-multiple-letters
(assert-count
"First don't laugh. then: don't cry. You're getting it."
'(#("first" 1)
#("don't" 2)
#("laugh" 1)
#("then" 1)
#("cry" 1)
#("you're" 1)
#("getting" 1)
#("it" 1))))

(deftest with-quotations
(assert-count
"Joe can't tell between 'large' and large."
'(#("joe" 1)
#("can't" 1)
#("tell" 1)
#("between" 1)
#("large" 2)
#("and" 1))))

(deftest substrings-from-the-beginning
(assert-count
"Joe can't tell between app, apple and a."
'(#("joe" 1)
#("can't" 1)
#("tell" 1)
#("between" 1)
#("app" 1)
#("apple" 1)
#("and" 1)
#("a" 1))))

(deftest multiple-spaces-not-detected-as-a-word
(assert-count
" multiple whitespaces"
'(#("multiple" 1)
#("whitespaces" 1))))

(deftest alternating-word-separators-not-detected-as-a-word
(assert-count
",\n,one,\n ,two \n 'three'"
'(#("one" 1)
#("two" 1)
#("three" 1))))

(deftest quotation-for-word-with-apostrophe
(assert-count
"can, can't, 'can't'"
'(#("can" 1)
#("can't" 2))))

(deftest prefix-punctuation
(assert-count
Expand Down

0 comments on commit 080589f

Please sign in to comment.