1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators fry interval-maps
4 kernel literals locals math namespaces parser sequences
5 simple-flat-file unicode.categories unicode.data
6 unicode.normalize.private words words.constant ;
23 CONSTANT: SpacingMark 10
25 CONSTANT: graphemes 12
27 : jamo-class ( ch -- class )
28 dup initial? [ drop L ]
29 [ dup medial? [ drop V ] [ final? T Any ? ] if ] if ;
31 : hangul-class ( ch -- class )
32 hangul-base - 0x1C mod zero? LV LVT ? ;
34 CATEGORY: grapheme-control Zl Zp Cc Cf ;
35 : control-class ( ch -- class )
46 "Other_Grapheme_Extend" property? ;
49 "Logical_Order_Exception" property? ;
51 CATEGORY: spacing Mc ;
53 : grapheme-class ( ch -- class )
55 { [ dup jamo? ] [ jamo-class ] }
56 { [ dup hangul? ] [ hangul-class ] }
57 { [ dup grapheme-control? ] [ control-class ] }
58 { [ dup extend? ] [ drop Extend ] }
59 { [ dup spacing? ] [ drop SpacingMark ] }
60 { [ loe? ] [ Prepend ] }
64 : init-table ( size -- table )
65 dup [ f <array> ] curry replicate ;
69 : finish-table ( -- table )
70 table get [ [ 1 = ] map ] map ;
72 : eval-seq ( seq -- seq )
73 [ dup word? [ execute( -- x ) ] when ] map ;
75 : (set-table) ( class1 class2 val -- )
76 [ table get nth ] dip '[ _ or ] change-nth ;
78 : set-table ( classes1 classes2 val -- )
79 [ [ eval-seq ] bi@ ] dip
80 [ [ (set-table) ] curry with each ] 2curry each ;
82 : connect ( class1 class2 -- ) 1 set-table ;
83 : disconnect ( class1 class2 -- ) 0 set-table ;
85 : make-grapheme-table ( -- )
87 { Control CR LF } graphemes iota disconnect
88 graphemes iota { Control CR LF } disconnect
89 { L } { L V LV LVT } connect
90 { LV V } { V T } connect
91 { LVT T } { T } connect
92 graphemes iota { Extend } connect
93 graphemes iota { SpacingMark } connect
94 { Prepend } graphemes iota connect ;
96 "grapheme-table" create-word-in
97 graphemes init-table table
98 [ make-grapheme-table finish-table ] with-variable
102 : grapheme-break? ( class1 class2 -- ? )
103 grapheme-table nth nth not ;
113 CONSTANT: wKatakana 6
115 CONSTANT: wMidLetter 8
117 CONSTANT: wMidNumLet 10
118 CONSTANT: wNumeric 11
119 CONSTANT: wExtendNumLet 12
120 CONSTANT: unicode-words 13
122 ! Is there a way to avoid this?
123 CONSTANT: word-break-classes H{
124 { "Other" 0 } { "CR" 1 } { "LF" 2 } { "Newline" 3 }
125 { "Extend" 4 } { "Format" 5 } { "Katakana" 6 }
126 { "ALetter" 7 } { "MidLetter" 8 }
127 { "MidNum" 9 } { "MidNumLet" 10 } { "Numeric" 11 }
128 { "ExtendNumLet" 12 }
131 "word-break-table" create-word-in
132 "vocab:unicode/data/WordBreakProperty.txt"
133 load-interval-file dup array>>
134 [ 2 swap [ word-break-classes at ] change-nth ] each
138 : word-break-prop ( char -- word-break-prop )
139 word-break-table interval-at wOther or ;
142 SYMBOL: check-letter-before
143 SYMBOL: check-letter-after
144 SYMBOL: check-number-before
145 SYMBOL: check-number-after
147 : make-word-table ( -- )
148 { wCR } { wLF } connect
149 { wNewline wCR wLF } unicode-words iota disconnect
150 unicode-words iota { wNewline wCR wLF } disconnect
151 { wALetter } { wMidLetter wMidNumLet } check-letter-after set-table
152 { wMidLetter wMidNumLet } { wALetter } check-letter-before set-table
153 { wNumeric wALetter } { wNumeric wALetter } connect
154 { wNumeric } { wMidNum wMidNumLet } check-number-after set-table
155 { wMidNum wMidNumLet } { wNumeric } check-number-before set-table
156 { wKatakana } { wKatakana } connect
157 { wALetter wNumeric wKatakana wExtendNumLet } { wExtendNumLet }
158 [ connect ] [ swap connect ] 2bi ;
160 : finish-word-table ( -- table )
162 [ { { 0 [ f ] } { 1 [ t ] } [ ] } case ] map
165 "word-table" create-word-in
166 unicode-words init-table table
167 [ make-word-table finish-word-table ] with-variable
171 : word-table-nth ( class1 class2 -- ? )
174 :: property-not= ( str i property -- ? )
176 i str ?nth [ word-break-prop property = not ]
180 : (format/extended?) ( class -- ? )
181 ${ wExtend wFormat } member? ; inline
183 : format/extended? ( ch -- ? )
184 word-break-prop (format/extended?) ;
186 : (walk-up) ( str i -- j )
187 swap [ format/extended? not ] find-from drop ;
189 : walk-up ( str i -- j )
190 dupd 1 + (walk-up) [ 1 + (walk-up) ] [ drop f ] if* ;
192 : (walk-down) ( str i -- j )
193 swap [ format/extended? not ] find-last-from drop ;
195 : walk-down ( str i -- j )
196 dupd (walk-down) [ 1 - (walk-down) ] [ drop f ] if* ;
198 : word-break? ( str i table-entry -- ? )
203 [ dupd walk-up wALetter property-not= ] }
204 { check-letter-before
205 [ dupd walk-down wALetter property-not= ] }
207 [ dupd walk-up wNumeric property-not= ] }
208 { check-number-before
209 [ dupd walk-down wNumeric property-not= ] }
212 :: word-break-next ( old-class new-char i str -- next-class ? )
213 new-char word-break-prop :> new-class
214 new-class (format/extended?)
215 [ old-class dup ${ wCR wLF wNewline } member? ] [
216 new-class old-class over word-table-nth
217 [ str i 1 - ] dip word-break?