Debian Codegraph @

contrib/unicode/names-pairs.lisp

raw
1 (declaim (optimize (debug 0) (speed 3)))
2
3 (setf *print-circle* t)
4
5 (defun compute-pairs (data table)
6 ;; Compute the frequency of consecutive pairs of words and store
7 ;; them in table.
8 (clrhash table)
9 (loop with max = 0
10 with max-pair = nil
11 for (code name . l) in data
12 do (loop for l2 on l
13 for a = (first l2)
14 for b = (second l2)
15 while b
16 do (let* ((pair (cons a b))
17 (freq (gethash pair table)))
18 (setf (gethash pair table)
19 (setf freq (if freq (1+ freq) 1))
20 a b)
21 (when (> freq max)
22 (setf max freq max-pair pair))))
23 finally (return (cons max max-pair))))
24
25 (defun replace-pair (pair code data)
26 ;; For all characters in data, replace the pair representing two
27 ;; consecutive words by code (which is a single integer).
28 ;; Returns the total number of pairs left.
29 (let ((old-a (car pair))
30 (old-b (cdr pair)))
31 (loop with more = 0
32 for (ucd-code name . l) in data
33 do (loop with l2 = l
34 for a = (first l2)
35 for b = (second l2)
36 while b
37 do (when (and (eql a old-a) (eql b old-b))
38 ;; replace (a b . rest) with (code . rest)
39 (setf (car l2) code
40 (cdr l2) (cddr l2)))
41 do (setf l2 (cdr l2)))
42 (incf more (1- (length l)))
43 finally (return more))))
44
45 (defun compress (data)
46 ;; Compress data by replacing the pair of consecutive words with
47 ;; maximum frequency with a new code until no pairs with frequency >
48 ;; 1 are left.
49 (loop with last-length = 0
50 with table = (make-hash-table :size 2048 :test #'equal)
51 with pairs = '()
52 for new-symbol from (1+ *last-word-index*)
53 for (frequency . pair) = (compute-pairs data table)
54 while (and pair (> frequency 1))
55 do
56 (format t "~%;;; ~A, ~D -> ~D, ~D left" pair frequency new-symbol
57 (replace-pair pair new-symbol data))
58 (setf pairs (acons new-symbol pair pairs))
59 finally
60 ;; There are no redundant pairs. We just define ad-hoc new
61 ;; symbols for all remaining strings.
62 (loop with n = new-symbol
63 for (code name . l) in data
64 do (loop with l2 = l
65 for a = (first l2)
66 for b = (second l2)
67 while b
68 do (setf pairs (acons n (cons a b) pairs)
69 (car l2) n
70 (cdr l2) (cddr l2)
71 n (1+ n))))
72 (print 'finished)
73 (return-from compress (nreverse pairs))))
74
75 (progn
76 (defparameter *compressed-data* (copy-tree *data*))
77 (defparameter *paired-data* (compress *compressed-data*)))
78
79 (defparameter *last-code* (first (first (last *paired-data*))))
80
81 (defparameter *code-ndx-size* (ceiling (integer-length *last-code*) 8))
82
83 (defparameter *pair-table-size* (* (length *paired-data*)
84 (* 2 *code-ndx-size*)))
85
86 (defparameter *code-to-name-bytes*
87 (* (length *compressed-data*)
88 (+ 3 ; Size of Unicode code
89 ;; Size of index into the data table
90 *code-ndx-size*)))
91
92 (defparameter *sorted-names-bytes*
93 ;; The sorted list of character names is just a list of indices into
94 ;; the *code-to-name-bytes* table
95 (* (length *compressed-data*) *code-ndx-size*))
96
97 (defparameter *word-dictionary*
98 (+ *words-array-bytes*))
99
100 (format t "
101 ;;; Codes dictionary = ~D bytes
102 ;;; Pair table size = ~D bytes
103 ;;; Code to names table = ~D bytes
104 ;;; Names to codes table = ~D bytes
105 ;;; Total = ~D bytes
106 "
107 *word-dictionary*
108 *pair-table-size*
109 *code-to-name-bytes*
110 *sorted-names-bytes*
111 (+
112 *word-dictionary*
113 *pair-table-size*
114 *code-to-name-bytes*
115 *sorted-names-bytes*
116 ))
117
118 ;;; WITH HANGUL
119 ;;; Codes dictionary = 78566 bytes
120 ;;; Pair table size = 198752 bytes
121 ;;; Code to names table = 164570 bytes
122 ;;; Names to codes table = 65828 bytes
123 ;;; Total = 507716 bytes
124
125 ;;; WITHOUT HANGUL
126 ;;; Codes dictionary = 78555 bytes
127 ;;; Pair table size = 150868 bytes
128 ;;; Code to names table = 108710 bytes
129 ;;; Names to codes table = 43484 bytes
130 ;;; Total = 381617 bytes
131
132 ;;; Without HANGUL (split by space and -)
133 ;;; Codes dictionary = 58258 bytes
134 ;;; Pair table size = 160576 bytes
135 ;;; Code to names table = 108710 bytes
136 ;;; Names to codes table = 43484 bytes
137 ;;; Total = 371028 bytes
138
139 ;;; With HANGUL (split by space and -)
140 ;;; Codes dictionary = 58269 bytes
141 ;;; Pair table size = 208460 bytes
142 ;;; Code to names table = 164570 bytes
143 ;;; Names to codes table = 65828 bytes
144 ;;; Total = 497127 bytes