Skip to content

Go (Golang) implementation of the Aho-Corasick search algorithm

License

Notifications You must be signed in to change notification settings

LrsK/gomultifast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Gomultifast

This is a Go implementation of the Aho-Corasick search algorithm.

The code is a port from the great Multifast library written in C. Please visit http://multifast.sourceforge.net/ to learn more about it.

Usage example

package main

import (
        "fmt"
        "github.com/lrsk/gomultifast"
)

func main() {
        // Aho-Corasick automaton
        atm := gomultifast.NewAutomaton()
        
        // Add search terms, of course this can be done in a loop from a file etc.
        key := "term1"
        search_term := "golang"
        tmp_patt := gomultifast.NewPattern(key, search_term)
        atm.Add(tmp_patt)
        
        key = "term21"
        search_term = "example.com"
        tmp_patt := gomultifast.NewPattern(key, search_term)
        atm.Add(tmp_patt)

        // No more adding of terms after this
        atm.Finalize()

        fmt.Printf("Finished adding search terms\n")

        //atm.Print() // This will print out the nodes for debugging

        atm.Search("thisissometextwithgolanginit", false, match_handler, "")
        atm.Search("iloveexamplesthatuseexample.cominthem", false, match_handler, "")
        atm.Search("thisisjust some text here", false, match_handler, "")

}

// Callback function to run against matches
func match_handler(matchp gomultifast.Match, matchedLine string, param string) bool {
        for _, m := range matchp.Patterns {
                // Print search hit
                fmt.Printf("%s,%s\n", m.Ident, matchedLine)
        }
        return false
}

About

Go (Golang) implementation of the Aho-Corasick search algorithm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages