ir.synonyms¶
The synonym ledger: corpus vocabulary an agent learns, for keyword and regex search.
Lexical search (BM25, grep) misses when the searcher’s word is not the document’s
word. An LLM closes most of that gap on its own by writing variants of the query,
but it cannot know a corpus’s private vocabulary: a project’s nickname, a renamed
package, a person’s handle, in-house jargon. The ledger keeps exactly those, learned
from misses, so the next search starts where the last one ended (research report
misc/docs/ir_13).
One JSON object per line in synonyms.jsonl under ir’s config dir (override the
path with $IR_SYNONYMS):
{"terms": ["acmecorp", "acme corp", "ac"], "note": "client; repo dir ac/", "added": "2026-11-11"}
{"terms": ["vd", "vector database"], "oneway": true, "note": "vd expands, not back"}
terms are equivalent (each expands to the others); with oneway only the first
expands to the rest. corpus (optional) limits an entry to one corpus. An entry
may carry provenance ({"miss_query": ..., "found_ids": [...]}: the search that
missed and what the vocabulary found), which lets ir.synonyms_eval.replay_ledger()
re-test it; verified (the number of replays it won) and status ("demoted" entries are not
loaded by default) are maintained by that replay. The ledger
is the user’s private data: it lives in their config dir, never in a repo.
Three uses, one source: term_regex() builds an alternation for grep,
expand_query() produces query variants, and make_synonym_formulator()
plugs those into ir.retrieve.search()’s formulate= seam (deterministic, no
LLM; the original query always comes first).
>>> entries = [SynonymEntry(("acmecorp", "acme corp", "ac"))]
>>> aliases("Acme Corp", entries)
['Acme Corp', 'acmecorp', 'ac']
>>> term_regex(aliases("acmecorp", entries))
'acme[-_ ]?corp|acmecorp|\\bac\\b'
>>> expand_query("acme corp invoice", entries)
['acme corp invoice', 'acmecorp invoice', 'ac invoice']
Module Attributes
|
|
|
|
The ledger's file name inside |
|
Terms at most this long that are plain word characters get |
|
Default cap on the number of query variants (the original included). |
Functions
|
Append one entry to the ledger (creating it) and return it. |
|
|
|
|
Where the ledger lives: |
|
|
The ledger's entries (those for all corpora, plus |
|
A deterministic |
|
One alternation matching any of |
|
Rewrite the ledger with |
Classes
|
One ledger line: equivalent |
- ir.synonyms.ACTIVE = 'active'¶
DEMOTEDentries are kept (for audit) but not loaded.- Type:
Ledger entry statuses
- ir.synonyms.DEMOTED = 'demoted'¶
DEMOTEDentries are kept (for audit) but not loaded.- Type:
Ledger entry statuses
- ir.synonyms.DFLT_LEDGER_NAME = 'synonyms.jsonl'¶
The ledger’s file name inside
ir.config.config_dir().
- ir.synonyms.DFLT_MAX_VARIANTS = 6¶
Default cap on the number of query variants (the original included).
- ir.synonyms.SHORT_TERM_CHARS = 4¶
Terms at most this long that are plain word characters get
\bguards in a regex, soacdoes not match insidereact.
- class ir.synonyms.SynonymEntry(terms, oneway=False, note='', added='', corpus=None, provenance=None, verified=0, status='active', source_line='')[source]¶
Bases:
objectOne ledger line: equivalent
terms(oronewayfrom the first).- source_line: str = ''¶
the ledger line this entry was read from (
""if built in code); letsupdate_entries()patch exactly that line and leave the rest verbatim.
- ir.synonyms.add_synonyms(terms, *, note='', oneway=False, corpus=None, provenance=None, path=None)[source]¶
Append one entry to the ledger (creating it) and return it.
provenancerecords the miss that prompted the entry ({"miss_query": ..., "found_ids": [...]}, optionally"fix_query") so a later replay can verify the entry still helps.Add an entry only after a verified find that the corpus-specific word made possible; general-language synonyms are the searcher’s job, not the ledger’s.
- Return type:
- ir.synonyms.aliases(term, entries)[source]¶
termfirst, then every term the ledger makes equivalent to it (deduped).
- ir.synonyms.expand_query(query, entries, *, max_variants=6)[source]¶
queryfirst, then variants with one known term swapped for an alias.Terms are matched as whole words, case-insensitively, separator-tolerant.
- ir.synonyms.ledger_path()[source]¶
Where the ledger lives:
$IR_SYNONYMS, else<config_dir>/synonyms.jsonl.- Return type:
- ir.synonyms.load_synonyms(path=None, *, corpus=None, include_demoted=False)[source]¶
The ledger’s entries (those for all corpora, plus
corpus’s own).Demoted entries are skipped unless
include_demoted.A missing ledger is an empty one; a malformed line is skipped, so a hand edit gone wrong never breaks search.
- Return type:
- ir.synonyms.make_synonym_formulator(entries=None, *, path=None, corpus=None, max_variants=6)[source]¶
A deterministic
ir.formulate.Formulatorthat expands from the ledger.With
entries=Nonethe ledger is re-read on every call, so an entry an agent just added applies to its next search. Returns the original query alone when nothing in it is in the ledger (identity, the seam’s default behavior).
- ir.synonyms.term_regex(terms)[source]¶
One alternation matching any of
terms, tolerant of-/_/space.Longest first (so a regex engine prefers the full phrase); short plain words get word boundaries.
>>> term_regex(["db", "data base"]) 'data[-_ ]?base|\\bdb\\b'- Return type:
- ir.synonyms.update_entries(changes, path=None)[source]¶
Rewrite the ledger with
(old, new)entry changes; every other line stays verbatim.oldmust come fromload_synonyms()(it carries the line it was read from). Onlyverifiedandstatusare taken fromnew, and they are patched into the original JSON object, so comments, malformed lines and unknown keys a user hand-wrote survive. A line that changed since it was read (an edit or another writer) is left alone. The file is replaced atomically (unique temp file, then rename); anir synonyms-addappend landing between the final read and the rename could still be lost, so keep replays out of concurrent writers. Returns the number of lines patched.- Return type: