1
0
forked from x/icebergs
This commit is contained in:
shylinux 2021-06-12 20:37:22 +08:00
parent 56e9ae881a
commit b3950d3e2a
2 changed files with 36 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package cli
import ( import (
"encoding/base64" "encoding/base64"
"fmt"
"image/color" "image/color"
"math/rand" "math/rand"
"strconv" "strconv"
@ -92,6 +93,21 @@ func _qrcode_web(m *ice.Message, text string, arg ...string) {
} }
} }
func Color(m *ice.Message, c string, str string) string {
wrap, color := `<span style="color:%s">%s</span>`, c
if aaa.SessIsCli(m) {
wrap, color = "\033[3%sm%s\033[0m", map[string]string{
BLACK: "0",
RED: "1", GREEN: "2", YELLOW: "3",
BLUE: "4", MAGENTA: "5", CYAN: "6",
WHITE: "7",
}[c]
}
return fmt.Sprintf(wrap, color, str)
}
func ColorRed(m *ice.Message, str string) string { return Color(m, RED, str) }
func ColorGreen(m *ice.Message, str string) string { return Color(m, GREEN, str) }
const ( const (
FG = "fg" FG = "fg"
BG = "bg" BG = "bg"

View File

@ -1,9 +1,11 @@
package lex package lex
import ( import (
"sort"
"strconv" "strconv"
ice "github.com/shylinux/icebergs" ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/mdb"
kit "github.com/shylinux/toolkits" kit "github.com/shylinux/toolkits"
) )
@ -88,7 +90,7 @@ func (mat *Matrix) index(m *ice.Message, hash string, h string) int {
return which[h] return which[h]
} }
func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int { func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int {
m.Debug("%s %s page: %v hash: %v seed: %v", "train", "lex", npage, nhash, seed) m.Debug("%s %s page: %v hash: %v seed: %v", TRAIN, LEX, npage, nhash, seed)
page := mat.index(m, NPAGE, npage) page := mat.index(m, NPAGE, npage)
hash := mat.index(m, NHASH, nhash) hash := mat.index(m, NHASH, nhash)
@ -143,7 +145,7 @@ func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int {
} }
case '.': case '.':
for c := 0; c < len(cn); c++ { for c := 1; c < len(cn); c++ {
cc = append(cc, byte(c)) cc = append(cc, byte(c))
} }
@ -174,7 +176,7 @@ func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int {
if mat.mat[s][c] != nil { if mat.mat[s][c] != nil {
*state = *mat.mat[s][c] *state = *mat.mat[s][c]
} }
m.Debug("GET(%d,%d): %v", s, c, state) m.Debug("GET(%d,%d): %#v", s, c, state)
switch flag { switch flag {
case '+': case '+':
@ -189,13 +191,14 @@ func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int {
if state.next == 0 { if state.next == 0 {
state.next = len(mat.mat) state.next = len(mat.mat)
mat.mat = append(mat.mat, make(map[byte]*State)) mat.mat = append(mat.mat, make(map[byte]*State))
sn = append(sn, false) sn = append(sn, true)
} else {
sn[state.next] = true
} }
sn[state.next] = true
mat.mat[s][c] = state mat.mat[s][c] = state
points = append(points, &Point{s, c}) points = append(points, &Point{s, c})
m.Debug("SET(%d,%d): %v", s, c, state) m.Debug("SET(%d,%d): %#v", s, c, state)
} }
} }
@ -207,15 +210,17 @@ func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int {
} }
} }
sort.Ints(ss)
sort.Reverse(sort.IntSlice(ss))
for _, s := range ss { for _, s := range ss {
if s < mat.nlang || s >= len(mat.mat) { if s < mat.nlang || s >= len(mat.mat) {
continue continue
} }
if len(mat.mat[s]) > 0 {
if len(mat.mat[s]) == 0 { break
mat.mat = mat.mat[:s]
m.Debug("DEL: %d", len(mat.mat))
} }
mat.mat = mat.mat[:s]
m.Debug("DEL: %d", len(mat.mat))
} }
for _, s := range ss { for _, s := range ss {
@ -224,12 +229,12 @@ func (mat *Matrix) Train(m *ice.Message, npage, nhash string, seed string) int {
*state = *mat.mat[p.s][p.c] *state = *mat.mat[p.s][p.c]
if state.next == s { if state.next == s {
m.Debug("GET(%d, %d): %v", p.s, p.c, state) m.Debug("GET(%d, %d): %#v", p.s, p.c, state)
if state.hash = hash; state.next >= len(mat.mat) { if state.hash = hash; state.next >= len(mat.mat) {
state.next = 0 state.next = 0
} }
mat.mat[p.s][p.c] = state mat.mat[p.s][p.c] = state
m.Debug("SET(%d, %d): %v", p.s, p.c, state) m.Debug("SET(%d, %d): %#v", p.s, p.c, state)
} }
if x, ok := mat.state[*state]; !ok { if x, ok := mat.state[*state]; !ok {
@ -267,9 +272,7 @@ func (mat *Matrix) Parse(m *ice.Message, npage string, line []byte) (hash int, w
} }
// m.Debug("GET (%d,%d): %v", s, c, state) // m.Debug("GET (%d,%d): %v", s, c, state)
word = append(word, c) if word = append(word, c); state.star {
if state.star {
star = s star = s
} else if x, ok := mat.mat[star][c]; !ok || !x.star { } else if x, ok := mat.mat[star][c]; !ok || !x.star {
star = 0 star = 0
@ -313,9 +316,9 @@ func (mat *Matrix) show(m *ice.Message) {
key := kit.Format("%c", j) key := kit.Format("%c", j)
if node := mat.mat[i][byte(j)]; node != nil { if node := mat.mat[i][byte(j)]; node != nil {
if node.hash == 0 { if node.hash == 0 {
m.Push(key, kit.Select(kit.Format("%02d", node.next), mat.hand[node.next])) m.Push(key, kit.Select(kit.Format("%02d", node.next), cli.ColorRed(m, mat.hand[node.next])))
} else { } else {
m.Push(key, kit.Select(kit.Format("w%02d", node.hash), mat.word[node.hash])) m.Push(key, kit.Select(kit.Format("w%02d", node.hash), cli.ColorGreen(m, mat.word[node.hash])))
} }
} else { } else {
m.Push(key, "") m.Push(key, "")