NAME
ttypt/lemma.h - lemma โ corm-backed full-text index.
SYNOPSIS
#include <stddef.h>
#include <stdint.h>
#include <ttypt/corm.h>
Data Structures
Context for the lemma_rank FTS score function.
Macros
Canonical single-facet field for the store/unstore/readback adapters.
Typedefs
Opaque full-text index handle.
Functions
Fold a UTF-8 string to lowercase, preserving accents.
Open an index.
Open a lemma index from an opaque spec string.
Close the index and free all resources.
Drop all entries; the handle stays valid.
Index every token of value under (field, row_id).
rec_ref_t-native convenience wrapper for lemma_index.
Remove every posting for (field, row_id) plus the doc side-table.
rec_ref_t-native convenience wrapper for lemma_unindex.
Store a ref's whole value text (rec_axis_store convention).
Remove a ref's stored doc (rec_axis_unstore).
Read a ref's stored (folded) text (rec_axis_readback).
Query: every token of query must prefix-match in field.
Phrase query: prefix-match with contiguous subsequence ordering.
Recall-kernel lexical filler (adapter contract, see rec.h).
FTS score function for the recall-kernel rank loop.
Tokenize non-whitespace word tokens in folded.
Normalize a newline-separated token list.
Check if a newline-separated list contains token.
Append a token to a newline-separated list if not already present.
Initialize or obtain the shared global/default vocabulary.
Create a dynamic vocabulary instance (optionally extending a base vocab).
Free a dynamic vocabulary instance.
Load additional words from a plaintext wordlist file into a dynamic vocabulary.
Encode a text string losslessly into an array of 16-bit token IDs.
Decode an array of 16-bit token IDs losslessly into a character buffer.
Query the vocabulary for fuzzy or substring matching token IDs (Tier 1).
Check if a token ID represents a searchable word (vs delimiter/whitespace/raw byte).
Compute Levenshtein edit distance between two strings.
Index a pre-tokenized token stream under (field, row_id).
Fuzzy query: match with Levenshtein edit distance tolerance.
Conventional typed axis store plugin export for libcorm.
Detailed Description
lemma โ corm-backed full-text index.
A generic inverted index over (field, token) โ row_id. It knows nothing about rows, schemas, or the application: callers choose which fields to index and interpret row_ids. Match semantics are word-token based with prefix expansion: a query token must match the start of an indexed token. Multiple tokens in a query are ANDed.
Macro Definition Documentation
#define LEMMA_AXIS_TEXT_FIELD 'text'
Canonical single-facet field for the store/unstore/readback adapters. RECALL-KERNEL.md, Phase 2A per-axis role "lemma": the whole value string's text. Multi-facet indexing stays raw native (lemma_index / lemma_unindex per field).
Function Documentation
void lemma_clear (lemma_db_t * db)
Drop all entries; the handle stays valid.
Parameters
db Index handle.
void lemma_close (lemma_db_t * db)
Close the index and free all resources.
Parameters
db Index handle.
int lemma_fold (char * out, size_t outsz, const char * in)
Fold a UTF-8 string to lowercase, preserving accents. Accent-sensitive search: ASCII A-Z and the Latin-1 Supplement uppercase letters are lowercased; all other bytes are copied verbatim. The fold is locale-independent (no iconv, no setlocale) and never grows the output, so it fits whenever outsz > strlen(in).
Parameters
out Output buffer.
outsz Capacity of out.
in Input string.
Returns
Number of bytes written, or -1 if the output buffer is too small (the caller falls back to raw comparison).
int lemma_index (lemma_db_t * db, const char * field, const char * row_id, const char * value)
Index every token of value under (field, row_id). Duplicate tokens collapse automatically.
Parameters
db Index handle.
field Field name.
row_id Row id string.
value Text to tokenize and index.
Returns
0 on success, -1 on invalid arguments.
int lemma_index_ref (lemma_db_t * db, const char * field, rec_ref_t row_id, const char * value)
rec_ref_t-native convenience wrapper for lemma_index. Shared with libjoint/libsepal/libislet's rec_axis_fill_* functions at the id-uniformity boundary: formats row_id as the canonical decimal string lemma_rank/rec_axis_fill_tokens expect and calls lemma_index. This does not change lemma's storage or key format, and does not remove the generic string-row_id path (lemma_index proper) โ it only saves every rec_ref_t-keyed caller from doing its own decimal formatting before indexing. Same return contract as lemma_index.
Parameters
db Index handle.
field Field name.
row_id Row ref.
value Text to tokenize and index.
Returns
0 on success, -1 on invalid arguments.
int lemma_index_tokens (lemma_db_t * db, const char * field, const char * row_id, const uint16_t * tokens, size_t n_tokens)
Index a pre-tokenized token stream under (field, row_id). Enforces Selective Lexical Indexing: only Word tokens (lemma_tok_is_word) generate inverted index postings, filtering delimiters and whitespace. Stores the token sequence for ultra-fast phrase adjacency checking without duplicate folded string tables.
Parameters
db Index handle.
field Field name.
row_id Row id string.
tokens Array of token IDs.
n_tokens Count of tokens.
Returns
0 on success, -1 on invalid arguments.
int lemma_levenshtein (const char * s1, size_t l1, const char * s2, size_t l2)
Compute Levenshtein edit distance between two strings.
Parameters
s1 First string.
l1 Length of first string.
s2 Second string.
l2 Length of second string.
Returns
Levenshtein distance (insertions, deletions, substitutions).
int lemma_list_append (char * out, size_t out_sz, const char * token)
Append a token to a newline-separated list if not already present.
Parameters
out Destination list buffer.
out_sz Capacity of out.
token Token to append.
Returns
0 on success, -1 on buffer overflow.
int lemma_list_contains (const char * list, const char * token)
Check if a newline-separated list contains token.
Parameters
list Newline-separated list.
token Token to find.
Returns
1 if present, 0 otherwise.
int lemma_list_normalize (const char * input, char * out, size_t out_sz)
Normalize a newline-separated token list. Splits on newlines/CR, trims tokens, and appends deduplicated non-empty tokens into out separated by '
Parameters
input Input list.
out Destination buffer.
out_sz Capacity of out.
Returns
0 on success, -1 on buffer overflow.
lemma_db_t * lemma_open (unsigned mask)
Open an index.
Parameters
mask Corm hash mask (0 = corm default).
Returns
New index handle, or NULL on failure.
uint32_t lemma_query (lemma_db_t * db, const char * field, const char * query, uint32_t out_hd, int * handled)
Query: every token of query must prefix-match in field. Matching row_ids are stored as "row_id" โ "" into out_hd (a caller-opened corm; duplicates collapse). *handled is set to 1 when the query produced at least one token, 0 for an empty/zero-token query (no-op โ the caller should treat it as "matches everything").
Parameters
db Index handle.
field Field name.
query Query string.
out_hd Caller-opened corm handle for result rows.
handled 1 when the query had โฅ1 token, else 0.
Returns
Number of matches written.
uint32_t lemma_query_fuzzy (lemma_db_t * db, const char * field, const char * query, int max_edit_distance, uint32_t out_hd, int * handled)
Fuzzy query: match with Levenshtein edit distance tolerance. Query tokens match terms within max_edit_distance. Matches are ANDed.
Parameters
db Index handle.
field Field name.
query Query string.
max_edit_distance Maximum Levenshtein distance (e.g. 1 or 2).
out_hd Caller-opened corm handle for result rows.
handled 1 when query had >= 1 token, else 0.
Returns
Number of matches written.
uint32_t lemma_query_phrase (lemma_db_t * db, const char * field, const char * query, uint32_t out_hd, int * handled)
Phrase query: prefix-match with contiguous subsequence ordering. Every token of query must prefix-match in field, AND the tokens must occur in the indexed text as a contiguous subsequence (in query order). Line breaks and punctuation are token separators, so a phrase may span lines. A single-token query behaves exactly like lemma_query. The contract is otherwise identical to lemma_query.
Parameters
db Index handle.
field Field name.
query Query string.
out_hd Caller-opened corm handle for result rows.
handled 1 when the query had โฅ1 token, else 0.
Returns
Number of matches written.
int lemma_rank (struct lemma_rank_ctx * ctx, rec_ref_t ref, float * score)
FTS score function for the recall-kernel rank loop. score = ctx->matched / token_count of the folded field text of decimal(ref) (shorter docs rank higher on ties). The caller must have indexed by its decimal ref. Compatible with rec_score_fn via a caller adapter. The score may exceed 1.0 when matched > doc tokens.
Parameters
ctx Rank context.
ref Row ref.
score Receives the score.
Returns
0 ok; -1 on NULL args, an unknown (field, row), or a zero-token document (the rank loop skips the ref).
int lemma_tok_decode (const lemma_tok_vocab_t * vocab, const uint16_t * tokens, size_t n_tokens, char * dst, size_t dst_cap, size_t * bytes_written)
Decode an array of 16-bit token IDs losslessly into a character buffer. Reconstructs the exact original text.
Parameters
vocab Vocabulary handle (or NULL for default).
tokens Array of token IDs.
n_tokens Count of tokens.
dst Destination character buffer.
dst_cap Capacity of dst in bytes.
bytes_written Receives the number of bytes written (excluding NUL).
Returns
0 on success, -1 on invalid token ID, -2 on buffer overflow.
int lemma_tok_encode (const lemma_tok_vocab_t * vocab, const char * src, size_t src_len, uint16_t * dst, size_t dst_cap, size_t * dst_len)
Encode a text string losslessly into an array of 16-bit token IDs. Words matching DAFSA/lexicon emit their Word Token IDs (256..32767). Known delimiters emit Delimiter Token IDs. Fallback: bytes are emitted directly as byte primitives (0..255). Guaranteed 100% invertible: lemma_tok_decode(vocab, tokens, n, out, cap, NULL) == src.
Parameters
vocab Vocabulary handle (or NULL for default).
src Source text.
src_len Length of source text in bytes.
dst Destination array for token IDs.
dst_cap Capacity of dst in uint16_t elements.
dst_len Receives the number of tokens written.
Returns
0 on success, -1 on buffer overflow or NULL args.
int lemma_tok_is_word (const lemma_tok_vocab_t * vocab, uint16_t token_id)
Check if a token ID represents a searchable word (vs delimiter/whitespace/raw byte).
Parameters
vocab Vocabulary handle.
token_id Token ID.
Returns
1 if token is a searchable word, 0 otherwise.
const lemma_tok_vocab_t * lemma_tok_vocab_default (void )
Initialize or obtain the shared global/default vocabulary.
Returns
Shared vocabulary handle, or NULL on failure.
int lemma_tok_vocab_load_file (lemma_tok_vocab_t * v, const char * filepath)
Load additional words from a plaintext wordlist file into a dynamic vocabulary.
Parameters
v Dynamic vocabulary instance.
filepath Path to plaintext file (one word per line).
Returns
0 on success, -1 on failure.
int lemma_tok_vocab_match (const lemma_tok_vocab_t * vocab, const char * term, int max_edit_distance, uint16_t * out_token_ids, size_t out_cap, size_t * out_count)
Query the vocabulary for fuzzy or substring matching token IDs (Tier 1). Finds all Word Token IDs within Levenshtein distance max_edit_distance of term (or matching prefix/substring).
Parameters
vocab Vocabulary handle (or NULL for default).
term Query term (folded via lemma_fold).
max_edit_distance Maximum Levenshtein distance (0 = exact/prefix, 1 or 2 = fuzzy).
out_token_ids Destination array for matching token IDs.
out_cap Capacity of out_token_ids.
out_count Receives count of matching token IDs.
Returns
0 on success, -1 on buffer overflow or NULL args.
lemma_tok_vocab_t * lemma_tok_vocab_new (const lemma_tok_vocab_t * base)
Create a dynamic vocabulary instance (optionally extending a base vocab).
Parameters
base Optional base vocabulary (can be NULL or lemma_tok_vocab_default()).
Returns
New vocabulary instance.
void lemma_tokenize (const char * folded, void(* cb )(const char *tok, size_t len, void *user), void * user)
Tokenize non-whitespace word tokens in folded. Iterates non-whitespace word tokens in folded and invokes cb(token, len, user) for each.
Parameters
folded Folded text to tokenize.
cb Callback invoked per token.
user Opaque user pointer passed to cb.
int lemma_unindex (lemma_db_t * db, const char * field, const char * row_id)
Remove every posting for (field, row_id) plus the doc side-table. The differential inverse of lemma_index (Phase 2A rec_axis_unstore mechanism; RECALL-KERNEL.md "rec_axis_store convention"): index โ unindex leaves zero residual postings for the row and no doc entry, at a cost โ tokens of the row (never O(store), never a scan โ the doc side-table is walked backwards to recover the exact posting keys). Idempotent: an absent (field, row) reads as 0, not an error. Zero new stored state; memory-only.
Parameters
db Index handle.
field Field name.
row_id Row id string.
Returns
0 on success, -1 on invalid arguments.
int lemma_unindex_ref (lemma_db_t * db, const char * field, rec_ref_t row_id)
rec_ref_t-native convenience wrapper for lemma_unindex. Same id-uniformity boundary as lemma_index_ref: formats row_id as the canonical decimal string internally. Same return contract as lemma_unindex.
Parameters
db Index handle.
field Field name.
row_id Row ref.
Returns
0 on success, -1 on invalid arguments.
int rec_axis_fill_tokens (lemma_db_t * db, const char * field, const char * query, int phrase, rec_set_t * out)
Recall-kernel lexical filler (adapter contract, see rec.h). The exact set of refs whose row_id matches query in field. The consumer must index by its own decimal rec_ref_t (lemma_index(db, field, "42", value)) โ the filler pushes the parsed ids and lemma_rank reverses the mapping. Semantics are identical to lemma_query (phrase=0) / lemma_query_phrase (phrase=1). Refs are appended to out (additive) and the set is sealed (0 = ok).
Parameters
db Index handle.
field Field name.
query Query string.
phrase Non-zero for phrase semantics.
out rec_set_t to append matching refs to.
Returns
0 ok; -1 on NULL args or when a matched row_id is not strictly decimal or cannot be represented as a rec_ref_t (> UINT32_MAX) (fill aborts; raw entry points still serve non-numeric stores). Zero-token or empty queries yield a sealed empty set (mirrors the handled=0 no-op).
void * rec_axis_open (const char * spec)
Open a lemma index from an opaque spec string. Follows the rec_axis_open convention (RECALL-KERNEL.md, optional CLI-open convention, not part of libcorm's core rec_query registry API). Returns the ctx a caller then passes to rec_axis_set_ctx() โ the lemma_db_t* directly. Two spec shapes (2B-2):
- decimal mask (or empty/NULL): the lemma_open() mask, 0 = corm default โ a plain in-memory index, nothing seeded.
- a path (contains '/'): the 2B-2 primary-seeded rebuild. The directory is scanned for a <primary>.roster sidecar (written by the corm CLI's roster persistence); the primary corm store it prefixes is opened (:a:s raw and :a:u decimal alike) and every record is re-indexed under the canonical text field. A rebuild emits a one-line budget note (docs + ms) on stderr so callers can measure (mm-plan U4). A path whose sidecar is missing or whose primary cannot be opened warns loudly and falls back to an empty memory index โ it never silently serves stale or empty results.
Parameters
spec Spec string as described above.
Returns
A lemma_db_t* usable as the rec_axis ctx, or NULL on failure.
int rec_axis_readback (void * ctx, rec_ref_t ref, char ** blob_out, size_t * n_out)
Read a ref's stored (folded) text (rec_axis_readback). One malloc'd buffer holding the stored (folded โ lowercased, accent-preserving) text as one NUL-joined entry; n_out counts the display chars; absent โ NULL/0, still 0; NULL out args โ -1. Not kernel API, not in the rec_axis_t vtable; dlsym'd like rec_axis_open.
Parameters
ctx Index handle (the lemma_db_t*).
ref Row ref.
blob_out Receives the malloc'd text buffer (caller frees).
n_out Receives the display char count.
Returns
0 on success, -1 on error.
int rec_axis_store (void * ctx, const char * spec, rec_ref_t ref, const char * value)
Store a ref's whole value text (rec_axis_store convention). Phase 2A store contract (RECALL-KERNEL.md "rec_axis_store convention", optional CLI-specific โ not libcorm core API). The consumer passes (ref, value) blindly; lemma stores the WHOLE value string's text under LEMMA_AXIS_TEXT_FIELD (replace-in-place: store erases any previous entry for the ref first, so a ref owns exactly one doc). Not kernel API, not in the rec_axis_t vtable; dlsym'd like rec_axis_open.
Parameters
ctx Index handle (the lemma_db_t*).
spec Reserved โ always NULL.
ref Row ref.
value Whole value string to index.
Returns
0 ok; -1 with errno=EINVAL on bad args/empty value; ctx NULL โ -1.
int rec_axis_unstore (void * ctx, rec_ref_t ref)
Remove a ref's stored doc (rec_axis_unstore). Removes the ref's entry under LEMMA_AXIS_TEXT_FIELD; idempotent: absent โ 0. Not kernel API, not in the rec_axis_t vtable; dlsym'd like rec_axis_open.
Parameters
ctx Index handle (the lemma_db_t*).
ref Row ref.
Returns
0 on success; ctx NULL โ -1.
Author
Generated automatically by Doxygen for lemma from the source code.