Skip to content

Commit

Permalink
refactor: char result
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Mar 10, 2024
1 parent 63bdf7c commit 4702e8d
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 164 deletions.
80 changes: 45 additions & 35 deletions cmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func Output(data []*result.Result) {
t.AppendHeader(table.Row{"总键数", "码长", "十击速度", "非汉字数", "计数", "缺字数", "计数"})
for _, res := range data {
t.AppendRow([]any{
res.CodeLen.Total,
fmt.Sprintf("%.4f", res.CodeLen.PerChar),
fmt.Sprintf("%.2f", 600/res.CodeLen.PerChar),
res.Keys.Count,
fmt.Sprintf("%.4f", res.Keys.CodeLen),
fmt.Sprintf("%.2f", 600/res.Keys.CodeLen),
res.Han.NotHans, res.Han.NotHanCount,
res.Han.Lacks, res.Han.LackCount,
})
Expand All @@ -101,19 +101,16 @@ func Output(data []*result.Result) {
t = table.NewWriter()
t.AppendHeader(table.Row{"首选词", "打词", "--", "打词字数", "--", "选重", "--", "选重字数", "--"})
for _, res := range data {
commitRate := func(x int) float64 {
return div(x, res.Commit.Count)
}
t.AppendRow([]any{
res.Commit.WordFirst,
res.Commit.Word,
fmt.Sprintf("%.2f%%", 100*commitRate(res.Commit.Word)),
res.Commit.WordChars,
fmt.Sprintf("%.2f%%", 100*div(res.Commit.WordChars, res.Info.TextLen)),
commitRate(res.Commit.Word, res),
res.Char.Word,
charRate(res.Char.Word, res),
res.Commit.Collision,
fmt.Sprintf("%.2f%%", 100*commitRate(res.Commit.Collision)),
res.Commit.CollisionChars,
fmt.Sprintf("%.2f%%", 100*div(res.Commit.CollisionChars, res.Info.TextLen)),
commitRate(res.Commit.Collision, res),
res.Char.Collision,
charRate(res.Char.Collision, res),
})
}
t.SetStyle(table.StyleColoredBright)
Expand All @@ -123,12 +120,12 @@ func Output(data []*result.Result) {
t.AppendHeader(table.Row{"左手", "右手", "左右", "右左", "左左", "右右"})
for _, res := range data {
t.AppendRow([]any{
fmt.Sprintf("%.2f%%", 100*div(res.LeftHand, res.CodeLen.Total)),
fmt.Sprintf("%.2f%%", 100*div(res.RightHand, res.CodeLen.Total)),
fmt.Sprintf("%.2f%%", 100*div(res.Pair.LeftToRight, res.Pair.Count)),
fmt.Sprintf("%.2f%%", 100*div(res.Pair.RightToLeft, res.Pair.Count)),
fmt.Sprintf("%.2f%%", 100*div(res.Pair.LeftToLeft, res.Pair.Count)),
fmt.Sprintf("%.2f%%", 100*div(res.Pair.RightToRight, res.Pair.Count)),
keyRate(res.Keys.LeftHand, res),
keyRate(res.Keys.RightHand, res),
pairRate(res.Pair.LeftToRight, res),
pairRate(res.Pair.RightToLeft, res),
pairRate(res.Pair.LeftToLeft, res),
pairRate(res.Pair.RightToRight, res),
})
}
t.SetStyle(table.StyleColoredBright)
Expand All @@ -137,20 +134,17 @@ func Output(data []*result.Result) {
t = table.NewWriter()
t.AppendHeader(table.Row{"当量", "异手", "同指", "三连击", "两连击", "小跨排", "大跨排", "异指", "小指干扰", "错手"})
for _, res := range data {
pairRate := func(x int) float64 {
return div(x, res.Pair.Count)
}
t.AppendRow([]any{
fmt.Sprintf("%.4f", pairRate(int(res.Equivalent))),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.DiffHand)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.SameFinger)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.TribleHit)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.DoubleHit)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.SingleSpan)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.MultiSpan)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.DiffFinger)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.Disturb)),
fmt.Sprintf("%.2f%%", 100*pairRate(res.Pair.Staggered)),
fmt.Sprintf("%.4f", float64(res.Pair.Equivalent)/float64(res.Pair.Count)),
pairRate(res.Pair.DiffHand, res),
pairRate(res.Pair.SameFinger, res),
pairRate(res.Pair.TribleHit, res),
pairRate(res.Pair.DoubleHit, res),
pairRate(res.Pair.SingleSpan, res),
pairRate(res.Pair.MultiSpan, res),
pairRate(res.Pair.DiffFinger, res),
pairRate(res.Pair.Disturb, res),
pairRate(res.Pair.Staggered, res),
})
}
t.SetStyle(table.StyleColoredBright)
Expand All @@ -161,7 +155,7 @@ func Output(data []*result.Result) {
for _, res := range data {
newRow := []any{}
for i := 1; i < 11; i++ {
newRow = append(newRow, fmt.Sprintf("%.2f%%", 100*div(res.Dist.Finger[i], res.CodeLen.Total)))
newRow = append(newRow, keyRate(res.Dist.Finger[i], res))
}
t.AppendRow(newRow)
}
Expand All @@ -173,7 +167,7 @@ func Output(data []*result.Result) {
row := []any{}
for i := range len(keys) {
header = append(header, string(keys[i]))
row = append(row, fmt.Sprintf("%.2f%%", 100*res.Keys[string(keys[i])].Rate))
row = append(row, keyRate(res.Dist.Key[string(keys[i])], res))
}
writer := table.NewWriter()
writer.AppendHeader(header)
Expand All @@ -193,6 +187,22 @@ func Output(data []*result.Result) {
fmt.Println("----------------------")
}

func div(x, y int) float64 {
return float64(x) / float64(y)
func commitRate(count int, res *result.Result) string {
rate := float64(count) / float64(res.Commit.Count)
return fmt.Sprintf("%.2f%%", 100*rate)
}

func charRate(count int, res *result.Result) string {
rate := float64(count) / float64(res.Char.Count)
return fmt.Sprintf("%.2f%%", 100*rate)
}

func keyRate(count int, res *result.Result) string {
rate := float64(count) / float64(res.Keys.Count)
return fmt.Sprintf("%.2f%%", 100*rate)
}

func pairRate(count int, res *result.Result) string {
rate := float64(count) / float64(res.Pair.Count)
return fmt.Sprintf("%.2f%%", 100*rate)
}
28 changes: 11 additions & 17 deletions frontend/src/components/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,27 @@

export interface Data {
Info: Info;
Commit: Commit;
Pair: Pair;
Keys: { [key: string]: Key };
Commit: Char;
Char: Char;
Han: Han;
Pair: Pair;
Keys: Keys;
Dist: Dist;
CodeLen: CodeLen;
LeftHand: number;
RightHand: number;
Equivalent: number;
}

export interface CodeLen {
Total: number;
PerChar: number;
}

export interface Commit {
export interface Char {
Count: number;
Word: number;
WordChars: number;
WordFirst: number;
Collision: number;
CollisionChars: number;
}

export interface Dist {
CodeLen: number[];
WordLen: number[];
Collision: number[];
Finger: number[];
Key: { [key: string]: number };
}

export interface Han {
Expand All @@ -51,13 +42,16 @@ export interface Info {
Single: boolean;
}

export interface Key {
export interface Keys {
Count: number;
Rate: number;
CodeLen: number;
LeftHand: number;
RightHand: number;
}

export interface Pair {
Count: number;
Equivalent: number;
SameFinger: number;
DoubleHit: number;
TribleHit: number;
Expand Down
26 changes: 13 additions & 13 deletions frontend/src/components/MultiResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const res = computed(() => {
let cp = JSON.parse(JSON.stringify(props.data));
cp.Commit.Word = commitRate(cp.Commit.Word);
cp.Commit.Collision = commitRate(cp.Commit.Collision);
cp.Commit.WordChars = charRate(cp.Commit.WordChars);
cp.Commit.CollisionChars = charRate(cp.Commit.CollisionChars);
cp.LeftHand = keyRate(cp.LeftHand);
cp.RightHand = keyRate(cp.RightHand);
cp.Char.Word = charRate(cp.Char.Word);
cp.Char.Collision = charRate(cp.Char.Collision);
cp.Keys.LeftHand = keyRate(cp.Keys.LeftHand);
cp.Keys.RightHand = keyRate(cp.Keys.RightHand);
cp.Pair.SameHand = pairRate(cp.Pair.SameHand);
cp.Pair.DiffHand = pairRate(cp.Pair.DiffHand);
cp.Pair.SameFinger = pairRate(cp.Pair.SameFinger);
Expand All @@ -40,11 +40,11 @@ function commitRate(count: number): string {
}
function charRate(count: number): string {
return ((count / props.data.Info.TextLen) * 100).toFixed(2) + "%";
return ((count / props.data.Char.Count) * 100).toFixed(2) + "%";
}
function keyRate(count: number): string {
return ((count / props.data.CodeLen.Total) * 100).toFixed(2) + "%";
return ((count / props.data.Keys.Count) * 100).toFixed(2) + "%";
}
function pairRate(count: number): string {
Expand Down Expand Up @@ -82,8 +82,8 @@ function dist(dist: number[]) {
<th>选重</th>
</tr>
<tr>
<td>{{ data.CodeLen.PerChar.toFixed(2) }}</td>
<td>{{ data.CodeLen.Total }}</td>
<td>{{ data.Keys.CodeLen.toFixed(4) }}</td>
<td>{{ data.Keys.Count }}</td>
<td>{{ data.Commit.Count }}</td>
<td>{{ res.Commit.Word }}</td>
<td>{{ res.Commit.Collision }}</td>
Expand All @@ -100,8 +100,8 @@ function dist(dist: number[]) {
<td>{{ data.Info.DictLen }}</td>
<td>{{ data.Han.Lacks }}</td>
<td>{{ data.Info.TextLen }}</td>
<td>{{ res.Commit.WordChars }}</td>
<td>{{ res.Commit.CollisionChars }}</td>
<td>{{ res.Char.Word }}</td>
<td>{{ res.Char.Collision }}</td>
</tr>
</table>
</div>
Expand All @@ -116,7 +116,7 @@ function dist(dist: number[]) {
<th>右右</th>
</tr>
<tr>
<td>{{ res.LeftHand }}</td>
<td>{{ res.Keys.LeftHand }}</td>
<td>{{ res.Pair.LeftToLeft }}</td>
<td>{{ res.Pair.LeftToRight }}</td>
<td>{{ res.Pair.RightToLeft }}</td>
Expand All @@ -130,7 +130,7 @@ function dist(dist: number[]) {
<th>大跨排</th>
</tr>
<tr>
<td>{{ res.RightHand }}</td>
<td>{{ res.Keys.RightHand }}</td>
<td>{{ res.Pair.SameFinger }}</td>
<td>{{ res.Pair.DoubleHit }}</td>
<td>{{ res.Pair.SingleSpan }}</td>
Expand All @@ -144,7 +144,7 @@ function dist(dist: number[]) {
<th>小指干扰</th>
</tr>
<tr>
<td>{{ (data.Equivalent / data.Pair.Count).toFixed(4) }}</td>
<td>{{ (data.Pair.Equivalent / data.Pair.Count).toFixed(4) }}</td>
<td>{{ res.Pair.DiffHand }}</td>
<td>{{ res.Pair.DiffFinger }}</td>
<td>{{ res.Pair.Staggered }}</td>
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ const multi = ref(true);
<n-drawer v-model:show="active" :width="502" placement="bottom" height="100vh">
<n-drawer-content :native-scrollbar="false" closable>
<template #header>
<div style="display: flex; align-items: center; justify-content: center">
<div style="width: 100%; margin-right: 10px">文章</div>
<n-select v-model:value="resIndex" :options="resOptions" placeholder="请选择" style="min-width: 500px" />
</div>
<span style="width: auto">
<!-- <n-select v-model:value="resIndex" :options="resOptions" placeholder="请选择" style="min-width: 500px" /> -->
<span>标题</span>
</span>
</template>
<div>
<div v-if="multi" style="display: flex; width: 100%; margin: auto; justify-content: center">
Expand Down
4 changes: 4 additions & 0 deletions pkg/data/dict.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package data

import (
"fmt"
"sync"
"time"

"github.com/nopdan/gosmq/pkg/matcher"
"github.com/nopdan/gosmq/pkg/util"
Expand Down Expand Up @@ -50,6 +52,7 @@ func (d *Dict) Init() {
return
}
}
now := time.Now()
// 选重键
d.selectKeys = make([]string, 0, 10)
for i := range len(d.SelectKeys) {
Expand Down Expand Up @@ -100,6 +103,7 @@ func (d *Dict) Init() {
d.IsInit = true
if dict == nil || len(dict) == 0 {
d.Matcher.Build()
logger.Info(fmt.Sprintf("已载入码表: %s", d.Text.Name), "耗时", time.Since(now))
return
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/matcher/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package matcher

import (
"bytes"
"unicode"
"unicode/utf8"
)

Expand Down Expand Up @@ -49,6 +50,9 @@ func (s *Single) Match(brd *bytes.Reader, res *Result) {
res.Char = ch
res.Size = size
res.Length = 1
if unicode.IsSpace(ch) {
return
}
if v, ok := s.dict[ch]; ok {
res.Code = v.code
res.Pos = v.pos
Expand Down
10 changes: 7 additions & 3 deletions pkg/matcher/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package matcher
import (
"bytes"
"io"
"unicode"
)

type Trie struct {
Expand Down Expand Up @@ -65,18 +66,21 @@ func (t *Trie) Match(brd *bytes.Reader, res *Result) {
var Size, Length int
var order int
for {
char, size, err := brd.ReadRune()
ch, size, err := brd.ReadRune()
if err != nil {
break
}
if Char == 0 {
Char = char
Char = ch
CharSize = size
}
Size += size
Length++

node = node.ch[char]
if unicode.IsSpace(ch) {
break
}
node = node.ch[ch]
if node == nil {
break
}
Expand Down
Loading

0 comments on commit 4702e8d

Please sign in to comment.