goahocorasick

package module
v0.0.0-...-5fd6c24 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2016 License: MIT Imports: 2 Imported by: ? modules, 0 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

Aho–Corasick algorithm

Intro

A faster and more effective implement of Aho-Corasick algorithm in golang and supports Both Chinese and English. To improve the performance and reduce memory usage, the program uses Double Array Trie instead of common Linked List Trie. In the benchmark, it is 10 times faster than the most popular AC algorithm implement in golang @ github and tenth of its memory usage. You can find more information in the benchmark parts.

Besides Multi-Pattern Search using AC algorithm, the program also provides "exact match search" using Double Array Trie

Aho-Corasick algorithm is first presented in the paper below:

Efficient string matching: an aid to bibliographic search

the wikipedia link is: aho-corasick algorithm

Usage

Multi-Pattern Search Example

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "io"
    "os"
 )

import (
	"github.com/anknown/ahocorasick"
)

func ReadRunes(filename string) ([][]rune, error) {
    dict := [][]rune{}

    f, err := os.OpenFile(filename, os.O_RDONLY, 0660)
    if err != nil {
        return nil, err
    }

    r := bufio.NewReader(f)
    for {
        l, err := r.ReadBytes('\n')
        if err != nil || err == io.EOF {
            break
        }
        l = bytes.TrimSpace(l)
        dict = append(dict, bytes.Runes(l))
    }

    return dict, nil
}

func main() {
    dict, err := ReadRunes("your_dict_files")
    if err != nil {
        fmt.Println(err)
        return
    }

    content := []rune("your text")

    m := new(goahocorasick.Machine)
    if err := m.Build(dict); err != nil {
        fmt.Println(err)
        return
    }

    terms := m.MultiPatternSearch(content, false)
    for _, t := range terms {
        fmt.Printf("%d %s\n", t.Pos, string(t.Word))
    }
}

I do not provide read file API because I think your dict may coming form other source

Benchmark

Multi-Pattern Search

compare with cloudflare/ahocorasick who receives most stars and forks in all the implements written in golang

To Run Benchmark, go to test dir

go build benchmark.go

./benchmark
  • For Chinese Test

Dictionary contains 153,151 words, Text contains 777,277 words

====================================================================
							cost(million sec)	memory usage(MBytes)
cloudflare/ahocorasick		28926				1911
anknown/ahocorasick			1814				155
====================================================================
  • For English Test

Dictionary contains 127,141 words, Text contains 674,669 words

====================================================================
							time(million sec)	memory usage(MBytes)
cloudflare/ahocorasick		19835				1340
anknown/ahocorasick			1619				203
====================================================================
License

MIT License

Documentation

Index

Constants

View Source
const FAIL_STATE = -1
View Source
const ROOT_STATE = 1

Variables

This section is empty.

Functions

This section is empty.

Types

type Machine

type Machine struct {
	Trie    *godarts.DoubleArrayTrie
	Failure []int
	Output  map[int]([][]rune)
}

func (*Machine) Build

func (m *Machine) Build(keywords [][]rune) (err error)

func (*Machine) ExactSearch

func (m *Machine) ExactSearch(content []rune) [](*Term)

func (*Machine) MultiPatternSearch

func (m *Machine) MultiPatternSearch(content []rune, returnImmediately bool) [](*Term)

func (*Machine) PrintFailure

func (m *Machine) PrintFailure()

func (*Machine) PrintOutput

func (m *Machine) PrintOutput()

type Term

type Term struct {
	Pos  int
	Word []rune
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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