Skip to content

Commit

Permalink
add target result
Browse files Browse the repository at this point in the history
  • Loading branch information
bean-du committed Apr 20, 2021
1 parent cf94afb commit 9d20e4a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
43 changes: 30 additions & 13 deletions dfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
defaultInvalidWorlds = " ,~,!,@,#,$,%,^,&,*,(,),_,-,+,=,?,<,>,.,—,,,。,/,\\,|,《,》,?,;,:,:,',‘,;,“,"
defaultInvalidWorlds = " ,~,!,@,#,$,%,^,&,*,(,),_,-,+,=,?,<,>,.,—,,,。,/,\\,|,《,》,?,;,:,:,',‘,;,“,¥,·"
defaultReplaceStr = "****"
)

Expand Down Expand Up @@ -55,25 +55,40 @@ func (f *DFA) SetReplaceStr(str string) {
f.replaceStr = str
}

func (f *DFA) Check(txt string) ([]string, bool) {
_, found, b := f.check(txt, false)
return found, b
func (f *DFA) Check(txt string) ([]string, []string, bool) {
_, found, target, b := f.check(txt, false)
return found, target, b
}

func (f *DFA) CheckAndReplace(txt string) (string, []string, bool) {
func (f *DFA) CheckAndReplace(txt string) (string, []string, []string, bool) {
return f.check(txt, true)
}

func (f *DFA) check(txt string, replace bool) (string, []string, bool) {
func (f *DFA) FilterInvalidChar(txt ...string) []string {
res := make([]string, 0, len(txt))
for _, s := range txt {
str := []rune(s)
for i, c := range str {
if _, ok := f.invalidWords[string(c)]; ok {
str = append(str[:i], str[i+1:]...)
}
}
res = append(res, string(str))
}
return res
}

func (f *DFA) check(txt string, replace bool) (dist string, found []string, target []string, b bool) {
var (
str = []rune(txt)
ok bool
found []string
node *Node
nodeMap map[rune]*Node
start, tag = -1, -1
result string
tmp = ""
)
target = make([]string, 0, 0)
f.l.Lock()
defer f.l.Unlock()

Expand All @@ -89,10 +104,12 @@ func (f *DFA) check(txt string, replace bool) (string, []string, bool) {
if tag == 0 {
start = i
}

tmp = node.Value
if !node.IsEnd {
nodeMap = node.Child
} else {
target = append(target, tmp)
tmp = ""
found = append(found, string(str[start:i+1]))
if replace {
result = strings.Replace(result, string(str[start:i+1]), f.replaceStr, 1)
Expand All @@ -105,19 +122,18 @@ func (f *DFA) check(txt string, replace bool) (string, []string, bool) {
nodeMap = nil
}
} else {
if start != -1 {
i = start + 1
}

nodeMap = nil
start = -1
tag = -1
}
} else {
if node, ok = nodeMap[val]; ok {
tmp += node.Value
if !node.IsEnd {
nodeMap = node.Child
} else {
target = append(target, tmp)
tmp = ""
found = append(found, string(str[start:i+1]))
if replace {
result = strings.Replace(result, string(str[start:i+1]), f.replaceStr, 1)
Expand All @@ -132,5 +148,6 @@ func (f *DFA) check(txt string, replace bool) (string, []string, bool) {
}
}
}
return result, found, len(found) > 0
b = len(found) > 0
return
}
10 changes: 5 additions & 5 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type Node struct {
Child map[rune]*Node
}

func newNode() *Node {
func newNode(val string) *Node {
return &Node{
IsEnd: false,
Value: "",
Value: val,
Child: make(map[rune]*Node),
}
}
Expand All @@ -27,16 +27,16 @@ func (t *Trie) Insert(key string) {
curNode := t.root
for _, v := range key {
if curNode.Child[v] == nil {
curNode.Child[v] = newNode()
curNode.Child[v] = newNode(string(v))
}
curNode = curNode.Child[v]
curNode.Value = string(v)
}

if !curNode.IsEnd {
t.size++
curNode.IsEnd = true
}
curNode.Value = key
}

func (t *Trie) PrefixMatch(key string) []string {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (t *Trie) Child(key string) *Node {

func NewTrie() *Trie {
return &Trie{
root: newNode(),
root: newNode(""),
size: 0,
}
}

0 comments on commit 9d20e4a

Please sign in to comment.