Documentation
¶
Index ¶
- func CustomSentenceSplitter(isSeparator func(rune) bool) func(string) []string
- func Highlight(text string, leftMarker string, rightMarker string, positions ...[2]int) string
- func SentenceSplitter(input string) []string
- func SnapMarkersToWordBoundaries(textRunes []rune, markers [][2]int) [][2]int
- func Snippet(text string, position [2]int, wordCount int) (string, [2]int)
- func WordSplitter(input string) []string
- type Config
- type Document
- type IndexedProcessor
- type IndexedRune
- type IndexedRuneGroup
- type MatchedDocument
- type Processor
- type Splitter
- type Storage
- func (s *Storage) AddDocuments(ctx context.Context, docs ...Document) error
- func (s *Storage) ApplyConfig(cfg Config) *Storage
- func (s *Storage) Close() error
- func (st *Storage) DeleteDocuments(ctx context.Context, ids ...string) error
- func (s *Storage) Search(ctx context.Context, threshold float64, query string, types ...string) ([]MatchedDocument, error)
- type Transformer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CustomSentenceSplitter ¶
CustomSentenceSplitter is same as SentenceSplitter, except we cand use custom-defined separator.
func SentenceSplitter ¶
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 ¶
Adjust positions of several markers to closest word boundaries. It doesn't mutate the original positions.
func Snippet ¶
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 ¶
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 ¶
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 Splitter ¶
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 ¶
Open the search storage in the specified path.
func (*Storage) AddDocuments ¶
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 (*Storage) DeleteDocuments ¶
DeleteDocuments remove the documents in the storage.
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.