ahocorasick

package module
v0.0.0-...-d944ae6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

ahocorasick

A Golang implementation of the Aho-Corasick string matching algorithm

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Match

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

type Matcher

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

Matcher is returned by NewMatcher and contains a list of blices to match against

func NewMatcher

func NewMatcher(dictionary [][]byte) *Matcher

NewMatcher creates a new Matcher used to match against a set of blices

func NewStringMatcher

func NewStringMatcher(dictionary []string) *Matcher

NewStringMatcher creates a new Matcher used to match against a set of strings (this is a helper to make initialization easy)

func (*Matcher) Contains

func (m *Matcher) Contains(in []byte) bool

Contains returns true if any string matches. This can be faster than Match() when you do not need to know which words matched.

func (*Matcher) Match

func (m *Matcher) Match(in []byte) []int

Match searches in for blices and returns all the blices found as indexes into the original dictionary.

This is not thread-safe method, seek for MatchThreadSafe() instead.

Example
m := NewMatcher([][]byte{
	[]byte("he"),
	[]byte("she"),
	[]byte("his"),
	[]byte("hers"),
	[]byte("she"),
})

matches := m.Match([]byte("usher"))
fmt.Print(matches)
Output:

[4 0]

func (*Matcher) MatchIndex

func (m *Matcher) MatchIndex(in []byte) []Match
Example
m := NewMatcher([][]byte{
	[]byte("he"),
	[]byte("she"),
	[]byte("his"),
	[]byte("hers"),
	[]byte("she"),
})

matches := m.MatchIndex([]byte("usher"))
fmt.Print(matches)
Output:

[{4 1} {0 2}]

func (*Matcher) MatchIndices

func (m *Matcher) MatchIndices(in []byte) iter.Seq[Match]
Example
matcher := NewMatcher([][]byte{
	[]byte("he"),
	[]byte("she"),
	[]byte("his"),
	[]byte("hers"),
	[]byte("she"),
})

for m := range matcher.MatchIndices([]byte("usher")) {
	fmt.Printf("%d ", m)
}
Output:

{4 1} {0 2}

func (*Matcher) MatchThreadSafe

func (m *Matcher) MatchThreadSafe(in []byte) []int

MatchThreadSafe provides the same result as Match() but does it in a thread-safe manner. Uses a sync.Pool of haystacks to track the uniqueness of the result items.

func (*Matcher) Matches

func (m *Matcher) Matches(in []byte) iter.Seq[int]
Example
matcher := NewMatcher([][]byte{
	[]byte("he"),
	[]byte("she"),
	[]byte("his"),
	[]byte("hers"),
	[]byte("she"),
})

for m := range matcher.Matches([]byte("usher")) {
	fmt.Printf("%d ", m)
}
Output:

4 0

Jump to

Keyboard shortcuts

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