interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / protocol / synapse / map / src / 30-tok.om

282 lines · 5259 bytes Raw Blame History
1note ======== tokens, sets, similarity ========
2note Tokens: maximal runs of ASCII letters and digits, lowercased; fewer than 2 bytes, all digits and
3note stopwords are dropped; plural "ies" becomes "y" and a final "s" (not "ss", "us", "is") is cut.
4note Sets (ids): record doc set r (cap 96: name, purpose, layer, topics), record purpose set 1024 + r
5note (cap 48), query set 2048 (cap 96), scratch set 2049 (cap 96). Doc entries carry a boost in bits 32+.
6
7global gth ulong 16384
8global gtko ulong 8192
9global gtkl ulong 8192
10global gtdf ulong 8192
11global gtb uchar 256
12global gset ulong 147648
13global gsetn ulong 2050
14
15const STBN ulong 25
16const SQSET ulong 2048
17const SXSET ulong 2049
18
19text tstop "an and are as at be by for from in into is it its of on or that the this to with without not no our we us my your you can each every per via so than then them they their these those has have had was were will would should could also only but do does done all any one how what which who why when where use used using new"
20
21fn sm_sbase id ulong to ulong
22if lt id 1024
23ret mul id 96
24end
25if lt id 2048
26ret add 98304 mul sub id 1024 48
27end
28ret add 147456 mul sub id 2048 96
29end
30
31fn sm_scap id ulong to ulong
32if and ge id 1024 lt id 2048
33ret 48
34end
35ret 96
36end
37
38fn sm_sn id ulong to ulong
39ret get gsetn id
40end
41
42fn sm_sget id ulong i ulong to ulong
43ret get gset add sm_sbase id i
44end
45
46fn sm_sclr id ulong to void
47put gsetn id 0
48end
49
50note sm_sadd: add token v with boost bo to set id (the larger boost is kept on a repeat)
51fn sm_sadd id ulong v ulong bo ulong to void
52b = sm_sbase id
53n = get gsetn id
54for i ulong n
55x = get gset add b i
56if eq and x 4294967295 v
57if lt shr x 32 bo
58put gset add b i or v shl bo 32
59end
60ret
61end
62end
63if ge n sm_scap id
64ret
65end
66put gset add b n or v shl bo 32
67put gsetn id add n 1
68end
69
70fn sm_shas id ulong v ulong to ulong
71b = sm_sbase id
72for i ulong get gsetn id
73x = get gset add b i
74if eq and x 4294967295 v
75ret add shr x 32 1
76end
77end
78ret 0
79end
80
81fn sm_isstop k ulong to bool
82s = ulong 0
83for j ulong add len tstop 1
84e = eq j len tstop
85if not e
86e = eq get tstop j 32
87end
88if e
89if eq sub j s k
90m = true
91for q ulong k
92if ne get gtb q get tstop add s q
93m = false
94end
95end
96if m
97ret true
98end
99end
100s = add j 1
101end
102end
103ret false
104end
105
106note sm_intern: id of gtb[0, k), adding it to the table when new; SNONE when the table is full
107fn sm_intern k ulong to ulong
108h = ulong 2166136261
109for i ulong k
110h = and mul xor h ulong get gtb i 16777619 4294967295
111end
112for j ulong 16384
113s = and add h j 16383
114v = get gth s
115if eq v 0
116id = get gst SNTOK
117if ge id 8192
118ret SNONE
119end
120o = get gst SPOOL
121for i ulong k
122sm_pb get gtb i
123end
124put gtko id o
125put gtkl id k
126put gtdf id 0
127put gth s add id 1
128put gst SNTOK add id 1
129ret id
130end
131t = sub v 1
132if eq get gtkl t k
133if sm_tbeq get gtko t k
134ret t
135end
136end
137end
138ret SNONE
139end
140
141note sm_tbeq: pool bytes gpool[o, o + k) equal the token buffer gtb[0, k)
142fn sm_tbeq o ulong k ulong to bool
143for i ulong k
144if ne get gpool add o i get gtb i
145ret false
146end
147end
148ret true
149end
150
151fn sm_tokend id ulong bo ulong to void
152k = get gst STBN
153put gst STBN 0
154if lt k 2
155ret
156end
157dg = true
158for i ulong k
159if not sm_isdig get gtb i
160dg = false
161end
162end
163if dg
164ret
165end
166if sm_isstop k
167ret
168end
169if gt k 4
170if eq get gtb sub k 3 105
171if eq get gtb sub k 2 101
172if eq get gtb sub k 1 115
173put gtb sub k 3 121
174k = sub k 2
175end
176end
177end
178end
179if gt k 3
180if eq get gtb sub k 1 115
181c = get gtb sub k 2
182if not or eq c 115 or eq c 117 eq c 105
183k = sub k 1
184end
185end
186end
187t = sm_intern k
188if ne t SNONE
189sm_sadd id t bo
190end
191end
192
193fn sm_isword c uchar to bool
194if sm_isdig c
195ret true
196end
197ret and ge c 97 le c 122
198end
199
200note sm_tokz: tokens of pool slice (o, n) into set id with boost bo
201fn sm_tokz o ulong n ulong id ulong bo ulong to void
202put gst STBN 0
203for i ulong n
204c = sm_lc get gpool add o i
205if sm_isword c
206k = get gst STBN
207if lt k len gtb
208put gtb k c
209put gst STBN add k 1
210end
211else
212sm_tokend id bo
213end
214end
215sm_tokend id bo
216end
217
218note sm_tokrec: doc and purpose sets of every record, then document frequencies over doc sets
219fn sm_tokrec to void
220for r ulong get gst SNREC
221sm_sclr r
222sm_sclr add 1024 r
223sm_tokz sm_fo r FPUR sm_fl r FPUR add 1024 r 0
224sm_tokz sm_fo r FNAME sm_fl r FNAME r 1
225sm_tokz sm_fo r FTOP sm_fl r FTOP r 1
226sm_tokz sm_fo r FPUR sm_fl r FPUR r 0
227sm_tokz sm_fo r FLAY sm_fl r FLAY r 0
228end
229for r ulong get gst SNREC
230for i ulong sm_sn r
231t = and sm_sget r i 4294967295
232put gtdf t add get gtdf t 1
233end
234end
235end
236
237note sm_jac: Jaccard index of two sets in thousandths (token ids only; 0 when both are empty)
238fn sm_jac a ulong b ulong to ulong
239na = sm_sn a
240nb = sm_sn b
241if eq add na nb 0
242ret 0
243end
244c = ulong 0
245for i ulong na
246if gt sm_shas b and sm_sget a i 4294967295 0
247c = add c 1
248end
249end
250ret div mul c 1000 sub add na nb c
251end
252
253note sm_lg2m: 1000 * log2(x / 1000) for x >= 1000 (0 below), by bit length then 10 squarings
254fn sm_lg2m x ulong to ulong
255if le x 1000
256ret 0
257end
258ip = ulong 0
259y = x
260while ge y 2000 bound 70
261y = div y 2
262ip = add ip 1
263end
264f = ulong 0
265b = ulong 500
266for i ulong 10
267y = div mul y y 1000
268if ge y 2000
269y = div y 2
270f = add f b
271end
272b = div b 2
273end
274ret add mul ip 1000 f
275end
276
277note sm_idf: 1000 + 1000 * log2((N + 1) / (df + 1)) over record doc sets
278fn sm_idf t ulong to ulong
279n = get gst SNREC
280d = get gtdf t
281ret add 1000 sm_lg2m div mul add n 1 1000 add d 1
282end