(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:
I didn't know that how-many function ! Thanks for the tip :-).
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.
Why not shell-command on region, where the shell command is wc -w?
Thanks for the tip, but I simply use command "count-matches" to count regexp "\w+".
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.
Post a Comment