factor/basis/unicode/collation/collation.factor

160 lines
4.6 KiB
Factor
Raw Normal View History

2008-10-05 19:36:56 -04:00
! Copyright (C) 2008 Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
2011-11-29 00:26:06 -05:00
USING: sequences io.files io.encodings.ascii kernel splitting
accessors math.parser ascii io assocs strings math namespaces make
sorting combinators math.order arrays unicode.normalize unicode.data
locals macros sequences.deep words unicode.breaks quotations
combinators.short-circuit simple-flat-file ;
2008-05-20 17:57:53 -04:00
IN: unicode.collation
2008-06-01 12:24:17 -04:00
<PRIVATE
2011-11-29 00:26:06 -05:00
SYMBOL: ducet
2008-05-20 17:57:53 -04:00
TUPLE: weight primary secondary tertiary ignorable? ;
: parse-weight ( string -- weight )
"]" split but-last [
weight new swap rest unclip CHAR: * = swapd >>ignorable?
swap "." split first3 [ hex> ] tri@
[ >>primary ] [ >>secondary ] [ >>tertiary ] tri*
] map ;
: parse-keys ( string -- chars )
" " split [ hex> ] "" map-as ;
2008-05-20 17:57:53 -04:00
: parse-ducet ( file -- ducet )
data [ [ parse-keys ] [ parse-weight ] bi* ] H{ } assoc-map-as ;
2008-05-20 17:57:53 -04:00
2011-11-29 00:26:06 -05:00
"vocab:unicode/collation/allkeys.txt" parse-ducet ducet set-global
2008-05-20 17:57:53 -04:00
! Fix up table for long contractions
: help-one ( assoc key -- )
! Need to be more general? Not for DUCET, apparently
2 head 2dup swap key? [ 2drop ] [
[ [ 1string of ] with { } map-as concat ]
[ swap set-at ] 2bi
] if ;
: insert-helpers ( assoc -- )
dup keys [ length 3 >= ] filter
[ help-one ] with each ;
2011-11-29 00:26:06 -05:00
ducet get-global insert-helpers
: base ( char -- base )
{
2011-11-23 21:49:33 -05:00
{ [ dup 0x3400 0x4DB5 between? ] [ drop 0xFB80 ] } ! Extension A
{ [ dup 0x20000 0x2A6D6 between? ] [ drop 0xFB80 ] } ! Extension B
{ [ dup 0x4E00 0x9FC3 between? ] [ drop 0xFB40 ] } ! CJK
[ drop 0xFBC0 ] ! Other
} cond ;
: AAAA ( char -- weight )
2011-11-23 21:49:33 -05:00
[ base ] [ -15 shift ] bi + 0x20 2 f weight boa ;
: BBBB ( char -- weight )
2011-11-23 21:49:33 -05:00
0x7FFF bitand 0x8000 bitor 0 0 f weight boa ;
: illegal? ( char -- ? )
{ [ "Noncharacter_Code_Point" property? ] [ category "Cs" = ] } 1|| ;
: derive-weight ( char -- weights )
first dup illegal?
[ drop { } ]
[ [ AAAA ] [ BBBB ] bi 2array ] if ;
2008-05-20 17:57:53 -04:00
: building-last ( -- char )
building get empty? [ 0 ] [ building get last last ] if ;
2008-05-24 13:17:08 -04:00
: blocked? ( char -- ? )
2009-01-11 20:41:48 -05:00
combining-class dup { 0 f } member?
[ drop building-last non-starter? ]
[ building-last combining-class = ] if ;
2008-05-24 13:17:08 -04:00
: possible-bases ( -- slice-of-building )
2009-01-11 20:41:48 -05:00
building get dup [ first non-starter? not ] find-last
2008-05-24 13:17:08 -04:00
drop [ 0 ] unless* tail-slice ;
:: ?combine ( char slice i -- ? )
2009-10-27 22:50:31 -04:00
i slice nth char suffix :> str
2011-11-29 00:26:06 -05:00
str ducet get-global key? dup
2009-10-27 22:50:31 -04:00
[ str i slice set-nth ] when ;
2008-05-24 13:17:08 -04:00
: add ( char -- )
dup blocked? [ 1string , ] [
2010-01-14 10:10:13 -05:00
dup possible-bases dup length iota
[ ?combine ] 2with any?
2008-05-24 13:17:08 -04:00
[ drop ] [ 1string , ] if
] if ;
: string>graphemes ( string -- graphemes )
[ [ add ] each ] { } make ;
2008-05-24 13:17:08 -04:00
: graphemes>weights ( graphemes -- weights )
[
dup weight? [ 1array ] ! From tailoring
2011-11-29 00:26:06 -05:00
[ dup ducet get-global at [ ] [ derive-weight ] ?if ] if
] { } map-as concat ;
2008-05-20 17:57:53 -04:00
: append-weights ( weights quot -- )
[ [ ignorable?>> ] reject ] dip
map [ zero? ] reject % 0 , ; inline
2008-05-20 17:57:53 -04:00
: variable-weight ( weight -- )
2011-11-23 21:49:33 -05:00
dup ignorable?>> [ primary>> ] [ drop 0xFFFF ] if , ;
2008-05-20 17:57:53 -04:00
: weights>bytes ( weights -- byte-array )
[
{
[ [ primary>> ] append-weights ]
[ [ secondary>> ] append-weights ]
[ [ tertiary>> ] append-weights ]
[ [ variable-weight ] each ]
} cleave
] { } make ;
2008-06-01 12:24:17 -04:00
PRIVATE>
2008-05-20 17:57:53 -04:00
: completely-ignorable? ( weight -- ? )
[ primary>> ] [ secondary>> ] [ tertiary>> ] tri
[ zero? ] tri@ and and ;
: filter-ignorable ( weights -- weights' )
2008-12-04 11:19:18 -05:00
f swap [
2009-01-23 19:20:47 -05:00
[ nip ] [ primary>> zero? and ] 2bi
2008-05-20 17:57:53 -04:00
[ swap ignorable?>> or ]
[ swap completely-ignorable? or not ] 2bi
] filter nip ;
: collation-key ( string -- key )
2008-05-25 21:21:39 -04:00
nfd string>graphemes graphemes>weights
filter-ignorable weights>bytes ;
2008-06-01 12:24:17 -04:00
<PRIVATE
2008-05-25 21:21:39 -04:00
: insensitive= ( str1 str2 levels-removed -- ? )
[
[ collation-key ] dip
[ [ 0 = not ] trim-tail but-last ] times
2012-07-21 13:22:44 -04:00
] curry same? ;
2008-06-01 12:24:17 -04:00
PRIVATE>
: primary= ( str1 str2 -- ? )
2008-05-25 21:21:39 -04:00
3 insensitive= ;
: secondary= ( str1 str2 -- ? )
2008-05-25 21:21:39 -04:00
2 insensitive= ;
: tertiary= ( str1 str2 -- ? )
2008-05-25 21:21:39 -04:00
1 insensitive= ;
: quaternary= ( str1 str2 -- ? )
2008-05-25 21:21:39 -04:00
0 insensitive= ;
2008-05-20 17:57:53 -04:00
: w/collation-key ( str -- {str,key} )
2008-06-01 14:50:12 -04:00
[ collation-key ] keep 2array ;
2008-05-20 17:57:53 -04:00
: sort-strings ( strings -- sorted )
[ w/collation-key ] map natural-sort values ;
2008-05-20 17:57:53 -04:00
: string<=> ( str1 str2 -- <=> )
2008-06-01 14:50:12 -04:00
[ w/collation-key ] compare ;