triesearch

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

README

triesearch

基于go+内存的轻量级搜索引擎

使用方法:

下载安装
go get -u github.com/BabyRunPlus/triesearch

第一步:初始化应用
s := triesearch.NewTrie()

第二步:设置索引深度 深度越小,搜索速度越快
s.SetDepth(5)

第三步:创建索引与搜索内容
  list := []string{
    "碳中和",
    "中国",
    "中华人民共和国"
  }

  #自动创建全索引搜索引擎
  s.AutoAddFull(list)

  #自动创建简单索引搜索引擎
  s.AutoAdd(list)

  #自定义关键词创建全索引搜索引擎
  for i := 0; i < len(list); i++ {
    s.AddFull("自定义关键词", list[i])
  }

  #自定义关键词创建简单索引搜索引擎
  for i := 0; i < len(list); i++ {
    s.Add("自定义关键词", list[i])
  }

  #数据结构创建结束后建议执行s.GC()用来清除临时数据

搜索:
r, c := s.Find("碳中和")
  #r 是否为完全匹配结果 关键词超出深度时返回false
  #s 搜索出来的内容列表

全索引与简单索引的区别
#搜索关键词“中”
  全索引结果:碳中和、中国、中华人民共和国
  简单索引结果:中国、中华人民共和国

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	//  Children
	/**
	 *  @Author: rym 2022-11-16 09:09:00
	 *  @Description: 该节点的子节点字典
	 */
	Children map[rune]*Node `json:"children"`

	//  ContentMap
	/**
	 *  @Author: rym 2023-03-09 16:00:51
	 *  @Description: 临时用的map,用来作数据去重
	 */
	ContentMap map[string]string

	//  Char
	/**
	 *  @Author: rym 2022-11-16 09:01:47
	 *  @Description: 该节点的字符
	 */
	Char string

	//  ContentList
	/**
	 *  @Author: rym 2022-11-16 09:10:54
	 *  @Description:
	 */
	Content []string `json:"content"`

	//  Code
	/**
	 *  @Author: rym 2022-11-16 09:08:38
	 *  @Description: 改节点的unicode
	 */
	Code rune
}

Node *

  • @Author: rym 2022-11-16 09:01:35
  • @Description: 数据结构节点对象

type Trie

type Trie struct {
	//  Root
	/**
	 *  @Author: rym 2022-11-16 09:14:41
	 *  @Description: 根节点
	 */
	Root *Node
	// contains filtered or unexported fields
}

Trie *

  • @Author: rym 2022-11-16 09:18:42
  • @Description:Trie Map 数据结构对象

func NewTrie

func NewTrie() *Trie

func (*Trie) Add

func (t *Trie) Add(keyword, content string)

Add *

  • @Author: rym 2022-11-16 17:39:14
  • @Description: 根据关键词添加节点,并下挂相关内容 对外方法
  • @Description: 创建普通的树节点 例如: 碳中和 通过 碳、碳中、碳中和 可以查找到对应的内容列表
  • @receiver t
  • @param keyword
  • @param content

func (*Trie) AddFull

func (t *Trie) AddFull(keyword, content string)

AddFull *

  • @Author: rym 2022-11-16 17:32:32
  • @Description: 根据关键词添加节点,并下挂相关内容 对外方法
  • @Description: 创建详细的树节点 例如:碳中和 通过 碳、碳中、碳中和、中、中和、和 都可以查找到对应的内容列表
  • @Description: 此方式创建的树,会更多消耗内存
  • @receiver t
  • @param keyword
  • @param content

func (*Trie) AutoAdd

func (t *Trie) AutoAdd(list []string)

AutoAdd *

  • @Author: rym 2023-01-31 17:26:29
  • @Description:自动添加非全词搜索
  • @receiver t
  • @param list

func (*Trie) AutoAddFull

func (t *Trie) AutoAddFull(list []string)

AutoAddFull *

  • @Author: rym 2023-01-31 17:24:56
  • @Description:自动添加全词搜索
  • @receiver t
  • @param list

func (*Trie) Find

func (t *Trie) Find(keyword string) (bool, []string)

Find *

  • @Author: rym 2022-11-16 09:44:22
  • @Description: 根据关键词,从树中找相关内容列表
  • @receiver t
  • @param keyword
  • @return bool
  • @return []string

func (*Trie) GC

func (t *Trie) GC()

GC *

  • @Author: rym 2023-03-09 16:11:00
  • @Description: 清除临时数据
  • @receiver t

func (*Trie) SetDepth

func (t *Trie) SetDepth(n int8)

SetDepth *

  • @Author: rym 2022-11-16 09:41:27
  • @Description: 设置树的最大深度 深度越大 查找速度越慢
  • @receiver t
  • @param n

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL