forked from x/icebergs
opt yac
This commit is contained in:
parent
4e1cd604f8
commit
09a83f74fa
@ -344,8 +344,8 @@ func init() {
|
|||||||
},
|
},
|
||||||
Commands: map[string]*ice.Command{
|
Commands: map[string]*ice.Command{
|
||||||
MATRIX: {Name: "matrix hash npage text auto", Help: "魔方矩阵", Action: map[string]*ice.Action{
|
MATRIX: {Name: "matrix hash npage text auto", Help: "魔方矩阵", Action: map[string]*ice.Action{
|
||||||
mdb.CREATE: {Name: "create nlang=32 ncell=256", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
|
mdb.CREATE: {Name: "create nlang=32 ncell=128", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
|
||||||
mat := NewMatrix(m, kit.Int(kit.Select("32", m.Option(NLANG))), kit.Int(kit.Select("256", m.Option(NCELL))))
|
mat := NewMatrix(m, kit.Int(kit.Select("32", m.Option(NLANG))), kit.Int(kit.Select("128", m.Option(NCELL))))
|
||||||
h := m.Rich(m.Prefix(MATRIX), "", kit.Data(kit.MDB_TIME, m.Time(), MATRIX, mat, NLANG, mat.nlang, NCELL, mat.ncell))
|
h := m.Rich(m.Prefix(MATRIX), "", kit.Data(kit.MDB_TIME, m.Time(), MATRIX, mat, NLANG, mat.nlang, NCELL, mat.ncell))
|
||||||
switch cb := m.Optionv(kit.Keycb(MATRIX)).(type) {
|
switch cb := m.Optionv(kit.Keycb(MATRIX)).(type) {
|
||||||
case func(string, *Matrix):
|
case func(string, *Matrix):
|
||||||
@ -376,7 +376,7 @@ func init() {
|
|||||||
mat, _ := value[MATRIX].(*Matrix)
|
mat, _ := value[MATRIX].(*Matrix)
|
||||||
|
|
||||||
hash, word, rest := mat.Parse(m, m.Option(NPAGE), []byte(m.Option(kit.MDB_TEXT)))
|
hash, word, rest := mat.Parse(m, m.Option(NPAGE), []byte(m.Option(kit.MDB_TEXT)))
|
||||||
m.Push(NHASH, mat.word[hash])
|
m.Push(NHASH, kit.Select(kit.Format("%d", hash), mat.word[hash]))
|
||||||
m.Push("word", string(word))
|
m.Push("word", string(word))
|
||||||
m.Push("rest", string(rest))
|
m.Push("rest", string(rest))
|
||||||
})
|
})
|
||||||
|
@ -45,7 +45,7 @@ func NewMatrix(m *ice.Message, nlang, ncell int) *Matrix {
|
|||||||
mat.word = map[int]string{}
|
mat.word = map[int]string{}
|
||||||
|
|
||||||
m.Option("matrix.cb", func(key string, lex *lex.Matrix) { mat.lex, mat.lex_key = lex, key })
|
m.Option("matrix.cb", func(key string, lex *lex.Matrix) { mat.lex, mat.lex_key = lex, key })
|
||||||
key := m.Cmdx("lex.matrix", mdb.CREATE, 32, 256)
|
key := m.Cmdx("lex.matrix", mdb.CREATE, 32)
|
||||||
m.Cmd("lex.matrix", mdb.INSERT, key, "space", "space", "[\t \n]")
|
m.Cmd("lex.matrix", mdb.INSERT, key, "space", "space", "[\t \n]")
|
||||||
|
|
||||||
mat.mat = make([][]*State, nlang)
|
mat.mat = make([][]*State, nlang)
|
||||||
@ -87,7 +87,7 @@ func (mat *Matrix) isVoid(page int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
func (mat *Matrix) train(m *ice.Message, page, hash int, word []string, level int) (int, []*Point, []*Point) {
|
func (mat *Matrix) train(m *ice.Message, page, hash int, word []string, level int) (int, []*Point, []*Point) {
|
||||||
m.Debug("%s %s\\%d page: %v hash: %v word: %v", TRAIN, strings.Repeat("#", level), level, page, hash, word)
|
m.Debug("%s %s\\%d page: %v hash: %v word: %v", TRAIN, strings.Repeat("#", level), level, page, hash, word)
|
||||||
@ -175,14 +175,11 @@ func (mat *Matrix) train(m *ice.Message, page, hash int, word []string, level in
|
|||||||
}
|
}
|
||||||
|
|
||||||
trans := map[int]int{page: page}
|
trans := map[int]int{page: page}
|
||||||
loop:
|
|
||||||
for i := mat.nlang; i < len(mat.mat); i++ { // 去空
|
for i := mat.nlang; i < len(mat.mat); i++ { // 去空
|
||||||
m.Debug("what %v %v", len(mat.mat[i]), mat.mat[i])
|
m.Debug("what %v %v", len(mat.mat[i]), mat.mat[i])
|
||||||
for j := 1; j < len(mat.mat[i]); j++ {
|
if !mat.isVoid(i) {
|
||||||
if mat.mat[i][j] != nil {
|
trans[i] = i
|
||||||
trans[i] = i
|
continue
|
||||||
continue loop
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for j := i + 1; j < len(mat.mat); j++ {
|
for j := i + 1; j < len(mat.mat); j++ {
|
||||||
@ -215,7 +212,7 @@ loop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mat *Matrix) Parse(m *ice.Message, rewrite Rewrite, page int, line []byte, level int) (hash int, word []string, rest []byte) {
|
func (mat *Matrix) Parse(m *ice.Message, rewrite Rewrite, page int, line []byte, level int) (hash int, word []string, rest []byte) {
|
||||||
// m.Debug("%s %s\\%d %s(%d): %s", PARSE, strings.Repeat("#", level), level, mat.name(page), page, string(line))
|
m.Debug("%s %s\\%d %s(%d): %s", PARSE, strings.Repeat("#", level), level, mat.name(page), page, string(line))
|
||||||
|
|
||||||
rest = line
|
rest = line
|
||||||
h, w, r := 0, []byte{}, []byte{}
|
h, w, r := 0, []byte{}, []byte{}
|
||||||
@ -225,7 +222,10 @@ func (mat *Matrix) Parse(m *ice.Message, rewrite Rewrite, page int, line []byte,
|
|||||||
// 解析单词
|
// 解析单词
|
||||||
h, w, r = mat.lex.Parse(m, mat.name(i), r)
|
h, w, r = mat.lex.Parse(m, mat.name(i), r)
|
||||||
// 解析状态
|
// 解析状态
|
||||||
s := mat.mat[i][h]
|
var s *State
|
||||||
|
if h < len(mat.mat[i]) {
|
||||||
|
s = mat.mat[i][h]
|
||||||
|
}
|
||||||
|
|
||||||
if s != nil { // 全局语法检查
|
if s != nil { // 全局语法检查
|
||||||
if hh, ww, _ := mat.lex.Parse(m, "key", rest); hh == 0 || len(ww) <= len(w) {
|
if hh, ww, _ := mat.lex.Parse(m, "key", rest); hh == 0 || len(ww) <= len(w) {
|
||||||
@ -236,7 +236,7 @@ func (mat *Matrix) Parse(m *ice.Message, rewrite Rewrite, page int, line []byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s == nil { // 嵌套语法递归解析
|
if s == nil { // 嵌套语法递归解析
|
||||||
for j := 0; j < mat.ncell; j++ {
|
for j := 1; j < len(mat.mat[i]); j++ {
|
||||||
if n := mat.mat[i][j]; j < mat.nlang && n != nil {
|
if n := mat.mat[i][j]; j < mat.nlang && n != nil {
|
||||||
if _, w, r := mat.Parse(m, rewrite, j, rest, level+1); len(r) != len(rest) {
|
if _, w, r := mat.Parse(m, rewrite, j, rest, level+1); len(r) != len(rest) {
|
||||||
s, word, rest = n, append(word, w...), r
|
s, word, rest = n, append(word, w...), r
|
||||||
@ -245,7 +245,7 @@ func (mat *Matrix) Parse(m *ice.Message, rewrite Rewrite, page int, line []byte,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// m.Debug("%s %s|%d GET \033[33m%s\033[0m", PARSE, strings.Repeat("#", level), level, w)
|
m.Debug("%s %s|%d GET \033[33m%s\033[0m %#v", PARSE, strings.Repeat("#", level), level, w, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
//语法切换
|
//语法切换
|
||||||
@ -262,7 +262,7 @@ func (mat *Matrix) Parse(m *ice.Message, rewrite Rewrite, page int, line []byte,
|
|||||||
hash, word, rest = rewrite(m, mat.hand[hash], hash, word, rest)
|
hash, word, rest = rewrite(m, mat.hand[hash], hash, word, rest)
|
||||||
}
|
}
|
||||||
|
|
||||||
// m.Debug("%s %s/%d %s(%d): %v %v", PARSE, strings.Repeat("#", level), level, mat.hand[hash], hash, word, rest)
|
m.Debug("%s %s/%d %s(%d): %v %v", PARSE, strings.Repeat("#", level), level, mat.hand[hash], hash, word, rest)
|
||||||
return hash, word, rest
|
return hash, word, rest
|
||||||
}
|
}
|
||||||
func (mat *Matrix) show(m *ice.Message) {
|
func (mat *Matrix) show(m *ice.Message) {
|
||||||
@ -276,7 +276,7 @@ func (mat *Matrix) show(m *ice.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 1; i < len(mat.mat); i++ {
|
for i := 1; i < len(mat.mat); i++ {
|
||||||
if len(mat.mat[i]) == 0 { // 无效行
|
if mat.isVoid(i) { // 无效行
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,7 +351,7 @@ func init() {
|
|||||||
mat.mat[page] = make([]*State, mat.ncell)
|
mat.mat[page] = make([]*State, mat.ncell)
|
||||||
}
|
}
|
||||||
|
|
||||||
mat.train(m, page, hash, kit.Split(m.Option(kit.MDB_TEXT)), 1)
|
mat.train(m, page, hash, kit.Split(m.Option(kit.MDB_TEXT), " ", " ", " "), 1)
|
||||||
m.Grow(m.Prefix(MATRIX), kit.Keys(kit.MDB_HASH, key), kit.Dict(
|
m.Grow(m.Prefix(MATRIX), kit.Keys(kit.MDB_HASH, key), kit.Dict(
|
||||||
kit.MDB_TIME, m.Time(), NPAGE, m.Option(NPAGE), NHASH, m.Option(NHASH), kit.MDB_TEXT, m.Option(kit.MDB_TEXT),
|
kit.MDB_TIME, m.Time(), NPAGE, m.Option(NPAGE), NHASH, m.Option(NHASH), kit.MDB_TEXT, m.Option(kit.MDB_TEXT),
|
||||||
))
|
))
|
||||||
@ -363,11 +363,27 @@ func init() {
|
|||||||
mdb.REMOVE: {Name: "create", Help: "删除", Hand: func(m *ice.Message, arg ...string) {
|
mdb.REMOVE: {Name: "create", Help: "删除", Hand: func(m *ice.Message, arg ...string) {
|
||||||
m.Cmdy(mdb.DELETE, m.Prefix(MATRIX), "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME))
|
m.Cmdy(mdb.DELETE, m.Prefix(MATRIX), "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME))
|
||||||
}},
|
}},
|
||||||
"show": {Name: "show", Help: "矩阵", Hand: func(m *ice.Message, arg ...string) {
|
PARSE: {Name: "parse name npage text=123", Help: "解析", Hand: func(m *ice.Message, arg ...string) {
|
||||||
m.Richs(m.Prefix(MATRIX), "", m.Option(kit.MDB_NAME), func(key string, value map[string]interface{}) {
|
m.Richs(m.Prefix(MATRIX), "", m.Option(kit.MDB_NAME), func(key string, value map[string]interface{}) {
|
||||||
value = kit.GetMeta(value)
|
value = kit.GetMeta(value)
|
||||||
mat, _ := value[MATRIX].(*Matrix)
|
mat, _ := value[MATRIX].(*Matrix)
|
||||||
mat.show(m)
|
|
||||||
|
hash, word, rest := mat.Parse(m, func(m *ice.Message, nhash string, hash int, word []string, rest []byte) (int, []string, []byte) {
|
||||||
|
m.Debug("\033[32mrun --- %v %v %v\033[0m", nhash, word, rest)
|
||||||
|
return hash, word, rest
|
||||||
|
}, mat.index(m, NPAGE, m.Option(NPAGE)), []byte(m.Option(kit.MDB_TEXT)), 1)
|
||||||
|
|
||||||
|
m.Push(kit.MDB_TIME, m.Time())
|
||||||
|
m.Push(kit.MDB_HASH, mat.word[hash])
|
||||||
|
m.Push("word", kit.Format(word))
|
||||||
|
m.Push("rest", string(rest))
|
||||||
|
})
|
||||||
|
m.ProcessInner()
|
||||||
|
}},
|
||||||
|
"show": {Name: "show", Help: "矩阵", Hand: func(m *ice.Message, arg ...string) {
|
||||||
|
m.Richs(m.Prefix(MATRIX), "", kit.Select(m.Option(kit.MDB_NAME), arg, 0), func(key string, value map[string]interface{}) {
|
||||||
|
value = kit.GetMeta(value)
|
||||||
|
value[MATRIX].(*Matrix).show(m)
|
||||||
})
|
})
|
||||||
m.ProcessInner()
|
m.ProcessInner()
|
||||||
}},
|
}},
|
||||||
@ -375,13 +391,14 @@ func init() {
|
|||||||
if m.Action(mdb.CREATE); len(arg) == 0 { // 矩阵列表
|
if m.Action(mdb.CREATE); len(arg) == 0 { // 矩阵列表
|
||||||
m.Fields(len(arg) == 0, "time,name,npage,nhash")
|
m.Fields(len(arg) == 0, "time,name,npage,nhash")
|
||||||
m.Cmdy(mdb.SELECT, m.Prefix(MATRIX), "", mdb.HASH)
|
m.Cmdy(mdb.SELECT, m.Prefix(MATRIX), "", mdb.HASH)
|
||||||
m.PushAction("show", mdb.INSERT, mdb.REMOVE)
|
m.PushAction(mdb.INSERT, "show", mdb.REMOVE)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Action(mdb.INSERT); len(arg) == 1 { // 词法列表
|
if m.Action(mdb.INSERT, "show"); len(arg) == 1 { // 词法列表
|
||||||
m.Fields(len(arg) == 1, "time,npage,nhash,text")
|
m.Fields(len(arg) == 1, "time,npage,nhash,text")
|
||||||
m.Cmdy(mdb.SELECT, m.Prefix(MATRIX), kit.Keys(kit.MDB_HASH, kit.Hashs(arg[0])), mdb.LIST)
|
m.Cmdy(mdb.SELECT, m.Prefix(MATRIX), kit.Keys(kit.MDB_HASH, kit.Hashs(arg[0])), mdb.LIST)
|
||||||
|
m.PushAction(PARSE)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,28 +5,30 @@ import (
|
|||||||
kit "github.com/shylinux/toolkits"
|
kit "github.com/shylinux/toolkits"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func _yac_load(m *ice.Message) {
|
||||||
|
m.Richs(m.Prefix(MATRIX), "", kit.MDB_FOREACH, func(key string, value map[string]interface{}) {
|
||||||
|
value = kit.GetMeta(value)
|
||||||
|
|
||||||
|
mat := NewMatrix(m, kit.Int(kit.Select("32", value[NLANG])), kit.Int(kit.Select("32", value[NCELL])))
|
||||||
|
m.Grows(m.Prefix(MATRIX), kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) {
|
||||||
|
page := mat.index(m, NPAGE, kit.Format(value[NPAGE]))
|
||||||
|
hash := mat.index(m, NHASH, kit.Format(value[NHASH]))
|
||||||
|
if mat.mat[page] == nil {
|
||||||
|
mat.mat[page] = make([]*State, mat.ncell)
|
||||||
|
}
|
||||||
|
|
||||||
|
mat.train(m, page, hash, kit.Simple(value[kit.MDB_TEXT]), 1)
|
||||||
|
})
|
||||||
|
value[MATRIX] = mat
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const YAC = "yac"
|
const YAC = "yac"
|
||||||
|
|
||||||
var Index = &ice.Context{Name: YAC, Help: "语法模块",
|
var Index = &ice.Context{Name: YAC, Help: "语法模块",
|
||||||
Commands: map[string]*ice.Command{
|
Commands: map[string]*ice.Command{
|
||||||
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
|
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
|
||||||
return
|
_yac_load(m.Load())
|
||||||
m.Load()
|
|
||||||
m.Richs(m.Prefix(MATRIX), "", kit.MDB_FOREACH, func(key string, value map[string]interface{}) {
|
|
||||||
value = kit.GetMeta(value)
|
|
||||||
|
|
||||||
mat := NewMatrix(m, kit.Int(kit.Select("32", value[NLANG])), kit.Int(kit.Select("32", value[NCELL])))
|
|
||||||
m.Grows(m.Prefix(MATRIX), kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) {
|
|
||||||
page := mat.index(m, NPAGE, kit.Format(value[NPAGE]))
|
|
||||||
hash := mat.index(m, NHASH, kit.Format(value[NHASH]))
|
|
||||||
if mat.mat[page] == nil {
|
|
||||||
mat.mat[page] = make([]*State, mat.ncell)
|
|
||||||
}
|
|
||||||
|
|
||||||
mat.train(m, page, hash, kit.Simple(value[kit.MDB_TEXT]), 1)
|
|
||||||
})
|
|
||||||
value[MATRIX] = mat
|
|
||||||
})
|
|
||||||
}},
|
}},
|
||||||
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
|
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) {
|
||||||
m.Save()
|
m.Save()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user