1
0
mirror of https://github.com/fumiama/emozi.git synced 2026-06-23 12:40:46 +08:00

feat(coder): finish decode

This commit is contained in:
源文雨
2024-02-15 15:28:57 +09:00
parent f7cc2d15d0
commit 96223b1b74
10 changed files with 883 additions and 509 deletions

86
data.go
View File

@@ -2,7 +2,9 @@ package emozi
import (
_ "embed"
"errors"
"strconv"
"strings"
)
// 字数据库 数据来自 https://github.com/shuowenjiezi/shuowen
@@ -11,7 +13,7 @@ import (
var 字数据库 []byte
// DatabasePath 字数据库的路径 如找不到会向对应路径写入内嵌的字数据库
var EmoziDatabasePath = "字a.db"
var EmoziDatabasePath = "字.db"
const (
主字表名 = "emozi"
@@ -19,6 +21,10 @@ const (
部首表名 = "radcl"
)
var (
ErrNoSuchChar = errors.New("no such char")
)
// 字表 emozi表 定义
type 字表 struct {
ID int64 // ID 高 32 位 W 的 rune, 低 32 位 保留8 S8 Y8 T8
@@ -35,33 +41,105 @@ func 字表ID(w rune, s 声母枚举, y 韵母枚举, t 声调枚举) int64 {
return int64((uint64(w) << 32) | (uint64(s) << 16) | (uint64(y) << 8) | (uint64(t)))
}
// 逆字ID 同声母 韵母 声调 部首的字的集合
func 逆字ID(s 声母枚举, y 韵母枚举, t 声调枚举, r rune) int64 {
return int64((uint64(r) << 32) | (uint64(s) << 16) | (uint64(y) << 8) | (uint64(t)))
}
// 查字 返回 lst lstbuf error
func (c *Coder) 查字(ch rune, lstbuf []字表) ([]字表, []字表, error) {
c.mu.RLock()
lst, ok := c.字表缓存[ch]
c.mu.RUnlock()
if ok {
if len(lst) == 0 {
return nil, lstbuf, ErrNoSuchChar
}
return lst, lstbuf, nil
}
lstbuf = lstbuf[:0]
x := 字表{}
q := "WHERE W=" + strconv.Itoa(int(ch))
c.mu.Lock()
defer c.mu.Unlock()
err := c.db.FindFor(附字表名, &x, "WHERE W="+strconv.Itoa(int(ch)), func() error {
err := c.db.FindFor(附字表名, &x, q, func() error {
lstbuf = append(lstbuf, x)
return nil
})
if err != nil {
lstbuf = lstbuf[:0]
err = c.db.FindFor(主字表名, &x, "WHERE W="+strconv.Itoa(int(ch)), func() error {
err = c.db.FindFor(主字表名, &x, q, func() error {
lstbuf = append(lstbuf, x)
return nil
})
}
if err != nil {
c.字表缓存[ch] = nil
return nil, lstbuf, err
}
if len(lstbuf) == 0 {
c.字表缓存[ch] = nil
return nil, lstbuf, ErrNoSuchChar
}
lstsave := make([]字表, len(lstbuf))
copy(lstsave, lstbuf)
c.字表缓存[ch] = lstsave
return lstbuf, lstbuf, err
return lstbuf, lstbuf, nil
}
// 逆字 逆查匹配的字
func (c *Coder) 逆字(s 声母枚举, y 韵母枚举, t 声调枚举, r rune, lstbuf []字表) ([]rune, []字表, error) {
id := 逆字ID(s, y, t, r)
c.mu.RLock()
matches, ok := c.逆字表缓存[id]
c.mu.RUnlock()
if ok {
if len(matches) == 0 {
return nil, lstbuf, ErrNoSuchChar
}
return matches, lstbuf, nil
}
lstbuf = lstbuf[:0]
x := 字表{}
sb := strings.Builder{}
sb.WriteString("WHERE S=")
sb.WriteString(strconv.Itoa(int(s)))
sb.WriteString(" AND Y=")
sb.WriteString(strconv.Itoa(int(y)))
sb.WriteString(" AND T=")
sb.WriteString(strconv.Itoa(int(t)))
if r != 0 {
sb.WriteString(" AND R=")
sb.WriteString(strconv.Itoa(int(r)))
}
q := sb.String()
c.mu.Lock()
defer c.mu.Unlock()
err := c.db.FindFor(附字表名, &x, q, func() error {
lstbuf = append(lstbuf, x)
return nil
})
if err != nil {
lstbuf = lstbuf[:0]
err = c.db.FindFor(主字表名, &x, q, func() error {
lstbuf = append(lstbuf, x)
return nil
})
}
if err != nil {
c.逆字表缓存[id] = nil
return nil, lstbuf, err
}
if len(lstbuf) == 0 {
c.逆字表缓存[id] = nil
return nil, lstbuf, ErrNoSuchChar
}
rs := make([]rune, len(lstbuf))
for i, x := range lstbuf {
rs[i] = x.W
}
c.逆字表缓存[id] = rs
return rs, lstbuf, nil
}
// 从表 从部首表