help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Most used words in current buffer


From: Udyant Wig
Subject: Re: Most used words in current buffer
Date: Wed, 18 Jul 2018 20:20:55 +0530
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

On 07/18/2018 05:18 PM, Emanuel Berg wrote:
> Great! Only
>
>     (require 'cl-lib)
>     loop -> cl-loop
>     first -> cl-first
>     second -> cl-second

Ah, yes.  Thank you.  I have included the fixes in the following.

---
(require 'cl-lib)

(defun buffer-most-used-words-1 (n)
  "Make a list of the N most used words in buffer."
  (let ((counts (make-hash-table :test #'equal))
        (words (split-string (buffer-string)))
        sorted-counts)
    (dolist (word words)
      (let ((count (gethash (downcase word) counts 0)))
        (puthash (downcase word) (1+ count) counts)))
    (cl-loop for word being the hash-keys of counts
       using (hash-values count)
       do
         (push (list word count) sorted-counts)
       finally (setf sorted-counts (cl-sort sorted-counts #'>
                                            :key #'cl-second)))
    (mapcar #'cl-first (cl-subseq sorted-counts 0 n))))
---

> Perhaps this should be published in M/ELPA or at the very least on
> gnu.emacs.sources :)

Maybe.  I hope this is useful to others also.

Udyant Wig
-- 
We make our discoveries through our mistakes: we watch one another's
success: and where there is freedom to experiment there is hope to
improve.
                                -- Arthur Quiller-Couch



reply via email to

[Prev in Thread] Current Thread [Next in Thread]