ahocorasick

package module
v0.0.0-...-46b7552 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 2 Imported by: 2

README

ahocorasick

Aho-Corasick string matching algorithm for golang

package main

import (
	"fmt"
	"log"

	"github.com/gansidui/ahocorasick"
)

func main() {
	ac := ahocorasick.NewMatcher()

	dictionary := []string{"hello", "world", "世界", "google", "golang", "c++", "love"}
	ac.Build(dictionary)

	s := "hello世界, hello google, i love golang!!!"
	ret := ac.Match(s)

	for i, _ := range ret {
		original := dictionary[ret[i].Index]
		matched := s[ret[i].EndPosition-len(original) : ret[i].EndPosition]
		if original != matched {
			log.Fatal()
		}
		fmt.Println(original, matched)
	}
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

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

func BuildNewMatcher

func BuildNewMatcher(dictionary []string) *Matcher

func NewMatcher

func NewMatcher() *Matcher

func (*Matcher) Build

func (m *Matcher) Build(dictionary []string)

initialize the ahocorasick

func (*Matcher) Match

func (m *Matcher) Match(s string) []*Term

string match search return all strings matched as indexes into the original dictionary and their positions on matched string

type Term

type Term struct {
	// indicates the index of the matching string in the original dictionary
	Index int

	// indicates the ending position index of the matched keyword in the input string s
	EndPosition int
}

Jump to

Keyboard shortcuts

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