github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/algorithm/datastructures/ac/aho_corasick_test.go (about)

     1  package ac
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestAC_Match(t *testing.T) {
     8  
     9  	acAuto := NewAc()
    10  	dirtWorld := []string{
    11  		"好屌",
    12  		"妈的",
    13  		"fuck",
    14  	}
    15  	acAuto.AddWorlds(dirtWorld)
    16  	t.Log(acAuto.Contains("好屌"))
    17  
    18  	acAuto.BuildFailurePointer()
    19  
    20  	acAuto.Remove("fuck")
    21  	acAuto.BuildFailurePointer()
    22  
    23  	text := "妈的-我看他说话的语气,好屌啊 fuck"
    24  	afterText := []rune(text)
    25  	acAuto.Match(text, func(start, end int) {
    26  		//t.Logf("%v [%v-%v]\n", text[start:end+1], start, end)
    27  		for i := start; i <= end; i++ {
    28  			afterText[i] = '*'
    29  		}
    30  	})
    31  	t.Log(string(afterText))
    32  }