2009-01-13

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!

5 comments:

Anonymous said...

I didn't know that how-many function ! Thanks for the tip :-).

Anonymous said...

I used a similar fragment (using count-matches) to count words in my article mode to count words. I'm going to convert it to a minor mode any day now and I'm tempted to add Flesch-Kincaid at the same time.

Robert said...

Why not shell-command on region, where the shell command is wc -w?

Unknown said...

Thanks for the tip, but I simply use command "count-matches" to count regexp "\w+".

Mario Orne Díaz Anadón said...

I like wind's technique.

In order to use this technique, you should first narrow the buffer to the section that you want to count. You do this by selecting the region and typing C-x n n.

As a benefit of using this technique, you remember the use of buffer narrowing and regular expressions.