Documentation
¶
Overview ¶
This package contains everything you need to work with word-based searching. Its internals are founded on the Levenshtein distance. Check https://github.com/typeflow/typeflow-go for an example.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // This error occurs when trying to rollback more than needed OutOfRangeRollbackError = errors.New("Unexpected rollback: out of range") EmptyStateError = errors.New("Unexpected: current state is empty") )
Errors
Functions ¶
func LevenshteinDistance ¶
This implementation takes advantage of the 2-columns approach since doesn't expose any incremental update functionality You are encouraged to use this function when simply interested in the levenshtein distance between 2 words
Types ¶
type LState ¶
type LState struct {
// contains filtered or unexported fields
}
An LState encapsulates the current state of a levenshtein-based distance computation. In particular, the matrix-based levenshtein computation is used. An LState instance can be updated or rolled back which is useful for iterative word computations. It will take care of managing the internal state and memory allocation for you.
func InitLState ¶
Initializes an empty LState. This is the first step required for working with a matrix-based levenshtein computation.
func (*LState) Distance ¶
Returns the newly computed distance or math.MaxInt32 if no distance has been computed yet. This method has complexity O(1) as the distance is computed upon every UpdateState call.
func (*LState) RollbackBy ¶
Rolls back the current state. Specify the amount of characters to roll back for the source string through the chars param
func (*LState) UpdateState ¶
Updates the current state with param source.
Since LState can be rolled back, source can also represent a slice to be appended to the previous one passed through a previous call to UpdateState.
E.g. you may want to compare the words 'levenshtein' and 'einstein' in two steps after having initialized the state using InitLState('einstein')
1) the first UpdateState will be called passing in 'leven' as source.
2) the second UpdateState will be called passing in 'stein' as source.
You will then be able to call Distance() on the updated state and get 4 as result.
type Match ¶
type Match struct {
// The actual string which matched
// the requested string
String string `json:"string"`
// The similarity value for the
// current word.
// Similarity is 1 when
// the two words are equal.
// See github.com/typeflow/typeflow-go for
// more details on how the similarity is computed.
Similarity float32 `json:"similarity"`
}
Represents a match found
type WordSource ¶
type WordSource struct {
// contains filtered or unexported fields
}
func NewWordSource ¶
func NewWordSource() (ws *WordSource)
NewWordSource Initializes a new empty WordSource
func (*WordSource) FindMatch ¶
func (ws *WordSource) FindMatch(substr string, minSimilarity float32) (matches []Match, err error)
FindMatch Finds a match among the current words in the source. minSimilarity is the minimum accepted similarity to use when filling the matches slice. Param substr is the string to match against.
func (*WordSource) SetSource ¶
func (ws *WordSource) SetSource(strs []string)
SetSource sets the given strings slice as the current source after applying the given filters in order
