typosearch

package module
v0.0.0-...-0bf69a7 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 12 Imported by: ? 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.

README

TypoSearch

TypoSearch is experimental search engine for typo-tolerant search. Uses SQLite as backend.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomSentenceSplitter

func CustomSentenceSplitter(isSeparator func(rune) bool) func(string) []string

CustomSentenceSplitter is same as SentenceSplitter, except we cand use custom-defined separator.

func Highlight

func Highlight(
	text string,
	leftMarker string,
	rightMarker string,
	positions ...[2]int,
) string

Highlight the matches in text.

func SentenceSplitter

func SentenceSplitter(input string) []string

SentenceSplitter splits input into sentences, keeping any whitespace that follows a sentence-ending punctuation mark attached to that sentence. It also keeps repeated punctuation (e.g. "...", "?!") and trailing closing quotes/brackets (e.g. the `"` in `He said "stop."`) as part of the same sentence, rather than splitting mid-punctuation or leaking a quote mark into the next sentence. For example:

Input: "Is that so?! Yes... Indeed."

Output: - 0: "Is that so?! " - 1: "Yes... " - 2: "Indeed."

func SnapMarkersToWordBoundaries

func SnapMarkersToWordBoundaries(textRunes []rune, markers [][2]int) [][2]int

Adjust positions of several markers to closest word boundaries. It doesn't mutate the original positions.

func Snippet

func Snippet(text string, position [2]int, wordCount int) (string, [2]int)

Snippet extracts a string from text around the marker position. It expands outward up to wordCount and adds ellipsis "..." if bounds are not reached.

func WordSplitter

func WordSplitter(input string) []string

SplitByWord splits input into words, keeping any spaces that follow a word attached to that word (rather than dropping them or starting a new segment). For example:

Input: "Hello. How are you?I'm fine! Great."

Output: - 0: "Hello. " - 1: "How are you?" - 2: "I'm fine! " - 3: "Great."

Types

type Config

type Config struct {
	// Transformers is map between Document type and its Transformer. Transformer
	// itself is object to preprocess the document content, before it's tokenized
	// and stored in indexes [Storage].
	//
	// This transformer later will be used on the submitted [Document], but won't be
	// used on user queries. For user queries, developer responsible to process it
	// themselves.
	Transformers map[string]Transformer
}

Config is the configuration for this search engine.

type Document

type Document struct {
	// Identifier is the unique identifier for this Document.
	Identifier string
	// Type is the type of this Document.
	Type string
	// Content is the text body of this Document.
	Content string
}

Document is the text document that will be indexed to be later searched on.

type IndexedProcessor

type IndexedProcessor func(string) []IndexedRune

IndexedProcessor is like the Processor, but returns the processed string as list of IndexedRune, which is a rune + its index in the original string.

type IndexedRune

type IndexedRune struct {
	R     rune
	Index int
}

Container for a processed rune and its position in the original string.

func IndexProcessedString

func IndexProcessedString(original, processed string) []IndexedRune

IndexProcessedString is a helper function to calculate the difference between the original and the processed string, then return list of IndexedRune.

type IndexedRuneGroup

type IndexedRuneGroup []IndexedRune

Group of IndexedRune, with various helper function.

func (IndexedRuneGroup) Range

func (irg IndexedRuneGroup) Range() (int, int)

Returns the start and end index for these runes.

func (IndexedRuneGroup) String

func (irg IndexedRuneGroup) String() string

Returns the runes as combined string.

type MatchedDocument

type MatchedDocument struct {
	Document
	// Markers is list of rune-based marker position inside Document. Represented
	// as 2-tuple of `[start, end]`.
	Markers [][2]int
	// WordMarkers is list of word-based marker position inside Document. Represented
	// as 2-tuple of `[start, end]`.
	WordMarkers [][2]int
	// Score is confidence level for this [Document].
	Score float64
}

MatchedDocument is the document that matched with the search query.

type Processor

type Processor func(string) string

Processor is function to process a string, then return the processed string.

type Splitter

type Splitter func(string) []string

Splitter is function to split string into several groups. Will be run before IndexedProcessor applied.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage is the container for storing trigram indexes for documents that will be searched later. Use sqlite3 as database engine.

func OpenStorage

func OpenStorage(ctx context.Context, path string, cfg Config) (*Storage, error)

Open the search storage in the specified path.

func (*Storage) AddDocuments

func (s *Storage) AddDocuments(ctx context.Context, docs ...Document) error

AddDocuments save and index the documents into the storage. If the document with matching ID already exist, it will be updated.

func (*Storage) ApplyConfig

func (s *Storage) ApplyConfig(cfg Config) *Storage

ApplyConfig applies the Config to the Storage.

func (*Storage) Close

func (s *Storage) Close() error

Close closes the underlying database for the search storage.

func (*Storage) DeleteDocuments

func (st *Storage) DeleteDocuments(ctx context.Context, ids ...string) error

DeleteDocuments remove the documents in the storage.

func (*Storage) Search

func (s *Storage) Search(ctx context.Context, threshold float64, query string, types ...string) ([]MatchedDocument, error)

Search the storage for suitable documents. Developer should normalize the query before submitting it to this function.

type Transformer

type Transformer struct {
	// Splitter is function to split runes into several groups before it being
	// processed and tokenized.
	Splitter Splitter
	// Processor is function to process a string into a normalized string. Used when
	// querying, so must have the same output as IndexedProcessor.
	Processor Processor
	// IndexedProcessor is like Processor, but returns a list of runes + its
	// original position in string. Used when indexing document, so must have the
	// same output as Processor.
	IndexedProcessor IndexedProcessor
}

Transformer is pair of function to handle Document before indexed.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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