Letter frequency: Difference between revisions

m
no edit summary
mNo edit summary
Line 2,988:
<syntaxhighlight lang="emacs lisp">
(defun tally-letter-frequency-in-file ()
"Open a file and count the number of times each letter appears."
(let ((alphabet "abcdefghijklmnopqrstuvwxyz")
(current-letter)
(count)
(case-fold-search t)) ; ignores case
(find-file "~/Documents/Elisp/MobyDick.txt") ; identifyopen file in a buffer (or switch to workbuffer if file is already withopen)
(while (>= (length alphabet) 1) ; as long as there is at least 1 letter left in alphabet
(beginning-of-buffer) ; go to the beginning of the buffer
(setq current-letter (substring alphabet 0 1)) ; set current-letter to first letter of alphabet
(setq count (how-many current-letter)) ; count how many of this letter in file
(end-of-buffer) ; go to the end of the buffe
(insert (format "\n%s%s - %7d" current-letter (upcase current-letter) count)) ; write how many times that letter appears
(setq alphabet (substring alphabet 1 nil))) ; remove first letter from alphabet
(insert "\n")))
 
31

edits