lex

package
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: MIT Imports: 5 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: "矩阵数量"},
	},
	Configs: map[string]*ctx.Config{
		"npage": &ctx.Config{Name: "npage", Value: "1", Help: "默认页"},
		"nhash": &ctx.Config{Name: "nhash", Value: "1", Help: "默认值"},
		"meta": &ctx.Config{Name: "meta", Value: map[string]interface{}{
			"ncell": 128, "nlang": 64, "compact": true,
			"name": "mat%d", "help": "matrix",
		}, 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) {
			if _, ok := m.Target().Server.(*LEX); m.Assert(ok) {
				m.Spawn().Cmd("train", "-?[a-zA-Z_0-9:/.]+", "key", "cmd")
				m.Spawn().Cmd("train", "\"[^\"]*\"", "str", "cmd")
				m.Spawn().Cmd("train", "'[^']*'", "str", "cmd")
				m.Spawn().Cmd("train", "#[^\n]*", "com", "cmd")
				m.Spawn().Cmd("train", "[~!@$%()]", "ops", "cmd")
			}
			return
		}},
		"spawn": &ctx.Command{Name: "spawn [help [name]]", 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(kit.Select(m.Conf("lex.meta", "name"), arg, 1), m.Capi("nmat", 1)),
					kit.Select(m.Conf("lex.meta", "help"), arg, 0))
			}
			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) {
				hash := lex.index(m, "nhash", m.Confx("nhash", arg, 1))
				page := lex.index(m, "npage", m.Confx("npage", arg, 2))
				if lex.mat[page] == nil {
					lex.mat[page] = map[byte]*State{}
				}
				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("%s,%s,%s", m.Cap("nseed", len(lex.seed)),
					m.Cap("npage"), m.Cap("nhash", len(lex.hash)-1)))
			}
			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) {
				hash, rest, word := lex.parse(m, lex.index(m, "npage", m.Confx("npage", arg, 1)), []byte(arg[0]))
				m.Result(0, hash, string(rest), string(word))
			}
			return
		}},
		"split": &ctx.Command{Name: "split 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) {
				for input := []byte(arg[0]); len(input) > 0; {
					hash, rest, word := lex.parse(m, lex.index(m, "npage", m.Confx("npage", arg, 1)), input)
					m.Log("fuck", "what %v %v %v", hash, rest, word)
					if hash == 0 || len(word) == 0 || len(rest) == len(input) {
						if len(input) > 0 {
							input = input[1:]
						}
						continue
					}

					m.Push("word", string(word))
					m.Push("hash", lex.word[hash])
					m.Push("rest", string(rest))
					input = rest
				}
				m.Table()
			}
			return
		}},
		"show": &ctx.Command{Name: "show seed|page|hash|mat|node", 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.Push("seed", len(lex.seed))
					m.Push("page", len(lex.page))
					m.Push("hash", len(lex.hash))
					m.Push("nmat", len(lex.mat))
					m.Push("node", len(lex.state))
					m.Table()
					return
				}
				switch arg[0] {
				case "seed":
					for _, v := range lex.seed {
						m.Push("page", lex.hand[v.page])
						m.Push("word", strings.Replace(strings.Replace(v.word, "\n", "\\n", -1), "\t", "\\t", -1))
						m.Push("hash", lex.word[v.hash])
					}
					m.Sort("page", "int").Table()

				case "page":
					for k, v := range lex.page {
						m.Push("page", k)
						m.Push("code", v)
					}
					m.Sort("code", "int").Table()

				case "hash":
					for k, v := range lex.hash {
						m.Push("hash", k)
						m.Push("code", v)
					}
					m.Sort("code", "int").Table()

				case "node":
					for _, v := range lex.state {
						m.Push("star", v.star)
						m.Push("next", v.next)
						m.Push("hash", v.hash)
					}
					m.Table()

				case "mat":
					for i, v := range lex.mat {
						if i <= m.Capi("npage") {
							m.Push("index", lex.hand[i])
						} else if i < m.Confi("meta", "nlang") {
							continue
						} else {
							m.Push("index", i)
						}

						for j := byte(0); j < byte(m.Confi("meta", "ncell")); j++ {
							c := fmt.Sprintf("%c", j)
							switch c {
							case "\n":
								c = "\\n"
							case "\t":
								c = "\\t"
							case " ":
							default:
								if j < 0x20 {
									c = fmt.Sprintf("\\%x", j)
								}
							}

							if s := v[j]; s == nil {
								m.Push(c, "")
							} else {
								star := 0
								if s.star {
									star = 1
								}
								m.Push(c, 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) Parse

func (lex *LEX) Parse(m *ctx.Message, line []byte, page string) (hash int, rest []byte, word []byte)

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