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 / tools / gen / protocol / genjac.c
| 1 | /* |
| 2 | * genjac.c -- word-3-gram Jaccard near-duplicate check for GEN held-out prompts (aien-spark/omega#476, |
| 3 | * docs/one_layer/GEN_PROTOCOL_V1.md section 2). Deterministic: no clock, no randomness, no environment. |
| 4 | * |
| 5 | * genjac pair <fileA> <fileB> |
| 6 | * print "grams_a=<n> grams_b=<m> inter=<i> union=<u> jaccard=<6 decimals>" |
| 7 | * genjac scan [--threshold <t>] [--show] <heldout-list> <corpus-list> |
| 8 | * every held-out file against every corpus file; a list file names one path per line (no blank |
| 9 | * lines, no comments). Prints "heldout=<n> corpus=<m> threshold=<t> max_jaccard=<6 decimals> |
| 10 | * above=<k>" where above = the number of held-out files with at least one corpus file whose Jaccard |
| 11 | * is strictly greater than t. Nothing about any held-out file is printed unless --show is given |
| 12 | * (custodian only): then one "above <heldout path> <corpus path> <jaccard>" line per such pair. |
| 13 | * Exit 0 when above = 0, 1 otherwise, 2 on usage or read errors. Default t = 0.5. |
| 14 | * |
| 15 | * Words: bytes A-Z are lowercased; a word is a maximal run of [a-z0-9]; every other byte separates (the |
| 16 | * genret tokenization, tools/gen/retrieval/CONTRACT.md section 4). Grams: the SET of consecutive word |
| 17 | * triples. Jaccard = |A n B| / |A u B|; 0 when both sets are empty (a file with fewer than 3 words has no |
| 18 | * gram). The comparison "> t" is exact: t is a decimal with at most 6 fraction digits, read as an integer |
| 19 | * ratio, and compared by integer cross-multiplication. |
| 20 | */ |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | typedef struct { char **g; int n; } Grams; |
| 27 | |
| 28 | static char *slurp(const char *path) |
| 29 | { |
| 30 | FILE *fp = fopen(path, "rb"); |
| 31 | if (!fp) { fprintf(stderr, "genjac: cannot read %s\n", path); exit(2); } |
| 32 | size_t cap = 4096, n = 0, r; |
| 33 | char *b = malloc(cap + 1); |
| 34 | while (b && (r = fread(b + n, 1, cap - n, fp)) > 0) { |
| 35 | n += r; |
| 36 | if (n == cap) { cap *= 2; b = realloc(b, cap + 1); } |
| 37 | } |
| 38 | fclose(fp); |
| 39 | if (!b) { perror("genjac"); exit(2); } |
| 40 | b[n] = 0; |
| 41 | for (size_t i = 0; i < n; i++) |
| 42 | if (!b[i]) { fprintf(stderr, "genjac: %s: NUL byte\n", path); exit(2); } |
| 43 | return b; |
| 44 | } |
| 45 | |
| 46 | static int cmp(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } |
| 47 | |
| 48 | static Grams grams(const char *path) |
| 49 | { |
| 50 | char *s = slurp(path); |
| 51 | size_t len = strlen(s); |
| 52 | char **w = malloc((len / 2 + 2) * sizeof *w); |
| 53 | int nw = 0; |
| 54 | for (size_t i = 0; i < len;) { |
| 55 | unsigned char c = (unsigned char)s[i]; |
| 56 | if (c >= 'A' && c <= 'Z') s[i] = (char)(c + 32), c = (unsigned char)s[i]; |
| 57 | if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { |
| 58 | size_t j = i; |
| 59 | while (j < len) { |
| 60 | unsigned char d = (unsigned char)s[j]; |
| 61 | if (d >= 'A' && d <= 'Z') s[j] = (char)(d + 32), d = (unsigned char)s[j]; |
| 62 | if (!((d >= 'a' && d <= 'z') || (d >= '0' && d <= '9'))) break; |
| 63 | j++; |
| 64 | } |
| 65 | w[nw++] = strndup(s + i, j - i); |
| 66 | i = j; |
| 67 | } else i++; |
| 68 | } |
| 69 | Grams G = {NULL, 0}; |
| 70 | if (nw >= 3) { |
| 71 | G.g = malloc((size_t)(nw - 2) * sizeof *G.g); |
| 72 | for (int i = 0; i + 2 < nw; i++) { |
| 73 | size_t l = strlen(w[i]) + strlen(w[i + 1]) + strlen(w[i + 2]) + 3; |
| 74 | G.g[i] = malloc(l); |
| 75 | snprintf(G.g[i], l, "%s %s %s", w[i], w[i + 1], w[i + 2]); |
| 76 | } |
| 77 | qsort(G.g, (size_t)(nw - 2), sizeof *G.g, cmp); |
| 78 | int u = 0; |
| 79 | for (int i = 0; i < nw - 2; i++) |
| 80 | if (u == 0 || strcmp(G.g[i], G.g[u - 1])) G.g[u++] = G.g[i]; |
| 81 | G.n = u; |
| 82 | } |
| 83 | for (int i = 0; i < nw; i++) free(w[i]); |
| 84 | free(w); |
| 85 | free(s); |
| 86 | return G; |
| 87 | } |
| 88 | |
| 89 | static void jac(const Grams *a, const Grams *b, int64_t *inter, int64_t *uni) |
| 90 | { |
| 91 | int i = 0, j = 0; |
| 92 | int64_t in = 0; |
| 93 | while (i < a->n && j < b->n) { |
| 94 | int c = strcmp(a->g[i], b->g[j]); |
| 95 | if (c == 0) { in++; i++; j++; } else if (c < 0) i++; else j++; |
| 96 | } |
| 97 | *inter = in; |
| 98 | *uni = a->n + b->n - in; |
| 99 | } |
| 100 | |
| 101 | /* t = num / den, den = 10^digits */ |
| 102 | static int parse_t(const char *s, int64_t *num, int64_t *den) |
| 103 | { |
| 104 | int64_t n = 0, d = 1; |
| 105 | int dot = 0, digits = 0, any = 0; |
| 106 | for (const char *p = s; *p; p++) { |
| 107 | if (*p == '.' && !dot) { dot = 1; continue; } |
| 108 | if (*p < '0' || *p > '9') return -1; |
| 109 | any = 1; |
| 110 | n = n * 10 + (*p - '0'); |
| 111 | if (dot) { d *= 10; if (++digits > 6) return -1; } |
| 112 | if (n > 10000000) return -1; |
| 113 | } |
| 114 | if (!any || n > d) return -1; /* 0 <= t <= 1 */ |
| 115 | *num = n; |
| 116 | *den = d; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static char **read_list(const char *path, int *n) |
| 121 | { |
| 122 | char *s = slurp(path); |
| 123 | size_t len = strlen(s); |
| 124 | if (len == 0 || s[len - 1] != '\n') { fprintf(stderr, "genjac: %s: empty or no final newline\n", path); exit(2); } |
| 125 | char **v = malloc((len + 1) * sizeof *v); |
| 126 | int k = 0; |
| 127 | for (char *p = s; *p;) { |
| 128 | char *e = strchr(p, '\n'); |
| 129 | *e = 0; |
| 130 | if (!*p) { fprintf(stderr, "genjac: %s: blank line\n", path); exit(2); } |
| 131 | v[k++] = p; |
| 132 | p = e + 1; |
| 133 | } |
| 134 | *n = k; |
| 135 | return v; |
| 136 | } |
| 137 | |
| 138 | static void print6(int64_t in, int64_t un) |
| 139 | { |
| 140 | /* exact rounding of in/un to 6 decimals, half up */ |
| 141 | if (un == 0) { printf("0.000000"); return; } |
| 142 | int64_t q = (in * 2000000 + un) / (2 * un); |
| 143 | printf("%lld.%06lld", (long long)(q / 1000000), (long long)(q % 1000000)); |
| 144 | } |
| 145 | |
| 146 | static int usage(void) |
| 147 | { |
| 148 | fprintf(stderr, "usage: genjac pair <fileA> <fileB> | scan [--threshold <t>] [--show] <heldout-list> <corpus-list>\n"); |
| 149 | return 2; |
| 150 | } |
| 151 | |
| 152 | int main(int argc, char **argv) |
| 153 | { |
| 154 | if (argc == 4 && !strcmp(argv[1], "pair")) { |
| 155 | Grams a = grams(argv[2]), b = grams(argv[3]); |
| 156 | int64_t in, un; |
| 157 | jac(&a, &b, &in, &un); |
| 158 | printf("grams_a=%d grams_b=%d inter=%lld union=%lld jaccard=", a.n, b.n, (long long)in, (long long)un); |
| 159 | print6(in, un); |
| 160 | putchar('\n'); |
| 161 | return 0; |
| 162 | } |
| 163 | if (argc < 4 || strcmp(argv[1], "scan")) return usage(); |
| 164 | const char *ts = "0.5"; |
| 165 | int show = 0, i = 2; |
| 166 | for (; i < argc - 2; i++) { |
| 167 | if (!strcmp(argv[i], "--threshold") && i + 1 < argc - 2) ts = argv[++i]; |
| 168 | else if (!strcmp(argv[i], "--show")) show = 1; |
| 169 | else return usage(); |
| 170 | } |
| 171 | if (i != argc - 2) return usage(); |
| 172 | int64_t tn, td; |
| 173 | if (parse_t(ts, &tn, &td)) { fprintf(stderr, "genjac: threshold must be a decimal in [0,1] with at most 6 fraction digits\n"); return 2; } |
| 174 | int nh, nc; |
| 175 | char **hl = read_list(argv[argc - 2], &nh), **cl = read_list(argv[argc - 1], &nc); |
| 176 | Grams *cg = malloc((size_t)nc * sizeof *cg); |
| 177 | for (int c = 0; c < nc; c++) cg[c] = grams(cl[c]); |
| 178 | int64_t bi = 0, bu = 1; /* max Jaccard as a ratio, starts at 0/1 */ |
| 179 | int above = 0; |
| 180 | for (int h = 0; h < nh; h++) { |
| 181 | Grams g = grams(hl[h]); |
| 182 | int hit = 0; |
| 183 | for (int c = 0; c < nc; c++) { |
| 184 | int64_t in, un; |
| 185 | jac(&g, &cg[c], &in, &un); |
| 186 | if (un == 0) continue; |
| 187 | if (in * bu > bi * un) { bi = in; bu = un; } |
| 188 | if (in * td > tn * un) { /* in/un > tn/td, exactly */ |
| 189 | hit = 1; |
| 190 | if (show) { printf("above %s %s ", hl[h], cl[c]); print6(in, un); putchar('\n'); } |
| 191 | } |
| 192 | } |
| 193 | above += hit; |
| 194 | } |
| 195 | printf("heldout=%d corpus=%d threshold=%s max_jaccard=", nh, nc, ts); |
| 196 | print6(bi, bu); |
| 197 | printf(" above=%d\n", above); |
| 198 | return above ? 1 : 0; |
| 199 | } |