]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/collation/collation.factor
Fix conflict
[factor.git] / basis / unicode / collation / collation.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg.\r
2 ! See http://factorcode.org/license.txt for BSD license.\r
3 USING: sequences io.files io.encodings.ascii kernel values splitting\r
4 accessors math.parser ascii io assocs strings math namespaces make\r
5 sorting combinators math.order arrays unicode.normalize unicode.data\r
6 locals macros sequences.deep words unicode.breaks quotations\r
7 combinators.short-circuit simple-flat-file ;\r
8 IN: unicode.collation\r
9 \r
10 <PRIVATE\r
11 VALUE: ducet\r
12 \r
13 TUPLE: weight primary secondary tertiary ignorable? ;\r
14 \r
15 : parse-weight ( string -- weight )\r
16     "]" split but-last [\r
17         weight new swap rest unclip CHAR: * = swapd >>ignorable?\r
18         swap "." split first3 [ hex> ] tri@\r
19         [ >>primary ] [ >>secondary ] [ >>tertiary ] tri*\r
20     ] map ;\r
21 \r
22 : parse-keys ( string -- chars )\r
23     " " split [ hex> ] "" map-as ;\r
24 \r
25 : parse-ducet ( file -- ducet )\r
26     data [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;\r
27 \r
28 "vocab:unicode/collation/allkeys.txt" parse-ducet to: ducet\r
29 \r
30 ! Fix up table for long contractions\r
31 : help-one ( assoc key -- )\r
32     ! Need to be more general? Not for DUCET, apparently\r
33     2 head 2dup swap key? [ 2drop ] [\r
34         [ [ 1string swap at ] with { } map-as concat ]\r
35         [ swap set-at ] 2bi\r
36     ] if ;\r
37 \r
38 : insert-helpers ( assoc -- )\r
39     dup keys [ length 3 >= ] filter\r
40     [ help-one ] with each ;\r
41 \r
42 ducet insert-helpers\r
43 \r
44 : base ( char -- base )\r
45     {\r
46         { [ dup HEX: 3400 HEX:  4DB5 between? ] [ drop HEX: FB80 ] } ! Extension A\r
47         { [ dup HEX: 20000 HEX: 2A6D6 between? ] [ drop HEX: FB80 ] } ! Extension B\r
48         { [ dup HEX: 4E00 HEX: 9FC3 between? ] [ drop HEX: FB40 ] } ! CJK\r
49         [ drop HEX: FBC0 ] ! Other\r
50     } cond ;\r
51 \r
52 : AAAA ( char -- weight )\r
53     [ base ] [ -15 shift ] bi + HEX: 20 2 f weight boa ;\r
54 \r
55 : BBBB ( char -- weight )\r
56     HEX: 7FFF bitand HEX: 8000 bitor 0 0 f weight boa ;\r
57 \r
58 : illegal? ( char -- ? )\r
59     { [ "Noncharacter_Code_Point" property? ] [ category "Cs" = ] } 1|| ;\r
60 \r
61 : derive-weight ( char -- weights )\r
62     first dup illegal?\r
63     [ drop { } ]\r
64     [ [ AAAA ] [ BBBB ] bi 2array ] if ;\r
65 \r
66 : last ( -- char )\r
67     building get empty? [ 0 ] [ building get peek peek ] if ;\r
68 \r
69 : blocked? ( char -- ? )\r
70     combining-class dup { 0 f } member?\r
71     [ drop last non-starter? ]\r
72     [ last combining-class = ] if ;\r
73 \r
74 : possible-bases ( -- slice-of-building )\r
75     building get dup [ first non-starter? not ] find-last\r
76     drop [ 0 ] unless* tail-slice ;\r
77 \r
78 :: ?combine ( char slice i -- ? )\r
79     [let | str [ i slice nth char suffix ] |\r
80         str ducet key? dup\r
81         [ str i slice set-nth ] when\r
82     ] ;\r
83 \r
84 : add ( char -- )\r
85     dup blocked? [ 1string , ] [\r
86         dup possible-bases dup length\r
87         [ ?combine ] with with any?\r
88         [ drop ] [ 1string , ] if\r
89     ] if ;\r
90 \r
91 : string>graphemes ( string -- graphemes )\r
92     [ [ add ] each ] { } make ;\r
93 \r
94 : graphemes>weights ( graphemes -- weights )\r
95     [\r
96         dup weight? [ 1array ] ! From tailoring\r
97         [ dup ducet at [ ] [ derive-weight ] ?if ] if\r
98     ] { } map-as concat ;\r
99 \r
100 : append-weights ( weights quot -- )\r
101     [ [ ignorable?>> not ] filter ] dip\r
102     map [ zero? not ] filter % 0 , ; inline\r
103 \r
104 : variable-weight ( weight -- )\r
105     dup ignorable?>> [ primary>> ] [ drop HEX: FFFF ] if , ;\r
106 \r
107 : weights>bytes ( weights -- byte-array )\r
108     [\r
109         {\r
110             [ [ primary>> ] append-weights ]\r
111             [ [ secondary>> ] append-weights ]\r
112             [ [ tertiary>> ] append-weights ]\r
113             [ [ variable-weight ] each ]\r
114         } cleave\r
115     ] { } make ;\r
116 PRIVATE>\r
117 \r
118 : completely-ignorable? ( weight -- ? )\r
119     [ primary>> ] [ secondary>> ] [ tertiary>> ] tri\r
120     [ zero? ] tri@ and and ;\r
121 \r
122 : filter-ignorable ( weights -- weights' )\r
123     f swap [\r
124         [ nip ] [ primary>> zero? and ] 2bi\r
125         [ swap ignorable?>> or ]\r
126         [ swap completely-ignorable? or not ] 2bi\r
127     ] filter nip ;\r
128 \r
129 : collation-key ( string -- key )\r
130     nfd string>graphemes graphemes>weights\r
131     filter-ignorable weights>bytes ;\r
132 \r
133 <PRIVATE\r
134 : insensitive= ( str1 str2 levels-removed -- ? )\r
135     [\r
136         [ collation-key ] dip\r
137         [ [ 0 = not ] trim-tail but-last ] times\r
138     ] curry bi@ = ;\r
139 PRIVATE>\r
140 \r
141 : primary= ( str1 str2 -- ? )\r
142     3 insensitive= ;\r
143 \r
144 : secondary= ( str1 str2 -- ? )\r
145     2 insensitive= ;\r
146 \r
147 : tertiary= ( str1 str2 -- ? )\r
148     1 insensitive= ;\r
149 \r
150 : quaternary= ( str1 str2 -- ? )\r
151     0 insensitive= ;\r
152 \r
153 <PRIVATE\r
154 : w/collation-key ( str -- {str,key} )\r
155     [ collation-key ] keep 2array ;\r
156 PRIVATE>\r
157 \r
158 : sort-strings ( strings -- sorted )\r
159     [ w/collation-key ] map natural-sort values ;\r
160 \r
161 : string<=> ( str1 str2 -- <=> )\r
162     [ w/collation-key ] compare ;\r