golnag 字典树,字符串查找

xmge · · 736 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

公司算法同学通过字典树,用来查找语义模板。所谓的语义模板就是字符串。 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 } ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

736 次点击  
加入收藏 微博
1 回复  |  直到 2019-11-14 11:27:30
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传