forked from x/icebergs
opt action
This commit is contained in:
parent
fd4486d3db
commit
47b38186b6
185
base/mdb/mdb.go
185
base/mdb/mdb.go
@ -2,6 +2,7 @@ package mdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/shylinux/icebergs"
|
"github.com/shylinux/icebergs"
|
||||||
|
"github.com/shylinux/icebergs/base/web"
|
||||||
"github.com/shylinux/toolkits"
|
"github.com/shylinux/toolkits"
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -9,10 +10,89 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func _list_import(m *ice.Message, prefix, key, file, text string) {
|
||||||
|
buf := bytes.NewBufferString(text)
|
||||||
|
r := csv.NewReader(buf)
|
||||||
|
|
||||||
|
if file != "" {
|
||||||
|
if f, e := os.Open(file); m.Assert(e) {
|
||||||
|
defer f.Close()
|
||||||
|
r = csv.NewReader(f)
|
||||||
|
// 导入文件
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
head, _ := r.Read()
|
||||||
|
for {
|
||||||
|
line, e := r.Read()
|
||||||
|
if e != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
data := kit.Dict()
|
||||||
|
for i, k := range head {
|
||||||
|
if k == kit.MDB_EXTRA {
|
||||||
|
data[k] = kit.UnMarshal(line[i])
|
||||||
|
} else {
|
||||||
|
data[k] = line[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导入数据
|
||||||
|
n := m.Grow(prefix, key, data)
|
||||||
|
m.Log_INSERT(kit.MDB_KEY, kit.Keys(prefix, key), kit.MDB_ID, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func _list_export(m *ice.Message, prefix, key, file string) {
|
||||||
|
f, p, e := kit.Create(path.Join("usr/export", kit.Keys(file, "csv")))
|
||||||
|
m.Assert(e)
|
||||||
|
defer f.Close()
|
||||||
|
defer m.Cmdy(web.STORY, "catch", "csv", p)
|
||||||
|
|
||||||
|
w := csv.NewWriter(f)
|
||||||
|
defer w.Flush()
|
||||||
|
|
||||||
|
head := []string{}
|
||||||
|
m.Grows(prefix, key, "", "", func(index int, value map[string]interface{}) {
|
||||||
|
if index == 0 {
|
||||||
|
// 输出表头
|
||||||
|
for k := range value {
|
||||||
|
head = append(head, k)
|
||||||
|
}
|
||||||
|
sort.Strings(head)
|
||||||
|
w.Write(head)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出数据
|
||||||
|
data := []string{}
|
||||||
|
for _, k := range head {
|
||||||
|
data = append(data, kit.Format(value[k]))
|
||||||
|
}
|
||||||
|
w.Write(data)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func _hash_import(m *ice.Message, prefix, key, file, text string) {
|
||||||
|
data := kit.Parse(nil, "", kit.Split(text, "\t: ,\n")...).(map[string]interface{})
|
||||||
|
for k, v := range data {
|
||||||
|
m.Log_MODIFY(kit.MDB_KEY, kit.Keys(prefix, key), "k", k, "v", v)
|
||||||
|
m.Conf(prefix, kit.Keys(key, k), v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func _hash_export(m *ice.Message, prefix, key, file string) {
|
||||||
|
f, p, e := kit.Create(path.Join("usr/export", kit.Keys(file, "json")))
|
||||||
|
m.Assert(e)
|
||||||
|
defer f.Close()
|
||||||
|
defer m.Cmdy(web.STORY, "catch", "json", p)
|
||||||
|
|
||||||
|
en := json.NewEncoder(f)
|
||||||
|
en.Encode(m.Confv(prefix, key))
|
||||||
|
}
|
||||||
|
|
||||||
func distance(lat1, long1, lat2, long2 float64) float64 {
|
func distance(lat1, long1, lat2, long2 float64) float64 {
|
||||||
lat1 = lat1 * math.Pi / 180
|
lat1 = lat1 * math.Pi / 180
|
||||||
long1 = long1 * math.Pi / 180
|
long1 = long1 * math.Pi / 180
|
||||||
@ -21,6 +101,9 @@ func distance(lat1, long1, lat2, long2 float64) float64 {
|
|||||||
return 2 * 6371 * math.Asin(math.Sqrt(math.Pow(math.Sin(math.Abs(lat1-lat2)/2), 2)+math.Cos(lat1)*math.Cos(lat2)*math.Pow(math.Sin(math.Abs(long1-long2)/2), 2)))
|
return 2 * 6371 * math.Asin(math.Sqrt(math.Pow(math.Sin(math.Abs(lat1-lat2)/2), 2)+math.Cos(lat1)*math.Cos(lat2)*math.Pow(math.Sin(math.Abs(long1-long2)/2), 2)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const IMPORT = "import"
|
||||||
|
const EXPORT = "export"
|
||||||
|
|
||||||
var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
||||||
Caches: map[string]*ice.Cache{},
|
Caches: map[string]*ice.Cache{},
|
||||||
Configs: map[string]*ice.Config{
|
Configs: map[string]*ice.Config{
|
||||||
@ -33,6 +116,23 @@ var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
|||||||
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
m.Save(m.Prefix("location"))
|
m.Save(m.Prefix("location"))
|
||||||
}},
|
}},
|
||||||
|
kit.MDB_IMPORT: {Name: "import conf key type file", Help: "导入数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
switch msg := m.Cmd(ice.WEB_STORY, "index", arg[3]); arg[2] {
|
||||||
|
case kit.MDB_LIST:
|
||||||
|
_list_import(m, arg[0], arg[1], msg.Append(kit.MDB_FILE), msg.Append(kit.MDB_TEXT))
|
||||||
|
case kit.MDB_HASH:
|
||||||
|
_hash_import(m, arg[0], arg[1], msg.Append(kit.MDB_FILE), msg.Append(kit.MDB_TEXT))
|
||||||
|
}
|
||||||
|
}},
|
||||||
|
kit.MDB_EXPORT: {Name: "export conf key type [name]", Help: "导出数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
switch file := kit.Select(kit.Select(arg[0], arg[0]+":"+arg[1], arg[1] != ""), arg, 3); arg[2] {
|
||||||
|
case kit.MDB_LIST:
|
||||||
|
_list_export(m, arg[0], arg[1], file)
|
||||||
|
case kit.MDB_HASH:
|
||||||
|
_hash_export(m, arg[0], arg[1], file)
|
||||||
|
}
|
||||||
|
}},
|
||||||
|
|
||||||
"location": {Name: "location", Help: "location", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
"location": {Name: "location", Help: "location", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
if len(arg) == 0 {
|
if len(arg) == 0 {
|
||||||
m.Grows("location", nil, "", "", func(index int, value map[string]interface{}) {
|
m.Grows("location", nil, "", "", func(index int, value map[string]interface{}) {
|
||||||
@ -97,89 +197,6 @@ var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
|||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
|
|
||||||
ice.MDB_IMPORT: {Name: "import conf key type file", Help: "导入数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
msg := m.Cmd(ice.WEB_STORY, "index", arg[3])
|
|
||||||
|
|
||||||
switch arg[2] {
|
|
||||||
case kit.MDB_DICT:
|
|
||||||
case kit.MDB_META:
|
|
||||||
case kit.MDB_LIST:
|
|
||||||
buf := bytes.NewBufferString(msg.Append("text"))
|
|
||||||
r := csv.NewReader(buf)
|
|
||||||
if msg.Append("file") != "" {
|
|
||||||
if f, e := os.Open(msg.Append("file")); m.Assert(e) {
|
|
||||||
// 导入文件
|
|
||||||
r = csv.NewReader(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
head, _ := r.Read()
|
|
||||||
|
|
||||||
for {
|
|
||||||
line, e := r.Read()
|
|
||||||
if e != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
data := kit.Dict()
|
|
||||||
for i, k := range head {
|
|
||||||
if k == kit.MDB_EXTRA {
|
|
||||||
data[k] = kit.UnMarshal(line[i])
|
|
||||||
} else {
|
|
||||||
data[k] = line[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 导入数据
|
|
||||||
n := m.Grow(arg[0], arg[1], data)
|
|
||||||
m.Log(ice.LOG_INSERT, "index: %d value: %v", n, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
case kit.MDB_HASH:
|
|
||||||
data := map[string]interface{}{}
|
|
||||||
m.Assert(json.Unmarshal([]byte(msg.Append("text")), &data))
|
|
||||||
for k, v := range data {
|
|
||||||
m.Conf(arg[0], kit.Keys(arg[1], k), v)
|
|
||||||
m.Log(ice.LOG_MODIFY, "%s: %s", k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}},
|
|
||||||
ice.MDB_EXPORT: {Name: "export conf key type name", Help: "导出数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
name := kit.Select(kit.Select(arg[0], arg[0]+":"+arg[1], arg[1] != ""), arg, 3)
|
|
||||||
|
|
||||||
switch arg[2] {
|
|
||||||
case kit.MDB_DICT:
|
|
||||||
case kit.MDB_META:
|
|
||||||
case kit.MDB_LIST:
|
|
||||||
f, p, e := kit.Create("var/temp/" + kit.Keys(name, "csv"))
|
|
||||||
m.Assert(e)
|
|
||||||
w := csv.NewWriter(f)
|
|
||||||
|
|
||||||
head := []string{}
|
|
||||||
m.Grows(arg[0], arg[1], "", "", func(index int, value map[string]interface{}) {
|
|
||||||
if index == 0 {
|
|
||||||
// 输出表头
|
|
||||||
for k := range value {
|
|
||||||
head = append(head, k)
|
|
||||||
}
|
|
||||||
sort.Strings(head)
|
|
||||||
w.Write(head)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 输出数据
|
|
||||||
data := []string{}
|
|
||||||
for _, k := range head {
|
|
||||||
data = append(data, kit.Format(value[k]))
|
|
||||||
}
|
|
||||||
w.Write(data)
|
|
||||||
})
|
|
||||||
w.Flush()
|
|
||||||
|
|
||||||
m.Cmdy(ice.WEB_STORY, "catch", "csv", p)
|
|
||||||
|
|
||||||
case kit.MDB_HASH:
|
|
||||||
m.Cmdy(ice.WEB_STORY, "add", "json", name, kit.Formats(m.Confv(arg[0], arg[1])))
|
|
||||||
}
|
|
||||||
}},
|
|
||||||
ice.MDB_DELETE: {Name: "delete conf key type", Help: "删除", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
ice.MDB_DELETE: {Name: "delete conf key type", Help: "删除", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
switch arg[2] {
|
switch arg[2] {
|
||||||
case kit.MDB_DICT:
|
case kit.MDB_DICT:
|
||||||
@ -194,4 +211,4 @@ var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { ice.Index.Register(Index, nil) }
|
func init() { ice.Index.Register(Index, nil, IMPORT, EXPORT) }
|
||||||
|
@ -58,28 +58,11 @@ func _action_share_show(m *ice.Message, river, storm, index string, arg ...strin
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func _action_action(m *ice.Message, action string, arg ...string) bool {
|
func _action_order_list(m *ice.Message, river, storm string, arg ...string) {
|
||||||
switch action {
|
for _, v := range arg {
|
||||||
case "upload":
|
|
||||||
msg := m.Cmd(ice.WEB_STORY, "upload")
|
|
||||||
m.Option("name", msg.Append("name"))
|
|
||||||
m.Option("data", msg.Append("data"))
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func _action_proxy(m *ice.Message) (proxy []string) {
|
|
||||||
if m.Option("pod") != "" {
|
|
||||||
proxy = append(proxy, ice.WEB_PROXY, m.Option("pod"))
|
|
||||||
m.Option("pod", "")
|
|
||||||
}
|
|
||||||
return proxy
|
|
||||||
}
|
|
||||||
func _action_order(m *ice.Message, river, storm string, arg ...string) {
|
|
||||||
for i, v := range arg {
|
|
||||||
m.Push("river", river)
|
m.Push("river", river)
|
||||||
m.Push("storm", storm)
|
m.Push("storm", storm)
|
||||||
m.Push("action", i)
|
m.Push("action", v)
|
||||||
|
|
||||||
m.Push("node", "")
|
m.Push("node", "")
|
||||||
m.Push("group", "")
|
m.Push("group", "")
|
||||||
@ -93,6 +76,23 @@ func _action_order(m *ice.Message, river, storm string, arg ...string) {
|
|||||||
m.Push("inputs", msg.Append("list"))
|
m.Push("inputs", msg.Append("list"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func _action_action(m *ice.Message, action string, arg ...string) bool {
|
||||||
|
switch action {
|
||||||
|
case "upload":
|
||||||
|
msg := m.Cmd(ice.WEB_STORY, "upload")
|
||||||
|
m.Option("name", msg.Append("name"))
|
||||||
|
m.Option("data", msg.Append("data"))
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func _action_proxy(m *ice.Message) (proxy []string) {
|
||||||
|
if m.Option("pod") != "" {
|
||||||
|
proxy = append(proxy, ice.WEB_PROXY, m.Option("pod"))
|
||||||
|
m.Option("pod", "")
|
||||||
|
}
|
||||||
|
return proxy
|
||||||
|
}
|
||||||
func _action_list(m *ice.Message, river, storm string) {
|
func _action_list(m *ice.Message, river, storm string) {
|
||||||
prefix := kit.Keys(kit.MDB_HASH, river, "tool", kit.MDB_HASH, storm)
|
prefix := kit.Keys(kit.MDB_HASH, river, "tool", kit.MDB_HASH, storm)
|
||||||
m.Grows(ice.CHAT_RIVER, prefix, "", "", func(index int, value map[string]interface{}) {
|
m.Grows(ice.CHAT_RIVER, prefix, "", "", func(index int, value map[string]interface{}) {
|
||||||
@ -158,7 +158,7 @@ func init() {
|
|||||||
switch arg[2] {
|
switch arg[2] {
|
||||||
case "index":
|
case "index":
|
||||||
// 前端列表
|
// 前端列表
|
||||||
_action_order(m, arg[0], arg[1], arg[3:]...)
|
_action_order_list(m, arg[0], arg[1], arg[3:]...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
meta.go
13
meta.go
@ -386,17 +386,20 @@ func (m *Message) Split(str string, field string, space string, enter string) *M
|
|||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
func (m *Message) CSV(text string) *Message {
|
func (m *Message) CSV(text string, head ...string) *Message {
|
||||||
bio := bytes.NewBufferString(text)
|
bio := bytes.NewBufferString(text)
|
||||||
r := csv.NewReader(bio)
|
r := csv.NewReader(bio)
|
||||||
heads, _ := r.Read()
|
|
||||||
|
if len(head) == 0 {
|
||||||
|
head, _ = r.Read()
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
lines, e := r.Read()
|
line, e := r.Read()
|
||||||
if e != nil {
|
if e != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
for i, k := range heads {
|
for i, k := range head {
|
||||||
m.Push(k, kit.Select("", lines, i))
|
m.Push(k, kit.Select("", line, i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
|
@ -2,134 +2,123 @@ package alpha
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/shylinux/icebergs"
|
"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"
|
"github.com/shylinux/icebergs/core/wiki"
|
||||||
"github.com/shylinux/toolkits"
|
"github.com/shylinux/toolkits"
|
||||||
|
"github.com/shylinux/toolkits/task"
|
||||||
|
|
||||||
"bytes"
|
"io/ioutil"
|
||||||
"encoding/csv"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Index = &ice.Context{Name: "alpha", Help: "英汉词典",
|
func _alpha_find(m *ice.Message, method, word string) {
|
||||||
Caches: map[string]*ice.Cache{},
|
|
||||||
Configs: map[string]*ice.Config{
|
|
||||||
"alpha": {Name: "alpha", Help: "英汉词典", Value: kit.Data(
|
|
||||||
"store", "var/data/alpha", "fsize", "200000", "limit", "5000", "least", "1000",
|
|
||||||
"repos", "word-dict", "local", "some",
|
|
||||||
"field", []interface{}{"audio", "bnc", "collins", "definition", "detail", "exchange", "frq", "id", "oxford", "phonetic", "pos", "tag", "time", "translation", "word"},
|
|
||||||
)},
|
|
||||||
},
|
|
||||||
Commands: map[string]*ice.Command{
|
|
||||||
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
m.Load()
|
|
||||||
}},
|
|
||||||
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
m.Save("alpha")
|
|
||||||
}},
|
|
||||||
"load": {Name: "load file [name]", Help: "加载词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
// 下载词库
|
|
||||||
if m.Cmd("web.code.git.repos", m.Conf("alpha", "meta.repos"), "usr/"+m.Conf("alpha", "meta.repos")); m.Confs("alpha", "ecdict") {
|
|
||||||
m.Echo("ecdict: %v", m.Conf("alpha", "ecdict.meta.count"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
arg = append(arg, path.Join("usr", m.Conf("alpha", "meta.repos"), "ecdict"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清空数据
|
|
||||||
lib := kit.Select(path.Base(arg[0]), arg, 1)
|
|
||||||
m.Assert(os.RemoveAll(path.Join(m.Conf("alpha", "meta.store"), lib)))
|
|
||||||
m.Conf("alpha", lib, "")
|
|
||||||
|
|
||||||
// 缓存配置
|
|
||||||
m.Conf("alpha", kit.Keys(lib, "meta.store"), path.Join(m.Conf("alpha", "meta.store"), lib))
|
|
||||||
m.Conf("alpha", kit.Keys(lib, "meta.fsize"), m.Conf("alpha", "meta.fsize"))
|
|
||||||
m.Conf("alpha", kit.Keys(lib, "meta.limit"), m.Conf("alpha", "meta.limit"))
|
|
||||||
m.Conf("alpha", kit.Keys(lib, "meta.least"), m.Conf("alpha", "meta.least"))
|
|
||||||
|
|
||||||
m.Cmd(ice.MDB_IMPORT, "alpha", lib, "list",
|
|
||||||
m.Cmd(ice.WEB_CACHE, "catch", "csv", arg[0]+".csv").Append("data"))
|
|
||||||
|
|
||||||
// 保存词库
|
|
||||||
m.Conf("alpha", kit.Keys(lib, "meta.limit"), 0)
|
|
||||||
m.Conf("alpha", kit.Keys(lib, "meta.least"), 0)
|
|
||||||
m.Echo("%s: %d", lib, m.Grow("alpha", lib, kit.Dict("word", " ")))
|
|
||||||
}},
|
|
||||||
"push": {Name: "push lib word text", Help: "添加词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
m.Conf("alpha", kit.Keys(arg[0], "meta.limit"), 0)
|
|
||||||
m.Conf("alpha", kit.Keys(arg[0], "meta.least"), 0)
|
|
||||||
m.Echo("%s: %d", arg[0], m.Grow("alpha", arg[0], kit.Dict("word", arg[1], "translation", arg[2])))
|
|
||||||
}},
|
|
||||||
"list": {Name: "list [lib [offend [limit]]]", Help: "查看词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
kit.Fetch(m.Confv("alpha"), func(key string, value map[string]interface{}) {
|
|
||||||
if key != "meta" {
|
|
||||||
m.Push(key, value["meta"], []string{"key", "count"})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
lib := kit.Select("ecdict", arg, 0)
|
|
||||||
m.Option("cache.offend", kit.Select("0", arg, 1))
|
|
||||||
m.Option("cache.limit", kit.Select("10", arg, 2))
|
|
||||||
m.Grows("alpha", lib, "", "", func(index int, value map[string]interface{}) {
|
|
||||||
m.Push("", value, []string{"id", "word", "translation"})
|
|
||||||
})
|
|
||||||
}},
|
|
||||||
"save": {Name: "save lib [filename]", Help: "导出词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
m.Option("cache.offend", 0)
|
|
||||||
m.Option("cache.limit", -2)
|
|
||||||
m.Cmdy(ice.WEB_STORY, "watch", m.Cmdx(ice.MDB_EXPORT, "alpha", arg[0], "list"), arg[1:])
|
|
||||||
}},
|
|
||||||
|
|
||||||
"random": {Name: "random [count]", Help: "随机词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
count := kit.Int(m.Conf("alpha", "ecdict.meta.count")) + 1
|
|
||||||
for i := 0; i < kit.Int(kit.Select("10", arg, 0)); i++ {
|
|
||||||
m.Cmdy("list", "ecdict", count-rand.Intn(count), 1)
|
|
||||||
}
|
|
||||||
}},
|
|
||||||
"trans": {Name: "trans word=hi method auto", Help: "查找词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
// 收藏列表
|
|
||||||
m.Cmdy(ice.WEB_FAVOR, "alpha.word")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 搜索方法
|
// 搜索方法
|
||||||
method := kit.Select("word", arg, 1)
|
switch word = strings.TrimSpace(word); method {
|
||||||
switch arg[0] = strings.TrimSpace(arg[0]); method {
|
case LINE:
|
||||||
case "line":
|
case WORD:
|
||||||
case "word":
|
word = "," + word + "$"
|
||||||
arg[0] = "," + arg[0] + "$"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 搜索词汇
|
// 搜索词汇
|
||||||
field := map[string]int{}
|
msg := m.Cmd(cli.SYSTEM, "grep", "-rh", word, m.Conf(ALPHA, "meta.store"))
|
||||||
for i, k := range kit.Simple(m.Confv("alpha", "meta.field")) {
|
msg.CSV(msg.Result(), kit.Simple(m.Confv(ALPHA, "meta.field"))...).Table(func(index int, line map[string]string, head []string) {
|
||||||
field[k] = i
|
if method == WORD && index == 0 {
|
||||||
}
|
|
||||||
bio := csv.NewReader(bytes.NewBufferString(m.Cmdx(ice.CLI_SYSTEM, "grep", "-rh", arg[0], m.Conf("alpha", "meta.store"))))
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
if line, e := bio.Read(); e != nil {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
if method == "word" && i == 0 {
|
|
||||||
// 添加收藏
|
// 添加收藏
|
||||||
m.Cmd(ice.WEB_FAVOR, "alpha.word", "alpha",
|
m.Cmd(web.FAVOR, m.Conf(ALPHA, "meta.favor"), ALPHA, line["word"], line["translation"],
|
||||||
line[kit.Int(field["word"])], line[kit.Int(field["translation"])],
|
"id", line["id"], "definition", line["definition"])
|
||||||
"id", line[kit.Int(field["id"])], "definition", line[kit.Int(field["definition"])],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
for _, k := range []string{"id", "word", "translation", "definition"} {
|
for _, k := range []string{"id", "word", "translation", "definition"} {
|
||||||
// 输出词汇
|
// 输出词汇
|
||||||
m.Push(k, line[kit.Int(field[k])])
|
m.Push(k, line[k])
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func _alpha_find2(m *ice.Message, method, word string) {
|
||||||
|
p := path.Join(m.Conf(ALPHA, "meta.store"), ALPHA)
|
||||||
|
if ls, e := ioutil.ReadDir(p); m.Assert(e) {
|
||||||
|
args := []interface{}{}
|
||||||
|
for _, v := range ls {
|
||||||
|
args = append(args, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mu sync.Mutex
|
||||||
|
task.Sync(args, func(task *task.Task) error {
|
||||||
|
info := task.Arg.(os.FileInfo)
|
||||||
|
file := path.Join(p, info.Name())
|
||||||
|
kit.CSV(file, 100000, func(index int, value map[string]string, head []string) {
|
||||||
|
if value["word"] != word {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
m.Push("word", value["word"])
|
||||||
|
m.Push("translation", value["translation"])
|
||||||
|
m.Push("definition", value["definition"])
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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_DATA))
|
||||||
|
|
||||||
|
// 保存词库
|
||||||
|
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"
|
||||||
|
const (
|
||||||
|
WORD = "word"
|
||||||
|
LINE = "line"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Index = &ice.Context{Name: "alpha", Help: "英汉词典",
|
||||||
|
Configs: map[string]*ice.Config{
|
||||||
|
ALPHA: {Name: "alpha", Help: "英汉词典", Value: kit.Data(
|
||||||
|
kit.MDB_STORE, "usr/export", kit.MDB_FSIZE, "2000000",
|
||||||
|
kit.MDB_LIMIT, "50000", kit.MDB_LEAST, "1000",
|
||||||
|
"repos", "word-dict", "local", "person",
|
||||||
|
"field", []interface{}{"audio", "bnc", "collins", "definition", "detail", "exchange", "frq", "id", "oxford", "phonetic", "pos", "tag", "time", "translation", "word"},
|
||||||
|
web.FAVOR, "alpha.word",
|
||||||
|
)},
|
||||||
|
},
|
||||||
|
Commands: map[string]*ice.Command{
|
||||||
|
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Load() }},
|
||||||
|
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Save(ALPHA) }},
|
||||||
|
|
||||||
|
"find": {Name: "find word=hi method auto", Help: "查找词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
_alpha_find(m, kit.Select("word", arg, 1), arg[0])
|
||||||
|
}},
|
||||||
|
"find2": {Name: "find word=hi method auto", Help: "查找词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
_alpha_find2(m, kit.Select("word", arg, 1), arg[0])
|
||||||
|
}},
|
||||||
|
"load": {Name: "load file [name]", Help: "加载词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
if meta := m.Confm(ALPHA, "meta"); len(arg) == 0 {
|
||||||
|
arg = append(arg, path.Join("usr", kit.Format(meta["repos"]), "ecdict"))
|
||||||
|
}
|
||||||
|
_alpha_load(m, arg[0], kit.Select(path.Base(arg[0]), arg, 1))
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"github.com/shylinux/icebergs/base/web"
|
"github.com/shylinux/icebergs/base/web"
|
||||||
"github.com/shylinux/icebergs/core/code"
|
"github.com/shylinux/icebergs/core/code"
|
||||||
"github.com/shylinux/toolkits"
|
"github.com/shylinux/toolkits"
|
||||||
"github.com/shylinux/toolkits/conf"
|
|
||||||
"github.com/shylinux/toolkits/task"
|
"github.com/shylinux/toolkits/task"
|
||||||
|
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -15,20 +14,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
"sync"
|
||||||
|
|
||||||
const (
|
|
||||||
INPUT = "input"
|
|
||||||
)
|
|
||||||
const (
|
|
||||||
LINE = "line"
|
|
||||||
WORD = "word"
|
|
||||||
)
|
|
||||||
const (
|
|
||||||
FILE = "file"
|
|
||||||
CODE = "code"
|
|
||||||
TEXT = "text"
|
|
||||||
WEIGHT = "weight"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func _input_list(m *ice.Message, lib string) {
|
func _input_list(m *ice.Message, lib string) {
|
||||||
@ -89,8 +75,8 @@ func _input_find(m *ice.Message, method, word, limit string) {
|
|||||||
m.Sort(WEIGHT, "int_r")
|
m.Sort(WEIGHT, "int_r")
|
||||||
}
|
}
|
||||||
func _input_find2(m *ice.Message, method, word, limit string) {
|
func _input_find2(m *ice.Message, method, word, limit string) {
|
||||||
files := map[string]bool{}
|
|
||||||
list := []interface{}{}
|
list := []interface{}{}
|
||||||
|
files := map[string]bool{}
|
||||||
m.Richs(INPUT, "", kit.MDB_FOREACH, func(key string, value map[string]interface{}) {
|
m.Richs(INPUT, "", kit.MDB_FOREACH, func(key string, value map[string]interface{}) {
|
||||||
kit.Fetch(kit.Value(value, "meta.record"), func(index int, value map[string]interface{}) {
|
kit.Fetch(kit.Value(value, "meta.record"), func(index int, value map[string]interface{}) {
|
||||||
file := value["file"].(string)
|
file := value["file"].(string)
|
||||||
@ -103,35 +89,22 @@ func _input_find2(m *ice.Message, method, word, limit string) {
|
|||||||
})
|
})
|
||||||
defer m.Cost("some")
|
defer m.Cost("some")
|
||||||
|
|
||||||
p.Sync(list, func(task *task.Task) error {
|
var mu sync.Mutex
|
||||||
file := task.Arg.(string)
|
task.Sync(list, func(task *task.Task) error {
|
||||||
f, e := os.Open(file)
|
kit.CSV(kit.Format(task.Arg), 100000, func(index int, value map[string]string, head []string) {
|
||||||
if e != nil {
|
if value["code"] != word {
|
||||||
return e
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
r := csv.NewReader(f)
|
m.Push(FILE, task.Arg)
|
||||||
head, e := r.Read()
|
m.Push(kit.MDB_ID, value[kit.MDB_ID])
|
||||||
if e != nil {
|
m.Push(CODE, value["code"])
|
||||||
return e
|
m.Push(TEXT, value["text"])
|
||||||
}
|
m.Push(WEIGHT, value["weight"])
|
||||||
for {
|
m.Push(kit.MDB_TIME, value["time"])
|
||||||
line, e := r.Read()
|
})
|
||||||
if e != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if head[0] == "code" {
|
|
||||||
if line[0] == word {
|
|
||||||
m.Push(FILE, file)
|
|
||||||
m.Push(kit.MDB_ID, line[1])
|
|
||||||
m.Push(CODE, line[0])
|
|
||||||
m.Push(TEXT, line[2])
|
|
||||||
m.Push(kit.MDB_TIME, line[3])
|
|
||||||
m.Push(WEIGHT, line[4])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -192,7 +165,17 @@ func _input_load(m *ice.Message, file string, libs ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = task.New(nil, 10)
|
const INPUT = "input"
|
||||||
|
const (
|
||||||
|
WORD = "word"
|
||||||
|
LINE = "line"
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
FILE = "file"
|
||||||
|
CODE = "code"
|
||||||
|
TEXT = "text"
|
||||||
|
WEIGHT = "weight"
|
||||||
|
)
|
||||||
|
|
||||||
var Index = &ice.Context{Name: "input", Help: "输入法",
|
var Index = &ice.Context{Name: "input", Help: "输入法",
|
||||||
Configs: map[string]*ice.Config{
|
Configs: map[string]*ice.Config{
|
||||||
@ -221,10 +204,6 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
|
|||||||
_input_find(m, kit.Select(WORD, arg, 1), arg[0], kit.Select("100", arg, 2))
|
_input_find(m, kit.Select(WORD, arg, 1), arg[0], kit.Select("100", arg, 2))
|
||||||
}},
|
}},
|
||||||
"find2": {Name: "find2 key [word|line [limit]]", Help: "查找字码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
"find2": {Name: "find2 key [word|line [limit]]", Help: "查找字码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
if len(arg) == 0 {
|
|
||||||
web.FavorList(m, "input.word", "")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_input_find2(m, kit.Select(WORD, arg, 1), arg[0], kit.Select("100", arg, 2))
|
_input_find2(m, kit.Select(WORD, arg, 1), arg[0], kit.Select("100", arg, 2))
|
||||||
}},
|
}},
|
||||||
|
|
||||||
@ -234,16 +213,6 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
|
|||||||
"load": {Name: "load file lib", Help: "导入词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
"load": {Name: "load file lib", Help: "导入词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
_input_load(m, kit.Select("usr/wubi-dict/wubi86", arg, 0))
|
_input_load(m, kit.Select("usr/wubi-dict/wubi86", arg, 0))
|
||||||
}},
|
}},
|
||||||
|
|
||||||
"compare": {Name: "demo list nconn nreq", Help: "导入词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
m.Cmdy("web.code.bench", "http://localhost:9020/code/bench?cmd=web.code.input.find&cmd=shwq")
|
|
||||||
m.Cmdy("web.code.bench", "http://localhost:9020/code/bench?cmd=web.code.input.find2&cmd=shwq")
|
|
||||||
}},
|
|
||||||
"demo": {Name: "demo list nconn nreq", Help: "导入词库", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
conf, e := conf.Open(kit.Select("hi.shy", arg, 0))
|
|
||||||
m.Assert(e)
|
|
||||||
m.Echo(conf.Get("he"))
|
|
||||||
}},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
name.go
1
name.go
@ -10,7 +10,6 @@ var ErrNameExists = errors.New("name already exists")
|
|||||||
|
|
||||||
func Name(name string, value interface{}) string {
|
func Name(name string, value interface{}) string {
|
||||||
if _, ok := names[name]; ok {
|
if _, ok := names[name]; ok {
|
||||||
println(name)
|
|
||||||
panic(ErrNameExists)
|
panic(ErrNameExists)
|
||||||
}
|
}
|
||||||
names[name] = value
|
names[name] = value
|
||||||
|
17
type.go
17
type.go
@ -78,6 +78,9 @@ func (c *Context) Cap(key string, arg ...interface{}) string {
|
|||||||
return c.Caches[key].Value
|
return c.Caches[key].Value
|
||||||
}
|
}
|
||||||
func (c *Context) Run(m *Message, cmd *Command, key string, arg ...string) *Message {
|
func (c *Context) Run(m *Message, cmd *Command, key string, arg ...string) *Message {
|
||||||
|
m.meta[MSG_DETAIL] = kit.Simple(key, arg)
|
||||||
|
m.Hand = true
|
||||||
|
|
||||||
action, args := m.Option("_action"), arg
|
action, args := m.Option("_action"), arg
|
||||||
if len(arg) > 0 && arg[0] == "action" {
|
if len(arg) > 0 && arg[0] == "action" {
|
||||||
action, args = arg[1], arg[2:]
|
action, args = arg[1], arg[2:]
|
||||||
@ -594,14 +597,14 @@ func (m *Message) Cmd(arg ...interface{}) *Message {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Search(list[0], func(p *Context, c *Context, key string, cmd *Command) {
|
if ctx, ok := names[list[0]].(*Context); ok {
|
||||||
m.TryCatch(m.Spawns(c), true, func(msg *Message) {
|
m.Hand = true
|
||||||
m.Hand, msg.Hand = true, true
|
return ctx.Run(m.Spawns(ctx), ctx.Commands[list[0]], list[0], list[1:]...)
|
||||||
msg.meta[MSG_DETAIL] = list
|
}
|
||||||
|
|
||||||
p.Run(msg, cmd, key, list[1:]...)
|
m.Search(list[0], func(p *Context, c *Context, key string, cmd *Command) {
|
||||||
m.Hand, msg.Hand, m = true, true, msg
|
m = p.Run(m.Spawns(c), cmd, key, list[1:]...)
|
||||||
})
|
m.Hand = true
|
||||||
})
|
})
|
||||||
|
|
||||||
if m.Warn(m.Hand == false, "not found %v", list) {
|
if m.Warn(m.Hand == false, "not found %v", list) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user