searchtext

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package searchtext turns text into the words a search index keeps and a query into the words it looks for: folded (lowercase, "ё" as "е") and stemmed in a language (Snowball), so "cats" finds "cat".

Index

Constants

View Source
const MaxQueryWords = 8

MaxQueryWords - the words of a query looked for; the rest are ignored.

View Source
const MaxWordLen = 64

MaxWordLen - the longest word kept, in bytes (the index column's size); a longer one is cut at a letter.

Variables

This section is empty.

Functions

func Fold

func Fold(s string) string

Fold - s lowercased, "ё" as "е": how the search compares text.

func Stem

func Stem(word, lang string) string

Stem - the stem of the folded word in the language lang ("pt-BR" - Portuguese); the word as it is in a language without a stemmer.

func Tokens

func Tokens(text string) []string

Tokens - the folded words of text in their order: runs of letters and digits, at least 2 letters long.

func Words

func Words(text, lang string) []string

Words - the words of text (plain, not HTML) an index keeps: its tokens stemmed in lang, each once, cut to MaxWordLen.

Types

type Part

type Part struct {
	Text  string
	Match bool
}

Part - a piece of a snippet: text, a word of the query (Match) or not.

type Query

type Query struct {
	Phrase string
	// Words - per word of the query (Tokens, each once, at most
	// MaxQueryWords) its forms: the word and its stems in the languages.
	Words [][]string
}

Query - a search query: the phrase (folded, single spaces) and its words, each as the forms an indexed word may have.

func ParseQuery

func ParseQuery(q string, langs []string) Query

ParseQuery - the query q in the languages langs (the site's: the text may be in any of them).

func (Query) Matches

func (q Query) Matches(words []string) bool

Matches - whether the indexed words have every word of the query: one of its forms, or (Prefix) a word beginning with one.

func (Query) Prefix

func (q Query) Prefix(i int) bool

Prefix - whether the i-th word is matched as the beginning of a word: the last one, likely still being typed.

func (Query) Snippet

func (q Query) Snippet(text, lang string, width int) (Snippet, bool)

Snippet - the piece of the plain text around its first word of the query (the words stemmed in lang, as Words does): from snippetLead letters before it, about width letters, cut between words, the query's words marked. False - the text has none of them.

type Snippet

type Snippet struct {
	Parts []Part
	// Before, After - the text goes on before (after) the piece.
	Before, After bool
}

Snippet - a piece of a text around a query's word in it.

Jump to

Keyboard shortcuts

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