From 5ee2ae03e7d835995fa20c12a316bf52795cedf5 Mon Sep 17 00:00:00 2001 From: jao Date: Mon, 28 Sep 2026 21:52:40 +0100 Subject: wee improvements --- lib/doc/jao-words.el | 53 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'lib/doc/jao-words.el') diff --git a/lib/doc/jao-words.el b/lib/doc/jao-words.el index 52e5093..5d5111d 100644 --- a/lib/doc/jao-words.el +++ b/lib/doc/jao-words.el @@ -24,13 +24,12 @@ ;;; Code: -(defun jao-words--count-words (raw-word-list) - (cl-loop with result = nil - for elt in raw-word-list - do (cl-incf (cdr (or (assoc elt result) - (car (push (cons elt 0) result))))) - finally return (sort result - (lambda (a b) (string< (car a) (car b)))))) +(require 'org) +(require 'cl-lib) + +(defvar jao-words-min-length 3) +(defvar jao-words-max-lines 12) +(defvar jao-words-min-line-distance 10) (defun jao-words--word-lines (buffer word) (let ((lines nil) @@ -40,36 +39,40 @@ (with-current-buffer buffer (save-excursion (goto-char (point-min)) - (while (and (re-search-forward rx nil t) (< (length lines) 12)) + (while (and (< (length lines) jao-words-max-lines) + (re-search-forward rx nil t)) (let* ((line (line-number-at-pos)) (d (- line (or last-line line)))) - (when (<= d 10) + (when (< d jao-words-min-line-distance) (when last-line - (push last-line lines) - (push line lines))) - (setq last-line (line-number-at-pos)))))) + (cl-pushnew last-line lines) + (cl-pushnew line lines))) + (setq last-line line))))) (mapconcat #'number-to-string (nreverse lines) " "))) +;;;###autoload (defun jao-words-show-stats () (interactive) - (let* ((words (split-string (downcase (buffer-string)) "\\W+" t)) - (word-list (jao-words--count-words words)) - (buffer (current-buffer))) + (let ((counts (make-hash-table :test 'equal)) + (buffer (current-buffer))) + (seq-each (lambda (word) + (when (>= (length word) jao-words-min-length) + (puthash word (1+ (gethash word counts 0)) counts))) + (split-string (downcase (buffer-string)) "\\W+" t)) (with-current-buffer (get-buffer-create "*word-statistics*") (erase-buffer) (insert "| word | occurences | lines | |-----------+------------|--------------------|\n") - (dolist (elt word-list) - (let* ((word (car elt)) - (count (cdr elt)) - (lines (jao-words--word-lines buffer word))) - (insert (format "| %s | %d | %s |\n" word count lines)))) + (maphash (lambda (word count) + (let ((lines (jao-words--word-lines buffer word))) + (insert (format "| %s | %d | %s |\n" word count lines)))) + counts) (org-mode) - (indent-region (point-min) (point-max)) - (goto-char 5) - (forward-line 2) - (org-table-next-field) - (org-table-sort-lines nil ?N))) + (org-table-goto-line 2) + (org-table-goto-column 2) + (org-table-sort-lines nil ?N) + (org-table-align) + (org-table-goto-line 2))) (pop-to-buffer "*word-statistics*")) -- cgit v1.2.3