Skip to content

Commit

Permalink
fix: some bugs
Browse files Browse the repository at this point in the history
cli 输入打词和选重字数占比错误
按键对应手指错误
ordered trie 条件判断错误
shift 用 ^ 表示
优化手感计算,缺字或有无效编码情况下更准确
nopdan committed Mar 8, 2024
1 parent 943940b commit aefa265
Showing 6 changed files with 38 additions and 28 deletions.
4 changes: 2 additions & 2 deletions cmd/output.go
Original file line number Diff line number Diff line change
@@ -102,11 +102,11 @@ func Output(data []*result.Result) {
res.Commit.Word,
fmt.Sprintf("%.2f%%", 100*commitRate(res.Commit.Word)),
res.Commit.WordChars,
fmt.Sprintf("%.2f%%", 100*commitRate(res.Commit.WordChars)),
fmt.Sprintf("%.2f%%", 100*div(res.Commit.WordChars, res.Info.TextLen)),
res.Commit.Collision,
fmt.Sprintf("%.2f%%", 100*commitRate(res.Commit.Collision)),
res.Commit.CollisionChars,
fmt.Sprintf("%.2f%%", 100*commitRate(res.Commit.CollisionChars)),
fmt.Sprintf("%.2f%%", 100*div(res.Commit.CollisionChars, res.Info.TextLen)),
})
}
t.SetStyle(table.StyleColoredBright)
8 changes: 6 additions & 2 deletions pkg/feeling/key.go
Original file line number Diff line number Diff line change
@@ -35,10 +35,14 @@ func finger(key byte) byte {
return 3
case '4', 'r', 'f', 'v':
return 4
case '5', 't', 'g', 'b', '_':
case '5', 't', 'g', 'b':
return 4
case '_':
return 5
case '6', 'y', 'h', 'n', '+':
case '+':
return 6
case '6', 'y', 'h', 'n':
return 7
case '7', 'u', 'j', 'm':
return 7
case '8', 'i', 'k', ',':
2 changes: 1 addition & 1 deletion pkg/matcher/trie.go
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ func (t *Trie) Match(brd *bytes.Reader, res *Result) {
break
}
if node.value != nil {
if !t.ordered || node.value.order > order {
if !t.ordered || order == 0 || node.value.order < order {
order = node.value.order
res.Size = Size
res.Length = Length
13 changes: 7 additions & 6 deletions pkg/smq/feel.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,12 @@ type feel struct {
last2Key byte
}

func (f *feel) Invalid() {
f.lastKey = 0
f.lastIsLeft = false
f.lastFinger = 0
}

func NewFeeling(target *result.MatchRes, spacePref string) *feel {
return &feel{mRes: target, spacePref: spacePref}
}
@@ -58,12 +64,7 @@ func (f *feel) Process(key byte) {
f.key = key
f.isLeft, f.finger = feeling.KeyPos(f.key)
// 如果当前键或者上一个键不合法(不在46键里)
if f.lastFinger == 0 || f.finger == 0 {
// 当前键不是第一个按键
if f.lastKey != 0 {
mRes.Equivalent += 2.0
mRes.Pair.Count++
}
if f.lastKey == 0 || f.finger == 0 {
f.step()
return
}
10 changes: 8 additions & 2 deletions pkg/smq/match.go
Original file line number Diff line number Diff line change
@@ -22,8 +22,12 @@ func (c *Config) match(buffer []byte, dict *data.Dict) *result.MatchRes {
util.Increase(&mRes.Dist.WordLen, res.Length)
util.Increase(&mRes.Dist.Collision, res.Pos)
util.Increase(&mRes.Dist.CodeLen, len(res.Code))
for i := range len(res.Code) {
feel.Process(res.Code[i])
if res.Code == "######" {
feel.Invalid()
} else {
for i := range len(res.Code) {
feel.Process(res.Code[i])
}
}
if res.Pos >= 2 {
mRes.Commit.Collision++
@@ -89,6 +93,7 @@ func (c *Config) match(buffer []byte, dict *data.Dict) *result.MatchRes {

// 匹配失败了
if c.Clean {
feel.Invalid()
continue
}
res.Pos = 1
@@ -125,6 +130,7 @@ func (c *Config) match(buffer []byte, dict *data.Dict) *result.MatchRes {
mRes.Dist.NotHan[ch]++
}
res.Code = "######"
res.Pos = 0
process(res)
}
return mRes
29 changes: 14 additions & 15 deletions pkg/smq/puncts.go
Original file line number Diff line number Diff line change
@@ -17,16 +17,16 @@ var zhKeysMap = map[rune]string{
'】': "]",
'·': "`",

'《': "=,",
'》': "=.",
'?': "=/",
':': "=;",
'“': "='",
'”': "='",
'!': "=1",
'¥': "=4",
'(': "=9",
')': "=0",
'《': "^,",
'》': "^.",
'?': "^/",
':': "^;",
'“': "^'",
'”': "^'",
'!': "^1",
'¥': "^4",
'(': "^9",
')': "^0",
}

var enKeysMap [128]string
@@ -38,20 +38,20 @@ func init() {
for b := byte(33); b < 128; b++ {
if 'A' <= b && b <= 'Z' {
// magic: 将英文字符转换为小写
enKeysMap[b] = string([]byte{'=', b | ' '})
enKeysMap[b] = string([]byte{'^', b | ' '})
} else if idx := strings.IndexByte(shiftKeys, b); idx != -1 {
// shift 符号
enKeysMap[b] = string([]byte{'=', baseKeys[idx]})
enKeysMap[b] = string([]byte{'^', baseKeys[idx]})
} else if idx = strings.IndexByte(numPuncts, b); idx != -1 {
// shift+数字 符号
enKeysMap[b] = "=" + strconv.Itoa(idx)
enKeysMap[b] = "^" + strconv.Itoa(idx)
} else {
enKeysMap[b] = string(b)
}
}
}

// = 作为 shift
// ^ 作为 shift
func convertPunct(char rune) string {
// 中文标点符号
if char >= 128 {
@@ -65,7 +65,6 @@ func convertPunct(char rune) string {
return ""
}
}

// 英文标点
b := byte(char)
return enKeysMap[b]

0 comments on commit aefa265

Please sign in to comment.