Documentation
¶
Index ¶
- Variables
- type Dat
- func (d *Dat[T]) Analysis(sentence string) (keys []string, indexes []int)
- func (d *Dat[T]) Depth() int
- func (d *Dat[T]) DumpToFile(filePath string) error
- func (d *Dat[T]) GetKey(index int) string
- func (d *Dat[T]) HitKeys(prefix string) (hits []string)
- func (d *Dat[T]) Hollow() int
- func (d *Dat[T]) KeySize() int
- func (d *Dat[T]) MatchGet(word string) (value any, ok bool)
- func (d *Dat[T]) MatchIndex(word string) int
- func (d *Dat[T]) MatchPrefix(word string) bool
- func (d *Dat[T]) Matches(word string) bool
- func (d *Dat[T]) MatchesIndex(word string) intdeprecated
- func (d *Dat[T]) ObtainPrefixes(word string) (result []string)
- func (d *Dat[T]) Size() int
- func (d *Dat[T]) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // MinExpansiveFactor 适配 state 时,数组最小扩容系数。如果同层子节点 code 相差大,则该值该调大。 MinExpansiveFactor = 1.2 // InitArrayFactor 初始扩容系数。 InitArrayFactor = 2.5 )
Functions ¶
This section is empty.
Types ¶
type Dat ¶ added in v1.2.0
type Dat[T any] struct { // contains filtered or unexported fields }
func NewWithValues ¶ added in v1.3.0
NewWithValues 构建双数组树。不处理空串。可使用 Dat.MatchGet 根据 key 获取 value。不支持备份到文件。
func (*Dat[T]) Analysis ¶ added in v1.2.0
Analysis 分析 sentence,获取 sentence 中包含词库的词,和该词在 sentence 中的位置。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
d := dat.New([]string{"abc", "ab", "abg", "jkl"})
sentence := "abc ab"
hitKeys, indexes := d.Analysis(sentence)
for i, index := range indexes {
word := sentence[index : index+len(hitKeys[i])]
fmt.Println(word == hitKeys[i])
}
}
Output: true true
func (*Dat[T]) DumpToFile ¶ added in v1.2.0
DumpToFile 持久化检索树。
func (*Dat[T]) HitKeys ¶ added in v1.3.0
HitKeys 获取词库中前缀部分是 prefix 的词。包含 prefix 与词完全一样的情形。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
d := dat.New([]string{"abc", "def", "abdi", "jkl"})
fmt.Println(d.HitKeys("ab"))
}
Output: [abc abdi]
func (*Dat[T]) MatchGet ¶ added in v1.3.0
MatchGet 判断 word 是否在词库中,返回词对应的 value。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
keyToValue := map[string]any{
"abc": 1,
"afr": 2,
"kja": 3,
}
d := dat.NewWithValues(keyToValue)
fmt.Println(d.MatchGet("abc"))
fmt.Println(d.MatchGet("none"))
}
Output: 1 true <nil> false
func (*Dat[T]) MatchIndex ¶ added in v1.3.0
MatchIndex 检索 word 是否在词库中,并返回词位置,不存在返回 -1。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
d := dat.New([]string{"abc", "def", "ghi", "jkl"})
index := d.MatchIndex("abc")
fmt.Println(d.GetKey(index) == "abc")
}
Output: true
func (*Dat[T]) MatchPrefix ¶ added in v1.2.0
MatchPrefix 判断 word 是否是词库中任何一个词的前缀。包含 word 与词完全一样的情形。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
d := dat.New([]string{"abc", "def", "ghi", "jkl"})
fmt.Println(d.MatchPrefix("abc"))
fmt.Println(d.MatchPrefix("ab"))
fmt.Println(d.MatchPrefix("cab"))
}
Output: true true false
func (*Dat[T]) Matches ¶ added in v1.2.0
Matches 判断 word 是否存在于词库中。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
d := dat.New([]string{"abc", "def", "ghi", "jkl"})
fmt.Println(d.Matches("abc"))
fmt.Println(d.Matches("abcd"))
fmt.Println(d.Matches("ab"))
fmt.Println(d.Matches("jkl"))
}
Output: true false false true
func (*Dat[T]) MatchesIndex
deprecated
added in
v1.2.0
func (*Dat[T]) ObtainPrefixes ¶ added in v1.2.0
ObtainPrefixes 返回所有匹配 word 前缀的词。
Example ¶
package main
import (
"fmt"
dat "gitee.com/ivfzhou/double-array-trie"
)
func main() {
d := dat.New([]string{"abc", "ab", "abg", "jkl"})
fmt.Println(d.ObtainPrefixes("abc"))
}
Output: [ab abc]
Click to show internal directories.
Click to hide internal directories.