{"id":21488611,"url":"https://github.com/yihleego/trie","last_synced_at":"2025-07-15T16:30:58.717Z","repository":{"id":37679362,"uuid":"483975099","full_name":"yihleego/trie","owner":"yihleego","description":"📒 An Aho-Corasick algorithm based string-searching utility for Go. It supports tokenization, ignoring case, replacing text. So you can use it to find keywords in an article, filter sensitive words, etc. ","archived":false,"fork":false,"pushed_at":"2022-09-14T12:13:49.000Z","size":50,"stargazers_count":48,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-14T15:53:26.218Z","etag":null,"topics":["aho-corasick","go","java","keywords","sensitive","stopwords","string-searching"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yihleego.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-21T08:48:22.000Z","updated_at":"2025-06-26T09:05:51.000Z","dependencies_parsed_at":"2022-09-26T21:01:18.617Z","dependency_job_id":null,"html_url":"https://github.com/yihleego/trie","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yihleego/trie","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yihleego%2Ftrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yihleego%2Ftrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yihleego%2Ftrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yihleego%2Ftrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yihleego","download_url":"https://codeload.github.com/yihleego/trie/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yihleego%2Ftrie/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265445302,"owners_count":23766445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aho-corasick","go","java","keywords","sensitive","stopwords","string-searching"],"created_at":"2024-11-23T14:10:28.471Z","updated_at":"2025-07-15T16:30:58.475Z","avatar_url":"https://github.com/yihleego.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trie\n\n[![GoDoc](https://godoc.org/github.com/yihleego/trie?status.svg)](https://godoc.org/github.com/yihleego/trie)\n[![Go Report Card](https://goreportcard.com/badge/github.com/yihleego/trie)](https://goreportcard.com/report/github.com/yihleego/trie)\n\nAn Aho-Corasick algorithm based string-searching utility for Go. It supports tokenization, ignoring case, replacing text. So you can use it to find keywords in an article, filter sensitive words, etc.\n\nImplementation in Java：[Trie4j](https://github.com/yihleego/trie4j)\n\n## Introduction\n\n判断一个字符串是否包含另一个字符串，我们通常使用`strings.Index()`或`strings.Contains()`进行判断，其底层实现基于RK、KMP、BM和Sunday等算法。如果要判断一个字符串是否包含多个字符串，比如在一篇文章找几个敏感词，继续使用上述的字符串搜索算法显然是不合适，这种场景就需要用到多模式匹配算法。\n\n[Aho–Corasick](http://cr.yp.to/bib/1975/aho.pdf) 算法是由贝尔实验室的 Alfred V. Aho 和 Margaret J. Corasick 在 1975 年发明的一种字符串搜索算法。它是一种字典匹配算法，可在输入文本中定位有限字符串集（字典）的元素。它同时匹配所有字符串。该算法的复杂性与字符串长度加上搜索文本的长度加上输出匹配的数量成线性关系。\n\n该算法主要依靠构造一个有限状态机来实现，然后通过失配指针在查找字符串失败时进行回退，转向某前缀的其他分支，免于重复匹配前缀，提高算法效率。\n\n## Usage\n\n### FindAll\n\n```go\nt := trie.New(\"雨疏\", \"风骤\", \"残酒\", \"卷帘人\", \"知否\")\nemits := t.FindAll(\"昨夜雨疏风骤，浓睡不消残酒。试问卷帘人，却道海棠依旧。知否，知否？应是绿肥红瘦。\", false)\n```\n\n```text\n[2:4=雨疏, 4:6=风骤, 11:13=残酒, 16:19=卷帘人, 27:29=知否, 30:32=知否]\n```\n\n### FindFirst\n\n```go\nt := trie.New(\"雨疏\", \"风骤\", \"残酒\", \"卷帘人\", \"知否\")\nemit := t.FindFirst(\"昨夜雨疏风骤，浓睡不消残酒。试问卷帘人，却道海棠依旧。知否，知否？应是绿肥红瘦。\", false)\n```\n\n```text\n2:4=雨疏\n```\n\n### FindAll (Case Insensitive)\n\n```go\nt := trie.New(\"poetry\", \"TRANSLATION\")\nemits := t.FindAll(\"Poetry is what gets lost in translation.\", true)\n```\n\n```text\n[0:6=poetry, 28:39=TRANSLATION]\n```\n\n### FindFirst (Case Insensitive)\n\n```go\nt := trie.New(\"poetry\", \"TRANSLATION\")\nemit := t.FindFirst(\"Poetry is what gets lost in translation.\", true)\n```\n\n```text\n0:6=poetry\n```\n\n### Tokenize\n\n```go\ns := \"常记溪亭日暮，沉醉不知归路。兴尽晚回舟，误入藕花深处。争渡，争渡，惊起一滩鸥鹭。\"\nt := trie.New(\"溪亭\", \"归路\", \"藕花\", \"争渡\")\nemits := t.FindAll(s, false)\ntokens := trie.Tokenize(emits, s)\n```\n\n```text\n[\"常记\", \"溪亭(2:4=溪亭)\", \"日暮，沉醉不知\", \"归路(11:13=归路)\", \"。兴尽晚回舟，误入\", \"藕花(22:24=藕花)\", \"深处。\", \"争渡(27:29=争渡)\", \"，\", \"争渡(30:32=争渡)\", \"，惊起一滩鸥鹭。\"]\n```\n\n### Replace\n\n```go\ns := \"我正在参加砍价，砍到0元就可以免费拿啦。亲~帮我砍一刀呗，咱们一起免费领好货。\"\nt := trie.New(\"0元\", \"砍一刀\", \"免费拿\", \"免费领\")\nemits := t.FindAll(s, false)\nr1 := trie.Replace(emits, s, \"*\")\nr2 := trie.Replace(emits, s, \"@#$%^\u0026*\")\n```\n\n```text\n我正在参加砍价，砍到**就可以***啦。亲~帮我***呗，咱们一起***好货。\n我正在参加砍价，砍到%^就可以#$%啦。亲~帮我%^\u0026呗，咱们一起\u0026*@好货。\n```\n\n## License\n\nThis project is under the MIT license. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyihleego%2Ftrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyihleego%2Ftrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyihleego%2Ftrie/lists"}