1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit combinators.smart fry kernel locals make
5 math math.order math.parser namespaces sequences
6 simple-flat-file splitting strings unicode.data ;
13 TUPLE: weight-levels primary secondary tertiary ignorable? ;
14 : <weight-levels> ( primary secondary tertiary -- weight-levels> )
18 swap >>primary ; inline
20 : parse-weight ( string -- weight )
22 weight-levels new swap rest unclip CHAR: * = swapd >>ignorable?
23 swap "." split first3 [ hex> ] tri@
24 [ >>primary ] [ >>secondary ] [ >>tertiary ] tri*
27 : parse-keys ( string -- chars )
28 " " split [ hex> ] "" map-as ;
30 : parse-ducet ( file -- ducet )
31 load-data-file [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;
33 "vocab:unicode/UCA/allkeys.txt" parse-ducet ducet set-global
35 ! https://www.unicode.org/reports/tr10/tr10-41.html#Well_Formed_DUCET
36 ! WF5 - Well-formedness 5 condition:
37 ! https://www.unicode.org/reports/tr10/tr10-41.html#WF5
38 ! { "0CC6" "0CC2" "0CD5" } ! 0CD5 is not a non-starter, don't add 2-gram "0CC6" "0CC2"to ducet
39 ! { "0DD9" "0DCF" "0DCA" } ! already in allkeys.txt file
40 ! { "0FB2" "0F71" "0F80" } ! added below
41 ! { "0FB3" "0F71" "0F80" } ! added below
42 ! This breaks the unicode tests that ship in CollationTest_SHIFTED.txt
43 ! but it's supposedly more correct.
44 : fixup-ducet-for-tibetan ( -- )
47 { 0x0FB2 0x0F71 } ! CE(0FB2) CE(0F71)
62 { 0x0FB3 0x0F71 } ! CE(0FB3) CE(0F71)
78 { 0x0FB2 0x0F71 0x0F72 } ! CE(0FB2) CE(0F71 0F72)
93 { 0x0FB2 0x0F73 } ! CE(0FB2) CE(0F71 0F72)
108 { 0x0FB2 0x0F71 0x0F74 } ! CE(0FB2) CE(0F71 0F74)
123 { 0x0FB2 0x0F75 } ! CE(0FB2) CE(0F71 0F74)
138 { 0x0FB3 0x0F71 0x0F72 } ! CE(0FB3) CE(0F71 0F72)
153 { 0x0FB3 0x0F73 } ! CE(0FB3) CE(0F71 0F72)
168 { 0x0FB3 0x0F71 0x0F74 } ! CE(0FB3) CE(0F71 0F74)
183 { 0x0FB3 0x0F75 } ! CE(0FB3) CE(0F71 0F74)
197 } ducet get-global '[ swap >string _ set-at ] assoc-each ;
199 ! These values actually break the collation unit tests in CollationTest_SHIFTED.txt
200 ! So we disable those tests in favor of supposedly better collation for Tibetan.
201 ! https://www.unicode.org/reports/tr10/tr10-41.html#Well_Formed_DUCET
203 fixup-ducet-for-tibetan
205 : tangut-block? ( char -- ? )
206 ! Tangut Block, Tangut Components Block
207 { [ 0x17000 0x187FF between? ] [ 0x18800 0x18AFF between? ] } 1|| ; inline
209 : nushu-block? ( char -- ? )
210 0x1b170 0x1B2FB between? ; inline
212 ! https://wiki.computercraft.cc/Module:Unicode_data
213 ! Unicode TR10 - Computing Implicit Weights
214 : base ( char -- base )
216 { [ dup 0x03400 0x04DB5 between? ] [ drop 0xFB80 ] } ! Extension A
217 { [ dup 0x20000 0x2A6D6 between? ] [ drop 0xFB80 ] } ! Extension B
218 { [ dup 0x2A700 0x2B734 between? ] [ drop 0xFB80 ] } ! Extension C
219 { [ dup 0x2B740 0x2B81D between? ] [ drop 0xFB80 ] } ! Extension D
220 { [ dup 0x2B820 0x2CEA1 between? ] [ drop 0xFB80 ] } ! Extension E
221 { [ dup 0x2CEB0 0x2EBE0 between? ] [ drop 0xFB80 ] } ! Extension F
222 { [ dup 0x04E00 0x09FEF between? ] [ drop 0xFB40 ] } ! CJK
223 [ drop 0xFBC0 ] ! Other
226 : tangut-AAAA ( char -- weight-levels )
227 drop 0xfb00 0x0020 0x0002 <weight-levels> ; inline
229 : tangut-BBBB ( char -- weight-levels )
230 0x17000 - 0x8000 bitor 0 0 <weight-levels> ; inline
232 : nushu-AAAA ( char -- weight-levels )
233 drop 0xfb01 0x0020 0x0002 <weight-levels> ; inline
235 : nushu-BBBB ( char -- weight-levels )
236 0x1B170 - 0x8000 bitor 0 0 <weight-levels> ; inline
238 : AAAA ( char -- weight-levels )
239 [ base ] [ -15 shift ] bi + 0x0020 0x0002 <weight-levels> ; inline
241 : BBBB ( char -- weight-levels )
242 0x7FFF bitand 0x8000 bitor 0 0 <weight-levels> ; inline
244 : derive-weight ( 1string -- weight-levels-pair )
247 { [ dup tangut-block? ] [ [ tangut-AAAA ] [ tangut-BBBB ] bi 2array ] }
248 { [ dup nushu-block? ] [ [ nushu-AAAA ] [ nushu-BBBB ] bi 2array ] }
249 [ [ AAAA ] [ BBBB ] bi 2array ]
252 : building-last ( -- char )
253 building get [ 0 ] [ last last ] if-empty ;
255 ! https://www.unicode.org/reports/tr10/tr10-41.html#Collation_Graphemes
256 : blocked? ( char -- ? )
257 combining-class dup { 0 f } member?
258 [ drop building-last non-starter? ]
259 [ building-last combining-class = ] if ;
261 : possible-bases ( -- slice-of-building )
262 building get dup [ first non-starter? not ] find-last
263 drop [ 0 ] unless* tail-slice ;
265 :: ?combine ( char slice i -- ? )
266 i slice nth char suffix :> str
267 str ducet get-global key? dup
268 [ str i slice set-nth ] when ;
271 dup blocked? [ 1string , ] [
272 dup possible-bases dup length <iota>
273 [ ?combine ] 2with any?
274 [ drop ] [ 1string , ] if
277 : string>graphemes ( string -- graphemes )
278 [ [ add ] each ] { } make ;
280 : char>weight-levels ( 1string -- weight-levels )
281 ducet get-global ?at [ derive-weight ] unless ; inline
283 : graphemes>weights ( graphemes -- weights )
286 [ 1array ] ! From tailoring
287 [ char>weight-levels ] if
288 ] { } map-as concat ;
290 : append-weights ( weight-levels quot -- seq )
291 [ [ ignorable?>> ] reject ] dip
292 map [ zero? ] reject ; inline
294 : variable-weight ( weight-levels -- obj )
295 dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if ;
297 : weights>bytes ( weights -- array )
300 [ [ primary>> ] append-weights { 0 } ]
301 [ [ secondary>> ] append-weights { 0 } ]
302 [ [ tertiary>> ] append-weights { 0 } ]
303 [ [ [ secondary>> ] [ tertiary>> ] bi [ zero? ] bi@ and not ] filter [ variable-weight ] map ]
305 ] { } append-outputs-as ;
309 : completely-ignorable? ( weight -- ? )
312 [ secondary>> zero? ]
316 : filter-ignorable ( weights -- weights' )
318 [ nip ] [ primary>> zero? and ] 2bi
319 [ swap ignorable?>> or ]
320 [ swap completely-ignorable? or not ] 2bi