sourcelibs/Embed/demos/pets.xtl
1⍝!/usr/bin/env xetal
2⍝│ Embed on a corpus small enough to read: twelve sentences about pets
3⍝│ and food. Run with "just demo-lib Embed pets".
4
5"em:" u̲se< "Embed"
6
7⍝│ Words: 1 the, 2 cat, 3 dog, 4 eats, 5 fish, 6 meat, 7 sleeps, 8 runs.
8⍝│ Each pet with each food and each action, so the two keep the same
9⍝│ company. Sentences as word numbers, each ended by 0.
10ids ← 1 2 4 5 0 1 2 4 6 0 1 3 4 5 0 1 3 4 6 0 1 2 7 0 1 2 8 0 1 3 7 0 1 3 8 0 2 4 5 0 3 4 6 0 2 8 0 3 7 0
11C ← (8, 2) ᵉᵐc̲ooccur ids
12⍝│ Who keeps whose company: the counts, then weighted.
13C
14P ← ᵉᵐp̲pmi C
15(f̲loat f̲loor 0.5 + 100.0 × P) ÷ 100.0
16⍝│ cat's 3 nearest (by cosine of the PPMI rows): the, eats, dog. In a
17⍝│ dozen sentences "the" and "eats" keep company with every pet, so
18⍝│ they look like the cat as much as the dog does. Repeating the same
19⍝│ sentences would change nothing (PPMI is a ratio); more varied ones
20⍝│ would: on the embedding explorer's corpus a word's nearest is of
21⍝│ its own kind for 33% of words at 12 sentences, 91% at 200 and 90%
22⍝│ from 1,600 on (its README has the table).
23(P, 3) ᵉᵐn̲earest 2
24⍝│ fish's 3 nearest: meat, the other food, first.
25(P, 3) ᵉᵐn̲earest 5