antlinker / go-dirtyfilter

Golang基于DFA算法实现的敏感词过滤
Apache License 2.0
339 stars 72 forks source link

合法内容replace之后输出为空 #3

Open fwhappy opened 7 years ago

fwhappy commented 7 years ago
package main

import (
    "fmt"

    "github.com/antlinker/go-dirtyfilter"
    "github.com/antlinker/go-dirtyfilter/store"
)

var (
    filterText = `我是需要过滤的内容,内容为:**文@@件,需要过滤。。。hello`
)

func main() {
    memStore, err := store.NewMemoryStore(store.MemoryConfig{
        DataSource: []string{"哈哈"},
    })
    if err != nil {
        panic(err)
    }
    filterManage := filter.NewDirtyManager(memStore)
    result, err := filterManage.Filter().Replace(filterText, '*')
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}

这段代码,应该是原样输出,实际输出是空字符串

func (nf *nodeFilter) Replace(text string, delim rune) (string, error) {
    uchars := []rune(text)
    idexs := nf.doIndexes(uchars)
    if len(idexs) == 0 {
        // return "", nil
        // 合法内容,原样输出
        return text, nil
    }
    for i := 0; i < len(idexs); i++ {
        uchars[idexs[i]] = rune(delim)
    }
    return string(uchars), nil
}

是不是修改一下Replace函数,在未找到屏蔽词的时候,原样输出

OpenJarvisAI commented 7 months ago

这就是个垃圾代码,无语了全是错误的结果