interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / fts / gen_like.py

36 lines · 1996 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Generator (test input only) for the LIKE differential: the cortex-rs shape '%<query>%'
3for real title words, plus '_' / '%' placements, ASCII and non-ASCII case variants,
4invalid UTF-8, embedded NUL and the 50000-byte pattern limit. Records: u32 length + bytes.
5Usage: gen_like.py DB OUT [N]"""
6import random, re, sqlite3, struct, sys
7db, out = sys.argv[1], sys.argv[2]
8target = int(sys.argv[3]) if len(sys.argv) > 3 else 600
9con = sqlite3.connect('file:%s?mode=ro' % db, uri=True)
10titles = [r[0] for r in con.execute("SELECT canonical_name FROM entities ORDER BY rowid")]
11rnd = random.Random(7)
12words = [w for t in titles for w in re.split(r'\s+', t) if w]
13na = [w for w in words if any(ord(c) > 127 for c in w)]
14P = [b'', b'%', b'%%', b'_', b'%_%', b'___', b'%a%', b'%A%', b'%gb10%', b'%GB10%', b'%Gb_0%', b'%nvrm\\_alloc%',
15 b'%nvrm_alloc%', b'%#225%', b'%caf\xc3\xa9%', b'%CAF\xc3\x89%', b'%\xc3\xa9%', b'%_\xc3\xa9%', b'%\xe2\x80\x94%',
16 b'%\xff%', b'%\xc0%', b'%\x80%', b'%\xc3%', b'%a\x00b%', b'\x00', b'%\x00', b'%%%%%x%%%%', b'%_%_%_%',
17 b'x%', b'%x', b'%e', b'E%', b'%' + b'a' * 49998, b'%' + b'a' * 49999, b'%' + b'a' * 50000 + b'%',
18 b'%[a]%', b'%*%', b'%?%', b'%\\%', b'%\'%', b'%"%', b'%' + ('\U0001F600'.encode()) + b'%']
19for t in titles[:150]:
20 P.append(b'%' + t.strip().encode() + b'%')
21while len(P) < target:
22 k = rnd.randrange(8)
23 wd = rnd.choice(words)
24 if k == 0: s = '%' + wd + '%'
25 elif k == 1: s = '%' + wd.upper() + '%'
26 elif k == 2: s = '%' + wd.lower() + '%'
27 elif k == 3: s = '%' + ''.join('_' if rnd.random() < 0.3 else c for c in wd) + '%'
28 elif k == 4: s = '%' + rnd.choice(na) + '%' if na else '%é%'
29 elif k == 5: s = '%' + wd[:rnd.randint(1, 3)] + '%' + rnd.choice(words)[:3] + '%'
30 elif k == 6: s = wd + '%'
31 else: s = '%' + wd.swapcase() + '%'
32 P.append(s.encode())
33with open(out, 'wb') as f:
34 for p in P:
35 f.write(struct.pack('<I', len(p)) + p)
36print(len(P), 'patterns')