1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-27 17:58:29 +08:00
icebergs/misc/alpha/alpha.go
2020-09-27 01:09:32 +08:00

96 lines
3.2 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/base/web"
"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, "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,
m.Cmd(web.CACHE, "catch", "csv", file+".csv").Append(kit.MDB_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{
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)))
}},
mdb.SEARCH: {Name: "search", Help: "搜索", Hand: func(m *ice.Message, arg ...string) {
_alpha_find(m.Spawn(), "word", arg[1]).Table(func(index int, value map[string]string, head []string) {
m.Push("file", "")
m.Push("line", arg[1])
m.Push("text", value["definition"]+"\n"+value["translation"])
})
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
_alpha_find(m, arg[0], arg[1])
}},
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Load()
m.Cmd(mdb.SEARCH, mdb.CREATE, ALPHA, ALPHA, c.Cap(ice.CTX_FOLLOW))
}},
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Save()
}},
},
}
func init() { wiki.Index.Register(Index, nil) }