1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 17:44:05 +08:00
icebergs/misc/alpha/alpha.go
2020-10-23 22:41:01 +08:00

82 lines
2.7 KiB
Go

package alpha
import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/core/wiki"
kit "github.com/shylinux/toolkits"
"os"
"path"
"strings"
)
func _alpha_find(m *ice.Message, method, word string) *ice.Message {
// 搜索方法
switch word = strings.TrimSpace(word); method {
case "line":
case "word":
word = "," + word + "$"
}
// 搜索词汇
msg := m.Cmd(cli.SYSTEM, "grep", "-rh", word, m.Conf(ALPHA, "meta.store"))
msg.CSV(msg.Result(), kit.Simple(m.Confv(ALPHA, "meta.field"))...).Table(func(index int, value map[string]string, head []string) {
if value["word"] == "" {
return
}
for _, k := range []string{"id", "word", "translation", "definition"} {
m.Push(k, value[k])
}
})
return m
}
func _alpha_load(m *ice.Message, file, name string) {
// 清空数据
meta := m.Confm(ALPHA, kit.MDB_META)
m.Assert(os.RemoveAll(path.Join(kit.Format(meta[kit.MDB_STORE]), name)))
m.Conf(ALPHA, name, "")
// 缓存配置
m.Conf(ALPHA, kit.Keys(name, kit.MDB_META), kit.Dict(
kit.MDB_STORE, meta[kit.MDB_STORE],
kit.MDB_FSIZE, meta[kit.MDB_FSIZE],
kit.MDB_LIMIT, meta[kit.MDB_LIMIT],
kit.MDB_LEAST, meta[kit.MDB_LEAST],
))
m.Cmd(mdb.IMPORT, ALPHA, name, kit.MDB_LIST, file)
// 保存词库
m.Conf(ALPHA, kit.Keys(name, "meta.limit"), 0)
m.Conf(ALPHA, kit.Keys(name, "meta.least"), 0)
m.Echo("%s: %d", name, m.Grow(ALPHA, name, kit.Dict("word", " ")))
}
const ALPHA = "alpha"
var Index = &ice.Context{Name: ALPHA, Help: "英汉词典",
Configs: map[string]*ice.Config{
ALPHA: {Name: ALPHA, Help: "英汉词典", Value: kit.Data(
kit.MDB_LIMIT, "50000", kit.MDB_LEAST, "1000",
kit.MDB_STORE, "usr/export/alpha", kit.MDB_FSIZE, "2000000",
kit.SSH_REPOS, "word-dict", kit.MDB_FIELD, []interface{}{"audio", "bnc", "collins", "definition", "detail", "exchange", "frq", "id", "oxford", "phonetic", "pos", "tag", "time", "translation", "word"},
)},
},
Commands: map[string]*ice.Command{
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Load() }},
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Save() }},
ALPHA: {Name: "alpha method=word,line word auto", Help: "英汉", Action: map[string]*ice.Action{
mdb.IMPORT: {Name: "import file=usr/word-dict/ecdict name", Help: "加载词库", Hand: func(m *ice.Message, arg ...string) {
_alpha_load(m, m.Option(kit.MDB_FILE), kit.Select(path.Base(m.Option(kit.MDB_FILE)), m.Option(kit.MDB_NAME)))
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
_alpha_find(m, arg[0], arg[1])
}},
},
}
func init() { wiki.Index.Register(Index, nil) }