summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-words.el
diff options
context:
space:
mode:
Diffstat (limited to 'lib/doc/jao-words.el')
-rw-r--r--lib/doc/jao-words.el80
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/doc/jao-words.el b/lib/doc/jao-words.el
new file mode 100644
index 0000000..5d5111d
--- /dev/null
+++ b/lib/doc/jao-words.el
@@ -0,0 +1,80 @@
+;;; jao-words.el --- utilities for word stats -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2026 Jose Antonio Ortega Ruiz
+
+;; Author: Jose Antonio Ortega Ruiz <mail@jao.io>
+;; Keywords: text
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Simple word counting and classification
+
+;;; Code:
+
+(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)
+ (last-line nil)
+ (case-fold-search nil)
+ (rx (format "\\b%s\\b" (regexp-quote word))))
+ (with-current-buffer buffer
+ (save-excursion
+ (goto-char (point-min))
+ (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 jao-words-min-line-distance)
+ (when last-line
+ (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 ((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")
+ (maphash (lambda (word count)
+ (let ((lines (jao-words--word-lines buffer word)))
+ (insert (format "| %s | %d | %s |\n" word count lines))))
+ counts)
+ (org-mode)
+ (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*"))
+
+
+(provide 'jao-words)
+;;; jao-words.el ends here