\ FORTH auto-generated from corpus_tokens.xml
\ Pipeline: XML --(XSLT)--> FORTH (no Python)
256 CONSTANT MAX-VOCAB
CREATE VOCAB MAX-VOCAB 32 * ALLOT
VARIABLE VOCAB-SIZE 0 VOCAB-SIZE !
: vocab-id ( c-addr u -- id|-1 )
-1 VOCAB-SIZE @ 0 ?DO
VOCAB I 32 * + COUNT 2OVER COMPARE 0= IF DROP I UNLOOP EXIT THEN
LOOP NIP NIP ;
: add-word ( c-addr u -- id )
2DUP vocab-id DUP -1 <> IF NIP NIP EXIT THEN
DROP VOCAB-SIZE @ DUP >R VOCAB R@ 32 * + PLACE
R@ 1+ VOCAB-SIZE ! R> ;
: .word ( id -- )
VOCAB SWAP 32 * + COUNT TYPE ;
: init-vocab
S" " add-word DROP
;
CONSTANT CORPUS-LEN
CREATE CORPUS
,
: cooccur-window ( center -- )
DUP 2 - MAX 0 SWAP 2 + CORPUS-LEN MIN SWAP
DO I OVER <> IF
." cooccur: " OVER .word ." ~ " I CORPUS + @ .word CR
THEN LOOP DROP ;
: train ( -- )
CR ." Training on " CORPUS-LEN . ." tokens..." CR
CORPUS-LEN 0 DO CORPUS I + @ cooccur-window LOOP ;
: demo
0 VOCAB-SIZE !
init-vocab
CR ." PocketLearn FORTH runtime" CR
." vocab size: " VOCAB-SIZE @ . CR
." Running co-occurrence (window=2)..." CR
\ train \ uncomment to print all pairs
CR ." Done. Type: 0 .word or train" CR ;
demo