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