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 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
⌂ 换网址