counting words

Emacs has many obscure functions (M-x phases-of-moon...) - but a function to count words is missing. So, every Emacs user, at one time in their life, wants to write their own version. So here is mine -- inspired by the version of Rudolf Olah.
(defun djcb-count-words (&optional begin end)
"count words between BEGIN and END (region); if no region defined, count words in buffer"
(interactive "r")
(let ((b (if mark-active begin (point-min)))
(e (if mark-active end (point-max))))
(message "Word count: %s" (how-many "\\w+" b e))))

The difference between this one and many others ones is that it uses the how-many function that returns the number of matches; this simplifies things quite a bit. I changed Rudolf's version in that it counts the words in the region (selection) if you have one, otherwise, use the whole buffer.

Now, as an exercise to the reader: implement a similar function to determine the Flesch-Kincaid Readability Tests (Reading Ease and Grade Level). Good luck!

No comments:

Post a Comment

Followers

Popular Posts