https://github.com/ikawaha/dartsclone
Double Array TRIE liblary
https://github.com/ikawaha/dartsclone
double-array trie
Last synced: 10 months ago
JSON representation
Double Array TRIE liblary
- Host: GitHub
- URL: https://github.com/ikawaha/dartsclone
- Owner: ikawaha
- License: apache-2.0
- Created: 2018-11-04T15:41:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-25T02:01:01.000Z (almost 3 years ago)
- Last Synced: 2025-03-28T02:46:38.989Z (10 months ago)
- Topics: double-array, trie
- Language: Go
- Homepage:
- Size: 5.7 MB
- Stars: 8
- Watchers: 0
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# dartsclone : Double Array TRIE liblary
[](https://travis-ci.org/ikawaha/dartsclone)
[](https://ci.appveyor.com/project/ikawaha/dartsclone/branch/master)
[](https://coveralls.io/github/ikawaha/dartsclone)
[](https://godoc.org/github.com/ikawaha/dartsclone)
Port of [Sudachi's dartsclone library](https://github.com/WorksApplications/Sudachi/tree/develop/src/main/java/com/worksap/nlp/dartsclone) to Go.
## Build & Save
```Go:
package main
import (
"os"
"github.com/ikawaha/dartsclone"
)
func main() {
keys := []string{
"電気",
"電気通信",
"電気通信大学",
"電気通信大学大学院",
"電気通信大学大学院大学",
}
// Build
builder := dartsclone.NewBuilder(nil)
if err := builder.Build(keys, nil); err != nil {
panic(err)
}
// Save
f, err := os.Create("my-double-array-file")
if err != nil {
panic(err)
}
builder.WriteTo(f)
f.Close()
}
```
## Load & Search
```Go:
package main
import (
"fmt"
"github.com/ikawaha/dartsclone"
)
func main() {
trie, err := dartsclone.Open("my-double-array-file")
if err != nil {
panic(err)
}
ret, err := trie.CommonPrefixSearch("電気通信大学大学院大学", 0)
for i := 0; i < len(ret); i++ {
fmt.Printf("id=%d, common prefix=%s\n", ret[i][0], "電気通信大学大学院大学"[0:ret[i][1]])
}
}
```
### outputs
```
id=0, common prefix=電気
id=1, common prefix=電気通信
id=2, common prefix=電気通信大学
id=3, common prefix=電気通信大学大学院
id=4, common prefix=電気通信大学大学院大学
```
## Use memory mapping
* Build Tags : mmap
* Support OS : linux, osx, windows
```
$ go build -tags mmap ./...
```
```Go:
package main
import (
"fmt"
"github.com/ikawaha/dartsclone"
)
func main() {
trie, err := dartsclone.OpenMmaped("my-double-array-file") // ← ★
if err != nil {
panic(err)
}
defer trie.Close() // ← ★
ret, err := trie.CommonPrefixSearch("電気通信大学大学院大学", 0)
for i := 0; i < len(ret); i++ {
fmt.Printf("id=%d, common prefix=%s\n", ret[i][0], "電気通信大学大学院大学"[0:ret[i][1]])
}
}
```