Letter frequency: Difference between revisions
Content added Content deleted
mNo edit summary |
mNo edit summary |
||
Line 2,989: | Line 2,989: | ||
(defun tally-letter-frequency-in-file () |
(defun tally-letter-frequency-in-file () |
||
"Open a file and count the number of times each letter appears." |
"Open a file and count the number of times each letter appears." |
||
(let ((alphabet "abcdefghijklmnopqrstuvwxyz") |
(let ((alphabet "abcdefghijklmnopqrstuvwxyz") ; variable to hold letters we will be counting |
||
(current-letter) ; variable to hold current letter we will be counting |
|||
(current-letter) |
|||
(count) ; variable to count how many times current letter appears |
|||
(count) |
|||
(case-fold-search t)) ; ignores case |
(case-fold-search t)) ; ignores case |
||
(find-file "~/Documents/Elisp/MobyDick.txt") ; open file in a buffer (or switch to buffer if file is already open) |
(find-file "~/Documents/Elisp/MobyDick.txt") ; open file in a buffer (or switch to buffer if file is already open) |
||
Line 2,998: | Line 2,998: | ||
(setq current-letter (substring alphabet 0 1)) ; set current-letter to first letter of alphabet |
(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 |
(setq count (how-many current-letter)) ; count how many of this letter in file |
||
(end-of-buffer) ; go to the end of the |
(end-of-buffer) ; go to the end of the buffer |
||
(insert (format "\n%s%s - %7d" current-letter (upcase current-letter) count)) ; write how many times that letter appears |
(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 |
(setq alphabet (substring alphabet 1 nil))) ; remove first letter from alphabet |