]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/breaks/breaks.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / unicode / breaks / breaks.factor
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
9 simple-flat-file ;
10 IN: unicode.breaks
11
12 <PRIVATE
13 ! Grapheme breaks
14
15 C-ENUM: Any L V T LV LVT Extend Control CR LF
16     SpacingMark Prepend graphemes ;
17
18 : jamo-class ( ch -- class )
19     dup initial? [ drop L ]
20     [ dup medial? [ drop V ] [ final? T Any ? ] if ] if ;
21
22 : hangul-class ( ch -- class )
23     hangul-base - HEX: 1C mod zero? LV LVT ? ;
24
25 CATEGORY: grapheme-control Zl Zp Cc Cf ;
26 : control-class ( ch -- class )
27     {
28         { CHAR: \r [ CR ] }
29         { CHAR: \n [ LF ] }
30         { HEX: 200C [ Extend ] }
31         { HEX: 200D [ Extend ] }
32         [ drop Control ]
33     } case ;
34
35 CATEGORY: extend
36     Me Mn |
37     "Other_Grapheme_Extend" property? ;
38
39 : loe? ( ch -- ? )
40     "Logical_Order_Exception" property? ;
41
42 CATEGORY: spacing Mc ;
43
44 : grapheme-class ( ch -- class )
45     {
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 ] }
52         [ Any ]
53     } cond ;
54
55 : init-table ( size -- table )
56     dup [ f <array> ] curry replicate ;
57
58 SYMBOL: table
59
60 : finish-table ( -- table )
61     table get [ [ 1 = ] map ] map ;
62
63 : eval-seq ( seq -- seq ) [ ?execute ] map ;
64
65 : (set-table) ( class1 class2 val -- )
66     [ table get nth ] dip '[ _ or ] change-nth ;
67
68 : set-table ( classes1 classes2 val -- )
69     [ [ eval-seq ] bi@ ] dip
70     [ [ (set-table) ] curry with each ] 2curry each ;
71
72 : connect ( class1 class2 -- ) 1 set-table ;
73 : disconnect ( class1 class2 -- ) 0 set-table ;
74   
75 : break-around ( classes1 classes2 -- )
76     [ disconnect ] [ swap disconnect ] 2bi ;
77
78 : make-grapheme-table ( -- )
79     { CR } { LF } connect
80     { Control CR LF } graphemes disconnect
81     graphemes { Control CR LF } disconnect
82     { L } { L V LV LVT } connect
83     { LV V } { V T } connect
84     { LVT T } { T } connect
85     graphemes { Extend } connect
86     graphemes { SpacingMark } connect
87     { Prepend } graphemes connect ;
88
89 VALUE: grapheme-table
90
91 : grapheme-break? ( class1 class2 -- ? )
92     grapheme-table nth nth not ;
93
94 : chars ( i str n -- str[i] str[i+n] )
95     swap [ dupd + ] dip [ ?nth ] curry bi@ ;
96
97 PRIVATE>
98
99 : first-grapheme ( str -- i )
100     unclip-slice grapheme-class over
101     [ grapheme-class [ nip ] [ grapheme-break? ] 2bi ] find drop
102     nip swap length or 1+ ;
103
104 : first-grapheme-from ( start str -- i )
105     over tail-slice first-grapheme + ;
106
107 : last-grapheme ( str -- i )
108     unclip-last-slice grapheme-class swap
109     [ grapheme-class dup rot grapheme-break? ] find-last drop ?1+ nip ;
110
111 : last-grapheme-from ( end str -- i )
112     swap head-slice last-grapheme ;
113
114 <PRIVATE
115
116 : >pieces ( str quot: ( str -- i ) -- graphemes )
117     [ dup empty? not ] swap '[ dup @ cut-slice swap ] produce nip ; inline
118
119 PRIVATE>
120
121 : >graphemes ( str -- graphemes )
122     [ first-grapheme ] >pieces ;
123
124 : string-reverse ( str -- rts )
125     >graphemes reverse concat ;
126
127 <PRIVATE
128
129 graphemes init-table table
130 [ make-grapheme-table finish-table ] with-variable
131 to: grapheme-table
132
133 ! Word breaks
134
135 VALUE: word-break-table
136
137 "vocab:unicode/data/WordBreakProperty.txt" load-interval-file
138 to: word-break-table
139
140 C-ENUM: wOther wCR wLF wNewline wExtend wFormat wKatakana wALetter wMidLetter
141 wMidNum wMidNumLet wNumeric wExtendNumLet words ;
142
143 : word-break-classes ( -- table ) ! Is there a way to avoid this?
144     H{
145         { "Other" 0 } { "CR" 1 } { "LF" 2 } { "Newline" 3 }
146         { "Extend" 4 } { "Format" 5 } { "Katakana" 6 }
147         { "ALetter" 7 } { "MidLetter" 8 }
148         { "MidNum" 9 } { "MidNumLet" 10 } { "Numeric" 11 }
149         { "ExtendNumLet" 12 }
150     } ;
151
152 : word-break-prop ( char -- word-break-prop )
153     word-break-table interval-at
154     word-break-classes at [ wOther ] unless* ;
155
156 SYMBOL: check-letter-before
157 SYMBOL: check-letter-after
158 SYMBOL: check-number-before
159 SYMBOL: check-number-after
160
161 : make-word-table ( -- )
162     { wCR } { wLF } connect
163     { wNewline wCR wLF } words disconnect
164     words { wNewline wCR wLF } disconnect
165     { wALetter } { wMidLetter wMidNumLet } check-letter-after set-table
166     { wMidLetter wMidNumLet } { wALetter } check-letter-before set-table
167     { wNumeric wALetter } { wNumeric wALetter } connect
168     { wNumeric } { wMidNum wMidNumLet } check-number-after set-table
169     { wMidNum wMidNumLet } { wNumeric } check-number-before set-table
170     { wKatakana } { wKatakana } connect
171     { wALetter wNumeric wKatakana wExtendNumLet } { wExtendNumLet }
172     [ connect ] [ swap connect ] 2bi ;
173
174 VALUE: word-table
175
176 : finish-word-table ( -- table )
177     table get [
178         [ { { 0 [ f ] } { 1 [ t ] } [ ] } case ] map
179     ] map ;
180
181 words init-table table
182 [ make-word-table finish-word-table ] with-variable
183 to: word-table
184
185 : word-table-nth ( class1 class2 -- ? )
186     word-table nth nth ;
187
188 :: property-not= ( str i property -- ? )
189     i [
190         i str ?nth [ word-break-prop property = not ]
191         [ f ] if*
192     ] [ t ] if ;
193
194 : format/extended? ( ch -- ? )
195     word-break-prop { 4 5 } member? ;
196
197 : (walk-up) ( str i -- j )
198     swap [ format/extended? not ] find-from drop ;
199
200 : walk-up ( str i -- j )
201     dupd 1+ (walk-up) [ 1+ (walk-up) ] [ drop f ] if* ;
202
203 : (walk-down) ( str i -- j )
204     swap [ format/extended? not ] find-last-from drop ;
205
206 : walk-down ( str i -- j )
207     dupd (walk-down) [ 1- (walk-down) ] [ drop f ] if* ;
208
209 : word-break? ( str i table-entry -- ? )
210     {
211         { t [ 2drop f ] }
212         { f [ 2drop t ] }
213         { check-letter-after
214             [ dupd walk-up wALetter property-not= ] }
215         { check-letter-before
216             [ dupd walk-down wALetter property-not= ] }
217         { check-number-after
218             [ dupd walk-up wNumeric property-not= ] }
219         { check-number-before
220             [ dupd walk-down wNumeric property-not= ] }
221     } case ;
222
223 :: word-break-next ( old-class new-char i str -- next-class ? )
224     new-char format/extended?
225     [ old-class dup { 1 2 3 } member? ] [
226         new-char word-break-prop old-class over word-table-nth
227         [ str i ] dip word-break?
228     ] if ;
229
230 PRIVATE>
231
232 : first-word ( str -- i )
233     [ unclip-slice word-break-prop over <enum> ] keep
234     '[ swap _ word-break-next ] assoc-find 2drop
235     nip swap length or 1+ ;
236
237 : >words ( str -- words )
238     [ first-word ] >pieces ;
239
240 <PRIVATE
241
242 : nth-next ( i str -- str[i-1] str[i] )
243     [ [ 1- ] keep ] dip '[ _ nth ] bi@ ;
244
245 PRIVATE>
246
247 : word-break-at? ( i str -- ? )
248     {
249         [ drop zero? ]
250         [ length = ]
251         [
252             [ nth-next [ word-break-prop ] dip ] 2keep
253             word-break-next nip
254         ]
255     } 2|| ;