retrieval

package
v0.10.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package retrieval turns identifiers and a plain-language question into the terms a lexical index stores and looks up.

It holds no state and knows nothing about the snapshot: it maps text to tokens, and the caller decides what to do with them. That boundary is what keeps the index format free of language rules and the language rules free of the index format.

Index

Constants

View Source
const TermPrefixRunes = 5

TermPrefixRunes is how many runes of a token make its term.

It is the one constant of this layer that was chosen by measurement rather than by argument, and the measurement is in benchmarks: over the identifiers of this repository, folding tokens to their first runes recovers the morphological pairs a query needs -- `registration` finding `register`, `parser` finding `parse`, `resolution` finding `resolve` -- which neither exact matching nor an English stemmer does. Exact matching missed a quarter of them; Porter2 misses agent nouns and `-ation` nominalisations, which is most of how code names things.

Four and five scored the same recall and differed only in noise, so the value here is the quieter of the two. Lowering it widens every answer; raising it past six starts losing the pairs that motivate the fold at all.

Variables

This section is empty.

Functions

func AppendTextKeys

func AppendTextKeys(dst []TermKey, spans []Span, value string) ([]TermKey, []Span)

AppendTextKeys splits value and folds it in one pass, for the callers that hold no span buffer of their own.

func QueryWords

func QueryWords(intent string, keywords []string) []string

QueryWords is QueryTerms with the words kept.

A caller that reports which words of a question matched and which did not has to name them the way they were written: `registration` and `registry` are one term, and an answer that showed the key, or the other spelling, would read as a tool that misheard the question. The first spelling wins because it is the one the reader typed first.

func Score

func Score(signals Signals) float64

Score ranks one candidate. Higher is earlier; the number means nothing on its own.

That last sentence is the contract, not a caveat. The score exists to order candidates against each other inside one answer: multiply every weight below by ten and the order is identical. It is therefore never published -- a caller shown `68.05` learns nothing it can act on, and would reasonably read it as a confidence this layer cannot claim.

The shape is a product of independent multipliers over a base of term hits, because the signals are independent: being a function does not make a symbol more or less exported, and a question that hits two terms is not twice as likely to want a test fixture.

Types

type Signals

type Signals struct {
	// Hits is how many distinct terms of the question this symbol carries.
	Hits int
	// Frequencies are the document frequencies of those hit terms, in any order.
	// A term matching most of the corpus separates nothing, and this is what
	// replaces a stopword list.
	Frequencies []int
	// Lengths are the rune counts of the words that hit, positionally aligned
	// with Frequencies. Frequency alone cannot tell a question's grammar from
	// its vocabulary: measured on this repository, `is` carries 178 of 22,299
	// symbols, which the discount below reads as almost pure signal, and it put
	// IsValidModuleKey first for three unrelated questions. Length can tell
	// them apart, and it does it without an English word list: the fold is a
	// prefix of five runes, so a two-rune word matched a two-rune segment of an
	// identifier, and in code those are particles -- Is, To, Of, At, In -- while
	// the vocabulary a question is actually about is longer than the fold.
	Lengths []int
	// Symbols is the size of the corpus the frequencies are counted against.
	Symbols int
	// Kind is the symbol's kind as the graph spells it.
	Kind string
	// Exported reports whether the declaration is visible outside its package.
	Exported bool
	// Path is the repository-relative path of the declaring file.
	Path string
	// Callers is how many resolved references point at the symbol.
	Callers int
	// Neighbours is how many terms of the question this symbol does not carry
	// itself and reaches through a resolved outgoing edge.
	//
	// It is the one signal in this struct that no text search can have. The
	// question that named this file -- where is every tool of this server
	// registered -- is answered by a function whose own name carries one term of
	// it and which calls twelve symbols carrying another; the file that holds it
	// ranked thirteenth on text alone and no amount of weighing its words moves
	// it, because the words are not there. Grep cannot see it, and a graph whose
	// edges come from matching names cannot claim it: an edge asserted by a
	// resolver is what makes this evidence rather than a coincidence.
	Neighbours int
}

Signals are what a candidate symbol carries that a question did not ask for and that still decides whether it is the answer.

Every field is already in the snapshot's symbol row or derivable from it. The two the graph cannot supply are absent on purpose: there is no docstring to weigh, and no recency, because a snapshot has one age and it is the same for every row in it.

type Span

type Span struct {
	Start uint32
	End   uint32
}

Span is one token's half-open byte range inside the string it came from.

Ranges rather than strings, because splitting runs over every symbol of the graph on every index pass and the caller almost always wants to intern the token rather than keep it: a []string would allocate the slice and one string per part, and then be thrown away a line later.

func AppendSpans

func AppendSpans(dst []Span, value string) []Span

AppendSpans appends the token ranges of one identifier to dst and returns it.

The algorithm is Daniel G. Taylor's `casing.Split`, adapted to append ranges instead of building a []string; see the attribution below. It handles the four conventions a mixed-language graph actually contains -- CamelCase, lowerCamel, snake_case, kebab-case -- plus digits, symbols and non-ASCII letters, and it drops separators instead of emitting them as tokens.

Behaviour is pinned against the upstream test table in split_test.go, so an adaptation that drifted from the original would fail rather than quietly answer something else.

Attribution: derived from github.com/danielgtaylor/casing v1.0.0 (casing.go, func Split), Copyright 2021 Daniel G. Taylor, MIT licence. Copied rather than imported: the module is frozen at v1.0.0 and declares go 1.15, so it offers no upgrade stream to follow while pulling six modules into go.sum for one self-contained function, none of which reach the binary. The permission notice travels in THIRD_PARTY_NOTICES.md.

type TermKey

type TermKey uint64

TermKey is a token folded to the term it is indexed under: the first TermPrefixRunes runes, lowercased, packed into one integer.

An integer rather than a string, because the indexes of this snapshot are built on the rule that nothing compares bytes on the hot path. A term of five runes is at most a handful of bytes, and packing them big-endian keeps integer order equal to byte order, so one uint64 comparison replaces a string comparison and the term dictionary needs no arena, no offset table and no copying to answer.

TermKeyNone is the key of a token that folds to nothing, which is what an empty or unusable token produces. It is never stored.

const TermKeyNone TermKey = 0

TermKeyNone is the zero key: no token folds to it, so it doubles as "absent".

func AppendKeys

func AppendKeys(dst []TermKey, spans []Span, value string) []TermKey

AppendKeys folds every token of value onto dst, skipping what folds to nothing. The buffer is the caller's so that indexing every symbol of the graph costs no allocation per symbol.

func Fold

func Fold(token string) TermKey

Fold returns the term key of one token, or TermKeyNone when the token carries nothing to index.

Single characters fold to nothing on purpose. A one-letter token is a loop variable or the tail of a split acronym, it appears in a large share of the corpus, and a term that matches a large share of the corpus tells a caller nothing it did not already know.

func QueryTerms

func QueryTerms(intent string, keywords []string) []TermKey

QueryTerms folds a plain-language question into the keys to look up, in the order they were written and without repeats.

It applies no stopword list. An English list is the wrong data twice over: it holds `himself` and `yourselves`, which no identifier contains, and it lacks `get`, `set`, `new`, `data` and `handler`, which are the terms that actually say nothing in a codebase -- and it would be an English answer for a graph that also holds Rust, Python, Dart and TypeScript. What a term is worth is decided where it can be measured instead: by how much of the corpus it matches, which the index already knows.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL