cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / fts / gen_queries.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generator (test input only) for the FTS differential: MATCH strings built from real |
| 3 | canonical_name titles of the aien-lessons / aien-sources spaces of the copy db, plus the |
| 4 | query shapes cortex-rs itself sends (raw text with '"' removed, the "quoted" phrase form, |
| 5 | the a AND b AND c fallback) and hand-written edge cases of the FTS5 grammar. |
| 6 | Output: records u32 length + bytes. Python's sqlite3 is used only to READ the titles. |
| 7 | |
| 8 | Usage: gen_queries.py DB OUT [N] [SEED] |
| 9 | """ |
| 10 | import random, re, sqlite3, struct, sys |
| 11 | |
| 12 | db, out = sys.argv[1], sys.argv[2] |
| 13 | target = int(sys.argv[3]) if len(sys.argv) > 3 else 2400 |
| 14 | seed = int(sys.argv[4]) if len(sys.argv) > 4 else 20261026 |
| 15 | con = sqlite3.connect('file:%s?mode=ro' % db, uri=True) |
| 16 | titles = [r[0] for r in con.execute( |
| 17 | "SELECT canonical_name FROM entities WHERE space_slug IN ('aien-lessons','aien-sources') ORDER BY rowid")] |
| 18 | rnd = random.Random(seed) |
| 19 | words = [] |
| 20 | for t in titles: |
| 21 | words += [w for w in re.split(r'[\s"]+', t) if w] |
| 22 | idents = [w for w in words if re.search(r'[_#0-9]', w)] |
| 23 | plain = [w for w in words if re.fullmatch(r'[A-Za-z]{3,}', w)] |
| 24 | nonascii = [w for w in words if any(ord(c) > 127 for c in w)] |
| 25 | |
| 26 | def w(): |
| 27 | return rnd.choice(words) |
| 28 | |
| 29 | def p(): |
| 30 | return rnd.choice(plain) |
| 31 | |
| 32 | Q = [] |
| 33 | edge = [ |
| 34 | '', ' ', ' ', '""', '"" ""', 'a ""', '"" a', 'memory OR ""', '"" OR memory', 'memory AND ""', |
| 35 | 'memory NOT ""', '"" NOT memory', '*', '*reads', '*id', '* reads', '*READS x', '*bogus', 'memory*', |
| 36 | 'mem*', 'm*', 'gb*', 'NEAR(memory model)', 'NEAR(memory model, 2)', 'NEAR(memory model, 0)', |
| 37 | 'NEAR(memory model,)', 'NEAR(memory)', 'NEAR(memory model, x)', 'NEAR(memory model, 99999999999)', |
| 38 | 'NEAR(memory model, 4294967295)', 'near(memory model)', 'NEAR (memory model)', 'NEAR(a b c, 5)', |
| 39 | 'canonical_name:memory', 'content:memory', 'CONTENT:memory', '"content":memory', 'id:memory', |
| 40 | 'space_slug:memory', 'nosuch:memory', '-canonical_name:memory', '-content:memory', '{canonical_name}:memory', |
| 41 | '{canonical_name content}:memory', '-{canonical_name content}:memory', '{id space_slug}:memory', |
| 42 | '{canonical_name}:(content:memory)', 'canonical_name:(memory OR model)', 'content:(memory model)', |
| 43 | '{content}: NEAR(memory model)', '^memory', '^memory model', 'canonical_name:^memory', '^"memory model"', |
| 44 | 'memory + model', '"memory model"', '"memory" + "model"', 'memory + ""', 'memory* + ""', '"" + memory', |
| 45 | 'memory + ""*', 'a + b + c', '(memory)', '((memory))', '(memory OR model) AND gb10', 'memory OR model AND gb10', |
| 46 | 'memory AND model OR gb10', 'memory NOT model OR gb10', 'memory NOT (model OR gb10)', 'memory model OR gb10', |
| 47 | 'memory OR model gb10', '(memory', 'memory)', 'AND', 'OR', 'NOT', 'memory AND', 'AND memory', 'memory OR OR model', |
| 48 | 'memory and model', 'memory or model', 'memory not model', '"unterminated', 'mem"ory', "it's", 'a-b', 'a:b', |
| 49 | 'a,b', 'a.b', 'nvrm_alloc', 'NVRM_ALLOC', 'GB10', 'gb10', '#225', '225', 'sm_121', '18091', 'Qwen3.6-35B', |
| 50 | 'café', 'CAFÉ', 'naïve', 'Ünïcödé', 'Ⅻ', 'fi', 'straße', '日本語', 'Ελληνικά', '😀', 'x\x1ay', '\x1a', |
| 51 | 'memory\x00model', 'memory\tmodel', 'memory\nmodel', 'memory\rmodel', '{', '}', '{}:memory', ':memory', |
| 52 | 'memory:', '^', '^^memory', '+memory', 'memory +', 'memory*model', 'memory**', 'NEAR(', 'NEAR()', |
| 53 | '((((((((((((((((((((((((((((((((((((((((((((((((((memory))))))))))))))))))))))))))))))))))))))))))))))))))', |
| 54 | '(' * 120 + 'memory' + ')' * 120, ' OR '.join(['memory'] * 300), ' AND '.join(plain[:40]), |
| 55 | ' '.join(plain[:60]), 'NEAR(' + ' '.join(plain[:12]) + ', 30)', |
| 56 | ] |
| 57 | Q += edge |
| 58 | # cortex-rs shapes from real titles |
| 59 | for t in titles[:400]: |
| 60 | clean = t.strip().replace('"', '') |
| 61 | Q.append(clean) |
| 62 | Q.append('"%s"' % clean) |
| 63 | terms = clean.split() |
| 64 | if len(terms) > 1: |
| 65 | Q.append(' AND '.join(terms)) |
| 66 | while len(Q) < target: |
| 67 | k = rnd.randrange(16) |
| 68 | if k == 0: q = w() |
| 69 | elif k == 1: q = rnd.choice(idents) if idents else w() |
| 70 | elif k == 2: q = '"%s %s"' % (w(), w()) |
| 71 | elif k == 3: q = '%s AND %s' % (p(), p()) |
| 72 | elif k == 4: q = '%s OR %s' % (p(), p()) |
| 73 | elif k == 5: q = '%s NOT %s' % (p(), p()) |
| 74 | elif k == 6: q = p()[:rnd.randint(1, 4)] + '*' |
| 75 | elif k == 7: q = 'NEAR(%s %s, %d)' % (p(), p(), rnd.randint(0, 12)) |
| 76 | elif k == 8: q = '%s:%s' % (rnd.choice(['canonical_name', 'content', '-content', '-canonical_name', '{content canonical_name}']), p()) |
| 77 | elif k == 9: q = '%s %s %s' % (p(), p(), p()) |
| 78 | elif k == 10: q = '(%s OR %s) %s' % (p(), p(), p()) |
| 79 | elif k == 11: q = rnd.choice(nonascii) if nonascii else 'café' |
| 80 | elif k == 12: q = '^%s %s' % (p(), p()) |
| 81 | elif k == 13: q = '%s + %s*' % (p(), p()[:3]) |
| 82 | elif k == 14: q = rnd.choice(titles) |
| 83 | else: q = ''.join(rnd.choice(' ()"*^+-:{},abcAND OR NOT') for _ in range(rnd.randint(1, 14))) |
| 84 | Q.append(q) |
| 85 | with open(out, 'wb') as f: |
| 86 | for q in Q: |
| 87 | b = q.encode('utf-8') |
| 88 | f.write(struct.pack('<I', len(b)) + b) |
| 89 | print(len(Q), 'queries') |