lex

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Index = &ctx.Context{Name: "lex", Help: "词法中心",
	Caches: map[string]*ctx.Cache{
		"nmat": &ctx.Cache{Name: "nmat", Value: "0", Help: "nmat"},
	},
	Configs: map[string]*ctx.Config{
		"npage": &ctx.Config{Name: "npage", Value: "1", Help: "npage"},
		"nhash": &ctx.Config{Name: "nhash", Value: "1", Help: "npage"},
		"info":  &ctx.Config{Name: "info", Value: map[string]interface{}{"compact": true, "ncell": 128, "nlang": 64}, Help: "嵌套层级日志的标记"},
	},
	Commands: map[string]*ctx.Command{
		"init": &ctx.Command{Name: "init", Help: "启动", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			return
		}},
		"spawn": &ctx.Command{Name: "spawn", Help: "添加词法规则", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if _, ok := m.Target().Server.(*LEX); m.Assert(ok) {
				m.Start(fmt.Sprintf("matrix%d", m.Capi("nmat", 1)), "matrix")
			}
			return
		}},
		"train": &ctx.Command{Name: "train seed [hash [page]", Help: "添加词法规则", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if lex, ok := m.Target().Server.(*LEX); m.Assert(ok) {
				page := lex.index(m, "npage", m.Confx("npage", arg, 2))
				hash := lex.index(m, "nhash", m.Confx("nhash", arg, 1))
				if lex.mat[page] == nil {
					lex.mat[page] = map[byte]*State{}
				}
				m.Cap("npage", len(lex.page))
				m.Cap("nhash", len(lex.hash))

				m.Result(0, lex.train(m, page, hash, []byte(arg[0])))
				lex.seed = append(lex.seed, &Seed{page, hash, arg[0]})
				m.Cap("stream", fmt.Sprintf("%d,%s,%s", m.Capi("nseed", 1), m.Cap("npage"), m.Cap("nhash")))
			}
			return
		}},
		"parse": &ctx.Command{Name: "parse line [page]", Help: "解析单词", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if lex, ok := m.Target().Server.(*LEX); m.Assert(ok) {
				page := lex.index(m, "npage", m.Confx("npage", arg, 1))
				hash, rest, word := lex.parse(m, page, []byte(arg[0]))
				m.Result(0, hash, string(rest), string(word))
			}
			return
		}},
		"show": &ctx.Command{Name: "show seed|page|hash|mat", Help: "查看信息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if lex, ok := m.Target().Server.(*LEX); m.Assert(ok) {
				if len(arg) == 0 {
					m.Append("seed", len(lex.seed))
					m.Append("page", len(lex.page))
					m.Append("hash", len(lex.hash))
					m.Append("node", len(lex.state))
					m.Table()
					return
				}
				switch arg[0] {
				case "seed":
					for _, v := range lex.seed {
						m.Add("append", "page", fmt.Sprintf("%d", v.page))
						m.Add("append", "hash", fmt.Sprintf("%d", v.hash))
						m.Add("append", "word", fmt.Sprintf("%s", strings.Replace(strings.Replace(v.word, "\n", "\\n", -1), "\t", "\\t", -1)))
					}
					m.Table()
				case "page":
					for k, v := range lex.page {
						m.Add("append", "page", k)
						m.Add("append", "code", fmt.Sprintf("%d", v))
					}
					m.Sort("code", "int").Table()
				case "hash":
					for k, v := range lex.hash {
						m.Add("append", "hash", k)
						m.Add("append", "code", fmt.Sprintf("%d", v))
					}
					m.Table()
				case "mat":
					for _, v := range lex.mat {
						for j := byte(0); j < byte(m.Confi("info", "ncell")); j++ {
							s := v[j]
							if s == nil {
								m.Add("append", fmt.Sprintf("%c", j), "")
							} else {
								star := 0
								if s.star {
									star = 1
								}
								m.Add("append", fmt.Sprintf("%c", j), fmt.Sprintf("%d,%d,%d", star, s.next, s.hash))
							}
						}
					}

					ncol := len(m.Meta["append"])
					nrow := len(m.Meta[m.Meta["append"][0]])
					for i := 0; i < ncol-1; i++ {
						for j := i + 1; j < ncol; j++ {
							same := true
							void := true
							for n := 0; n < nrow; n++ {
								if m.Meta[m.Meta["append"][i]][n] != "" {
									void = false
								}
								if m.Meta[m.Meta["append"][i]][n] != m.Meta[m.Meta["append"][j]][n] {
									same = false
									break
								}
							}

							if same {
								if !void {
									key = m.Meta["append"][i] + m.Meta["append"][j]
									m.Meta[key] = m.Meta[m.Meta["append"][i]]
									m.Meta["append"][i] = key
								}
								for k := j; k < ncol-1; k++ {
									m.Meta["append"][k] = m.Meta["append"][k+1]
								}
								ncol--
								j--
							}
						}
					}
					m.Meta["append"] = m.Meta["append"][:ncol]
					m.Table()
				}
			}
			return
		}},
	},
}

Functions

This section is empty.

Types

type LEX

type LEX struct {
	*ctx.Context
	// contains filtered or unexported fields
}

func (*LEX) Begin

func (lex *LEX) Begin(m *ctx.Message, arg ...string) ctx.Server

func (*LEX) Close

func (lex *LEX) Close(m *ctx.Message, arg ...string) bool

func (*LEX) Spawn

func (lex *LEX) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server

func (*LEX) Start

func (lex *LEX) Start(m *ctx.Message, arg ...string) bool

type Point

type Point struct {
	// contains filtered or unexported fields
}

type Seed

type Seed struct {
	// contains filtered or unexported fields
}

type State

type State struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL