snapkitty
machine-learning
python
File size: 1,745 Bytes
ce0d85d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!-- corpus_to_forth.xslt: XML -> FORTH dictionary. ZERO PYTHON. -->
<xsl:template match="/corpus">
\ 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 &lt;&gt; IF NIP NIP EXIT THEN
  DROP VOCAB-SIZE @  DUP &gt;R  VOCAB R@ 32 * + PLACE
  R@ 1+ VOCAB-SIZE !  R&gt; ;

: .word ( id -- )
  VOCAB SWAP 32 * + COUNT TYPE ;

: init-vocab
<xsl:for-each select="vocab/word">  S" <xsl:value-of select="."/>" add-word DROP
</xsl:for-each>;

<xsl:value-of select="count(vocab/word)"/> CONSTANT CORPUS-LEN
CREATE CORPUS
<xsl:for-each select="tokens/token">  <xsl:value-of select="@id"/> ,<xsl:if test="position() mod 10 = 0">
</xsl:if></xsl:for-each>

: cooccur-window ( center -- )
  DUP 2 - MAX 0 SWAP  2 + CORPUS-LEN MIN SWAP
  DO I OVER &lt;&gt; 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
</xsl:template>
</xsl:stylesheet>