summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-words.el
blob: 5d5111dae24cfaf4c7005411c89280369b3724e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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