diff --git a/common-lisp/word-count/word-count-test.lisp b/common-lisp/word-count/word-count-test.lisp index fc334f68e3292251d0f0725824b9adb97ac93f33..ba517698b28f58f2eae18ba503472111f1bd798e 100644 --- a/common-lisp/word-count/word-count-test.lisp +++ b/common-lisp/word-count/word-count-test.lisp @@ -108,15 +108,15 @@ three"))) (define-test - alternating-word-separators-not-detected-as-a-word - (assert-alist-equal - '(("one" . 1) ("two" . 1) ("three" . 1)) - (word-count:count-words ", + alternating-word-separators-not-detected-as-a-word + (assert-alist-equal + '(("one" . 1) ("two" . 1) ("three" . 1)) + (word-count:count-words ", ,one, ,two 'three'"))) -#-xlisp-test -(let ((*print-errors* t) +#-xlisp-test () +(LET ((*PRINT-ERRORS* T) (*print-failures* t)) (run-tests :all)) diff --git a/common-lisp/word-count/word-count.lisp b/common-lisp/word-count/word-count.lisp index 43773133aba6a2c09aff6109bdb56817714dfb1a..1ff2f6a70489abb101dc91a6716d65f8583c424a 100644 --- a/common-lisp/word-count/word-count.lisp +++ b/common-lisp/word-count/word-count.lisp @@ -6,9 +6,11 @@ (defun count-words (sentence) (loop - :with res-list = '() - :for x :in (uiop:split-string sentence :separator '(#\Space #\, #\Newline #\Return )) - :do (if (assoc (string-trim "'." x) res-list :test #'string-equal) - (incf (cdr (assoc (string-trim "'!@#$%^&*()_+:;{}|,.-=`~<>?/" x) res-list :test #'string-equal))) - (if (/= 0 (length (string-trim "'!@#$%^&*()_+:;{}|,.-=`~<>?/" x))) (setq res-list (acons (string-trim "'!@#$%^&*()_+:;{}|,.-=`~<>?/" (string-downcase x)) 1 res-list)))) - :finally (return res-list))) + :with res-list = '() + :for x :in (uiop:split-string sentence :separator '(#\Space #\, #\Newline #\Return )) + :do (let ((word (string-downcase (string-trim "'!@#$%^&*()_+:;{}|,.-=`~<>?/" x)))) + (if (assoc word res-list :test #'string-equal) + (incf (cdr (assoc word res-list :test #'string-equal))) + (if (/= 0 (length word)) + (setq res-list (acons word 1 res-list))))) + :finally (return res-list)))