公司算法同学通过字典树,用来查找语义模板。所谓的语义模板就是字符串。
e1:我要听{singer}的{song}
e2:我要看{movie}
e3:{city}的天气怎么样
```go
package main
type Trie struct {
value string
children map[string]*Trie
isWord bool
}
func Constructor() Trie {
return Trie{value:"", children:make(map[string]*Trie), isWord:false}
}
func (this *Trie)Insert(world string) {
root := this
for _,w := range world {
n := root.math(string(w))
if n == nil {
root.children[string(w)] = &Trie{value:string(w),children: map[string]*Trie{}}
root = root.children[string(w)]
}else {
root = n
}
}
root.isWord = true
}
func (this *Trie)Search(world string) bool {
if n := this.math(world);n!=nil&&n.isWord==true{
return true
}
return false
}
func (this *Trie)HasStartWith(world string) bool {
if this.math(world) != nil {
return true
}
return false
}
func (this *Trie)math(world string) *Trie {
root := this
for _,w := range world {
if _,ok := root.children[string(w)];ok{
root = root.children[string(w)]
}else {
return nil
}
}
return root
}
```
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)