! Copyright (C) 2008 Daniel Ehrenberg. ! See http://factorcode.org/license.txt for BSD license. USING: accessors alien.syntax arrays assocs combinators combinators.short-circuit compiler.units fry interval-maps io io.encodings.ascii io.files kernel literals locals make math math.parser math.ranges memoize namespaces parser sequences sets simple-flat-file splitting unicode.categories unicode.categories.syntax unicode.data unicode.normalize unicode.normalize.private words words.constant ; FROM: sequences => change-nth ; IN: unicode.breaks ] curry replicate ; SYMBOL: table : finish-table ( -- table ) table get [ [ 1 = ] map ] map ; : eval-seq ( seq -- seq ) [ dup word? [ execute( -- x ) ] when ] map ; : (set-table) ( class1 class2 val -- ) [ table get nth ] dip '[ _ or ] change-nth ; : set-table ( classes1 classes2 val -- ) [ [ eval-seq ] bi@ ] dip [ [ (set-table) ] curry with each ] 2curry each ; : connect ( class1 class2 -- ) 1 set-table ; : disconnect ( class1 class2 -- ) 0 set-table ; : make-grapheme-table ( -- ) { CR } { LF } connect { Control CR LF } graphemes iota disconnect graphemes iota { Control CR LF } disconnect { L } { L V LV LVT } connect { LV V } { V T } connect { LVT T } { T } connect graphemes iota { Extend } connect graphemes iota { SpacingMark } connect { Prepend } graphemes iota connect ; "grapheme-table" create-in graphemes init-table table [ make-grapheme-table finish-table ] with-variable define-constant >> : grapheme-break? ( class1 class2 -- ? ) grapheme-table nth nth not ; PRIVATE> : first-grapheme ( str -- i ) unclip-slice grapheme-class over [ grapheme-class [ nip ] [ grapheme-break? ] 2bi ] find drop nip swap length or 1 + ; : first-grapheme-from ( start str -- i ) over tail-slice first-grapheme + ; : last-grapheme ( str -- i ) unclip-last-slice grapheme-class swap [ grapheme-class dup rot grapheme-break? ] find-last drop ?1+ nip ; : last-grapheme-from ( end str -- i ) swap head-slice last-grapheme ; pieces ( str quot: ( str -- i ) -- graphemes ) [ dup empty? not ] swap '[ dup @ cut-slice swap ] produce nip ; inline PRIVATE> : >graphemes ( str -- graphemes ) [ first-grapheme ] >pieces ; : string-reverse ( str -- rts ) >graphemes reverse! concat ; > [ 2 swap [ word-break-classes at ] change-nth ] each define-constant >> : word-break-prop ( char -- word-break-prop ) word-break-table interval-at wOther or ; << SYMBOL: check-letter-before SYMBOL: check-letter-after SYMBOL: check-number-before SYMBOL: check-number-after : make-word-table ( -- ) { wCR } { wLF } connect { wNewline wCR wLF } words iota disconnect words iota { wNewline wCR wLF } disconnect { wALetter } { wMidLetter wMidNumLet } check-letter-after set-table { wMidLetter wMidNumLet } { wALetter } check-letter-before set-table { wNumeric wALetter } { wNumeric wALetter } connect { wNumeric } { wMidNum wMidNumLet } check-number-after set-table { wMidNum wMidNumLet } { wNumeric } check-number-before set-table { wKatakana } { wKatakana } connect { wALetter wNumeric wKatakana wExtendNumLet } { wExtendNumLet } [ connect ] [ swap connect ] 2bi ; : finish-word-table ( -- table ) table get [ [ { { 0 [ f ] } { 1 [ t ] } [ ] } case ] map ] map ; "word-table" create-in words init-table table [ make-word-table finish-word-table ] with-variable define-constant >> : word-table-nth ( class1 class2 -- ? ) word-table nth nth ; :: property-not= ( str i property -- ? ) i [ i str ?nth [ word-break-prop property = not ] [ f ] if* ] [ t ] if ; : (format/extended?) ( class -- ? ) ${ wExtend wFormat } member? ; inline : format/extended? ( ch -- ? ) word-break-prop (format/extended?) ; : (walk-up) ( str i -- j ) swap [ format/extended? not ] find-from drop ; : walk-up ( str i -- j ) dupd 1 + (walk-up) [ 1 + (walk-up) ] [ drop f ] if* ; : (walk-down) ( str i -- j ) swap [ format/extended? not ] find-last-from drop ; : walk-down ( str i -- j ) dupd (walk-down) [ 1 - (walk-down) ] [ drop f ] if* ; : word-break? ( str i table-entry -- ? ) { { t [ 2drop f ] } { f [ 2drop t ] } { check-letter-after [ dupd walk-up wALetter property-not= ] } { check-letter-before [ dupd walk-down wALetter property-not= ] } { check-number-after [ dupd walk-up wNumeric property-not= ] } { check-number-before [ dupd walk-down wNumeric property-not= ] } } case ; :: word-break-next ( old-class new-char i str -- next-class ? ) new-char word-break-prop :> new-class new-class (format/extended?) [ old-class dup ${ wCR wLF wNewline } member? ] [ new-class old-class over word-table-nth [ str i 1 - ] dip word-break? ] if ; PRIVATE> : first-word ( str -- i ) [ [ length ] [ first word-break-prop ] bi ] keep 1 swap dup '[ _ word-break-next ] find-index-from drop nip swap or ; : >words ( str -- words ) [ first-word ] >pieces ; : word-break-at? ( i str -- ? ) { [ drop zero? ] [ length = ] [ [ nth-next [ word-break-prop ] dip ] 2keep word-break-next nip ] } 2|| ; : first-word-from ( start str -- i ) over tail-slice first-word + ; : last-word ( str -- i ) [ length iota ] keep '[ _ word-break-at? ] find-last drop 0 or ; : last-word-from ( end str -- i ) swap head-slice last-word ;