Debian Codegraph @

contrib/unicode/load-names.lisp

raw
1 ;;; Load names of all unicode characters from the Unicode Character
2 ;;; Database
3 ;;;
4 ;;; The names are compressed by splitting up the name into words and
5 ;;; encoding each word by an index in a hash table. The data is then
6 ;;; stored in the list *data* in the following format
7 ;;;
8 ;;; (char-code name index1 index2 ...)
9 ;;;
10 ;;; For example `0` and `1` are encoded as
11 ;;; (48 "DIGIT_ZERO" 22 23)
12 ;;; (49 "DIGIT_ONE" 22 24)
13 ;;; in *data*. 22 is the index for the word "DIGIT" while 23 and 24
14 ;;; are the indices for "ZERO" and "ONE" respectively
15 (defun split-words (text &key (set '(#\Space)) (exclude t))
16 (loop with start = 0
17 with output = '()
18 with elt-type = (array-element-type text)
19 for i from 0 below (length text)
20 for c across text
21 when (member c set)
22 do (setf output (list* (make-array (+ (- i start) (if exclude 0 1))
23 :element-type elt-type
24 :displaced-to text
25 :displaced-index-offset start)
26 output)
27 start (1+ i))
28 finally (return (nreverse (list* (make-array (- i start)
29 :element-type elt-type
30 :displaced-to text
31 :displaced-index-offset start)
32 output)))))
33
34 (defun encode-words (words hash)
35 (loop for word in words
36 collect (or (gethash word hash)
37 (let* ((word (copy-seq word))
38 (ndx (hash-table-count hash)))
39 (setf (gethash word hash) (1+ ndx))))))
40
41 (defun fixup-hangul-syllables (dictionary)
42 ;; "Hangul Syllable Composition, Unicode 5.1 section 3-12"
43 (let* ((sbase #xac00)
44 (lbase #x1100)
45 (vbase #x1161)
46 (tbase #x11a7)
47 (scount 11172)
48 (lcount 19)
49 (vcount 21)
50 (tcount 28)
51 (ncount (* vcount tcount))
52 (table (make-hash-table)))
53 (with-open-file (*standard-input*
54 (make-pathname :name "Jamo" :type "txt"))
55 (loop for line = (read-line nil nil)
56 while line
57 if (position #\; line)
58 do (add-jamo-information line table)))
59 (loop for sindex from 0 below scount
60 for l = (+ lbase (floor sindex ncount))
61 for v = (+ vbase (floor (mod sindex ncount) tcount))
62 for tee = (+ tbase (mod sindex tcount))
63 for name = (list* "HANGUL_" "SYLLABLE_"
64 (gethash l table) (gethash v table)
65 (unless (= tee tbase) (list (gethash tee table))))
66 for code = (+ sbase sindex)
67 collect (list* code (apply #'concatenate 'string name)
68 (encode-words name dictionary)))))
69
70 (defun add-jamo-information (line table)
71 (let* ((split (split-words line :set '(#\;) :exclude t))
72 (code (parse-integer (first split) :radix 16))
73 (syllable (string-trim '(#\Space)
74 (subseq (second split) 0 (position #\# (second split))))))
75 (setf (gethash code table) syllable)))
76
77 (defvar *words*)
78
79 (defparameter *data*
80 (with-open-file (in "UnicodeData.txt" :direction :input)
81 (loop with words = (setf *words* (make-hash-table :size 1024 :test #'equal))
82 for ucd-line = (read-line in nil nil nil)
83 while ucd-line
84 nconc (let* ((ucd-data (split-words ucd-line :set '(#\;)))
85 (code (first ucd-data))
86 (name (second ucd-data)))
87 (unless (eql (char name 0) #\<)
88 (setf name (substitute #\_ #\Space name))
89 (list (list* (parse-integer code :radix 16)
90 name
91 (encode-words (split-words
92 name
93 :set '(#\Space #\_ #\-)
94 :exclude nil)
95 words))))))))
96
97 (print (length *data*))
98 (print (first (last *data*)))
99
100 ;#+(or)
101 (progn
102 (setf *data*
103 (sort (nconc (fixup-hangul-syllables *words*) *data*)
104 #'<
105 :key #'car))
106 (print (length *data*))
107 (print (first (last *data*))))
108
109 (defparameter *words-array*
110 (loop with array = (make-array (1+ (hash-table-count *words*)))
111 for k being the hash-key in *words* using (hash-value v)
112 do (setf (aref array v) k)
113 finally (return array)))
114
115 (defparameter *last-word-index* (1- (length *words-array*)))
116
117 (defparameter *words-array-bytes*
118 (loop for c across *words-array*
119 sum (1+ (length c))))
120
121 (defun code-to-string (code)
122 (aref *words-array* code))
123
124 (defparameter *flattened-data*
125 (loop for (code name . rest) in *data*
126 nconc (append rest (list 0))))
127
128 (defparameter *group-names*
129 (loop with output = '()
130 with start = (first (first *data*))
131 with last = start
132 for (code name . rest) in *data*
133 do (when (>= (- code last) 2)
134 (setf output (cons (list start last) output)
135 start code))
136 (setf last code)
137 finally (return (nreverse (cons (list start code) output)))))