1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit unicode.categories kernel math
4 combinators splitting sequences math.parser io.files io assocs
5 arrays namespaces make math.ranges unicode.normalize
6 unicode.normalize.private values io.encodings.ascii
7 unicode.data compiler.units fry unicode.categories.syntax
8 alien.syntax sets accessors interval-maps memoize locals words
15 C-ENUM: Any L V T LV LVT Extend Control CR LF
16 SpacingMark Prepend graphemes ;
18 : jamo-class ( ch -- class )
19 dup initial? [ drop L ]
20 [ dup medial? [ drop V ] [ final? T Any ? ] if ] if ;
22 : hangul-class ( ch -- class )
23 hangul-base - HEX: 1C mod zero? LV LVT ? ;
25 CATEGORY: grapheme-control Zl Zp Cc Cf ;
26 : control-class ( ch -- class )
30 { HEX: 200C [ Extend ] }
31 { HEX: 200D [ Extend ] }
37 "Other_Grapheme_Extend" property? ;
40 "Logical_Order_Exception" property? ;
42 CATEGORY: spacing Mc ;
44 : grapheme-class ( ch -- class )
46 { [ dup jamo? ] [ jamo-class ] }
47 { [ dup hangul? ] [ hangul-class ] }
48 { [ dup grapheme-control? ] [ control-class ] }
49 { [ dup extend? ] [ drop Extend ] }
50 { [ dup spacing? ] [ drop SpacingMark ] }
51 { [ loe? ] [ Prepend ] }
55 : init-table ( size -- table )
56 dup [ f <array> ] curry replicate ;
60 : finish-table ( -- table )
61 table get [ [ 1 = ] map ] map ;
63 : eval-seq ( seq -- seq ) [ ?execute ] map ;
65 : (set-table) ( class1 class2 val -- )
66 [ table get nth ] dip '[ _ or ] change-nth ;
68 : set-table ( classes1 classes2 val -- )
69 [ [ eval-seq ] bi@ ] dip
70 [ [ (set-table) ] curry with each ] 2curry each ;
72 : connect ( class1 class2 -- ) 1 set-table ;
73 : disconnect ( class1 class2 -- ) 0 set-table ;
75 : make-grapheme-table ( -- )
77 { Control CR LF } graphemes disconnect
78 graphemes { Control CR LF } disconnect
79 { L } { L V LV LVT } connect
80 { LV V } { V T } connect
81 { LVT T } { T } connect
82 graphemes { Extend } connect
83 graphemes { SpacingMark } connect
84 { Prepend } graphemes connect ;
88 : grapheme-break? ( class1 class2 -- ? )
89 grapheme-table nth nth not ;
93 : first-grapheme ( str -- i )
94 unclip-slice grapheme-class over
95 [ grapheme-class [ nip ] [ grapheme-break? ] 2bi ] find drop
96 nip swap length or 1 + ;
98 : first-grapheme-from ( start str -- i )
99 over tail-slice first-grapheme + ;
101 : last-grapheme ( str -- i )
102 unclip-last-slice grapheme-class swap
103 [ grapheme-class dup rot grapheme-break? ] find-last drop ?1+ nip ;
105 : last-grapheme-from ( end str -- i )
106 swap head-slice last-grapheme ;
110 : >pieces ( str quot: ( str -- i ) -- graphemes )
111 [ dup empty? not ] swap '[ dup @ cut-slice swap ] produce nip ; inline
115 : >graphemes ( str -- graphemes )
116 [ first-grapheme ] >pieces ;
118 : string-reverse ( str -- rts )
119 >graphemes reverse concat ;
123 graphemes init-table table
124 [ make-grapheme-table finish-table ] with-variable
129 VALUE: word-break-table
131 "vocab:unicode/data/WordBreakProperty.txt" load-interval-file
134 C-ENUM: wOther wCR wLF wNewline wExtend wFormat wKatakana wALetter wMidLetter
135 wMidNum wMidNumLet wNumeric wExtendNumLet words ;
137 : word-break-classes ( -- table ) ! Is there a way to avoid this?
139 { "Other" 0 } { "CR" 1 } { "LF" 2 } { "Newline" 3 }
140 { "Extend" 4 } { "Format" 5 } { "Katakana" 6 }
141 { "ALetter" 7 } { "MidLetter" 8 }
142 { "MidNum" 9 } { "MidNumLet" 10 } { "Numeric" 11 }
143 { "ExtendNumLet" 12 }
146 : word-break-prop ( char -- word-break-prop )
147 word-break-table interval-at
148 word-break-classes at [ wOther ] unless* ;
150 SYMBOL: check-letter-before
151 SYMBOL: check-letter-after
152 SYMBOL: check-number-before
153 SYMBOL: check-number-after
155 : make-word-table ( -- )
156 { wCR } { wLF } connect
157 { wNewline wCR wLF } words disconnect
158 words { wNewline wCR wLF } disconnect
159 { wALetter } { wMidLetter wMidNumLet } check-letter-after set-table
160 { wMidLetter wMidNumLet } { wALetter } check-letter-before set-table
161 { wNumeric wALetter } { wNumeric wALetter } connect
162 { wNumeric } { wMidNum wMidNumLet } check-number-after set-table
163 { wMidNum wMidNumLet } { wNumeric } check-number-before set-table
164 { wKatakana } { wKatakana } connect
165 { wALetter wNumeric wKatakana wExtendNumLet } { wExtendNumLet }
166 [ connect ] [ swap connect ] 2bi ;
170 : finish-word-table ( -- table )
172 [ { { 0 [ f ] } { 1 [ t ] } [ ] } case ] map
175 words init-table table
176 [ make-word-table finish-word-table ] with-variable
179 : word-table-nth ( class1 class2 -- ? )
182 :: property-not= ( str i property -- ? )
184 i str ?nth [ word-break-prop property = not ]
188 : format/extended? ( ch -- ? )
189 word-break-prop { 4 5 } member? ;
191 : (walk-up) ( str i -- j )
192 swap [ format/extended? not ] find-from drop ;
194 : walk-up ( str i -- j )
195 dupd 1 + (walk-up) [ 1 + (walk-up) ] [ drop f ] if* ;
197 : (walk-down) ( str i -- j )
198 swap [ format/extended? not ] find-last-from drop ;
200 : walk-down ( str i -- j )
201 dupd (walk-down) [ 1 - (walk-down) ] [ drop f ] if* ;
203 : word-break? ( str i table-entry -- ? )
208 [ dupd walk-up wALetter property-not= ] }
209 { check-letter-before
210 [ dupd walk-down wALetter property-not= ] }
212 [ dupd walk-up wNumeric property-not= ] }
213 { check-number-before
214 [ dupd walk-down wNumeric property-not= ] }
217 :: word-break-next ( old-class new-char i str -- next-class ? )
218 new-char format/extended?
219 [ old-class dup { 1 2 3 } member? ] [
220 new-char word-break-prop old-class over word-table-nth
221 [ str i ] dip word-break?
226 : first-word ( str -- i )
227 [ unclip-slice word-break-prop over <enum> ] keep
228 '[ swap _ word-break-next ] assoc-find 2drop
229 nip swap length or 1 + ;
231 : >words ( str -- words )
232 [ first-word ] >pieces ;
236 : nth-next ( i str -- str[i-1] str[i] )
237 [ [ 1 - ] keep ] dip '[ _ nth ] bi@ ;
241 : word-break-at? ( i str -- ? )
246 [ nth-next [ word-break-prop ] dip ] 2keep
251 : first-word-from ( start str -- i )
252 over tail-slice first-word + ;
254 : last-word ( str -- i )
255 [ length ] keep '[ _ word-break-at? ] find-last drop 0 or ;
257 : last-word-from ( end str -- i )
258 swap head-slice last-word ;