Skip to content

Commit

Permalink
refactor(core): almost all
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Mar 6, 2024
1 parent fd47cc0 commit e4f134c
Show file tree
Hide file tree
Showing 30 changed files with 999 additions and 852 deletions.
6 changes: 3 additions & 3 deletions frontend/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ declare module '@vue/runtime-core' {
CombsDescription: typeof import('./src/components/Result/CombsDescription.vue')['default']
CombsDistBar: typeof import('./src/components/Result/CombsDistBar.vue')['default']
ComparedBars: typeof import('./src/components/Result/comparedBars.vue')['default']
Dict: typeof import('./src/components/Home/Dict.vue')['default']
Dict: typeof import('./src/components/Dict.vue')['default']
FingerPie: typeof import('./src/components/Result/FingerPie.vue')['default']
FingersDescription: typeof import('./src/components/Result/FingersDescription.vue')['default']
HandComp: typeof import('./src/components/Result/HandComp.vue')['default']
HandsDescription: typeof import('./src/components/Result/HandsDescription.vue')['default']
Input: typeof import('./src/components/Home/Input.vue')['default']
Input: typeof import('./src/components/Input.vue')['default']
Jisu: typeof import('./src/components/Jisu.vue')['default']
KeyHeatSorted: typeof import('./src/components/Result/KeyHeatSorted.vue')['default']
Main: typeof import('./src/components/Main.vue')['default']
Expand Down Expand Up @@ -71,7 +71,7 @@ declare module '@vue/runtime-core' {
Result: typeof import('./src/components/Result/Result.vue')['default']
ResultBasic: typeof import('./src/components/Result/ResultBasic.vue')['default']
ResultKeyHeat: typeof import('./src/components/Result/ResultKeyHeat.vue')['default']
Text: typeof import('./src/components/Home/Text.vue')['default']
Text: typeof import('./src/components/Text.vue')['default']
WordsDescription: typeof import('./src/components/Result/WordsDescription.vue')['default']
WordsDistBar: typeof import('./src/components/Result/WordsDistBar.vue')['default']
}
Expand Down
21 changes: 14 additions & 7 deletions frontend/src/components/NewText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
<span class="name">匹配算法</span>
<n-radio-group v-model:value="dictConfig.algo" name="dictAlgo" :disabled="dictConfig.single">
<n-flex>
<n-radio v-for="algo in algoOptions" :key="algo.value" :value="algo.value">
<n-radio v-for="algo in algoOptions" :key="algo.value" :value="algo.value" :disabled="algo.disabled">
{{ algo.label }}
</n-radio>
</n-flex>
Expand Down Expand Up @@ -327,10 +327,12 @@ enum DictFormat {
Default = "default",
/** 极速赛码表 */
Jisu = "jisu",
/** 多多格式 */
/** 多多 */
Duoduo = "duoduo",
/** 极点格式 */
Jidian = "jidian",
/** 冰凌 */
Bingling = "bingling",
/** 小小 */
Xiaoxiao = "xiaoxiao",
}
enum Algorithm {
Expand Down Expand Up @@ -376,12 +378,16 @@ const formatOptions = [
value: DictFormat.Jisu,
},
{
label: "多多(Rime)",
label: "多多 | Rime",
value: DictFormat.Duoduo,
},
{
label: "极点",
value: DictFormat.Jidian,
label: "冰凌",
value: DictFormat.Bingling,
},
{
label: "小小 | 极点",
value: DictFormat.Xiaoxiao,
},
];
Expand All @@ -397,6 +403,7 @@ const algoOptions = [
{
label: "最短码长(慢)",
value: Algorithm.Dynamic,
disabled: true
},
];
Expand Down
51 changes: 0 additions & 51 deletions internal/gen/gen.go

This file was deleted.

16 changes: 0 additions & 16 deletions internal/gen/suf_test.go

This file was deleted.

54 changes: 0 additions & 54 deletions internal/gen/tsv.go

This file was deleted.

30 changes: 0 additions & 30 deletions internal/gen/write.go

This file was deleted.

135 changes: 135 additions & 0 deletions pkg/dict/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package dict

import (
"bufio"
"bytes"
"cmp"
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"unicode/utf8"

"github.com/nopdan/gosmq/pkg/matcher"
)

type Entry struct {
Word string
Code string
Pos int
}

// 初始化 Dict
func (d *Dict) init() {
// 匹配算法
if d.Single {
d.Matcher = matcher.NewSingle()
} else {
switch d.algorithm {
case "greedy", "":
d.Matcher = matcher.NewTrie(false, false)
case "ordered":
d.Matcher = matcher.NewTrie(true, false)
case "dynamic":
// TODO
fallthrough
default:
panic("不支持的匹配算法: " + d.algorithm)
}
}

var dict []*Entry
// 读取码表,构建 matcher
switch d.format {
case "default", "":
d.load()
case "jisu", "js":
dict = d.loadJisu()
case "duoduo", "dd", "rime":
dict = d.loadTSV(true)
case "bingling", "bl":
dict = d.loadTSV(false)
case "xiaoxiao", "xx", "jidian", "jd":
dict = d.loadXiao()
default:
panic("不支持的格式: " + d.format)
}
// 输出转换后的赛码表
var wg sync.WaitGroup
if dict != nil {
wg.Add(1)
go func() {
defer wg.Done()
Output(dict, filepath.Join("dict",
strings.TrimSuffix(d.Name, ".txt")+".txt"),
)
}()
}
d.Matcher.Build()
if dict != nil {
wg.Wait()
}
}

// 默认格式
func (d *Dict) load() {
scan := bufio.NewScanner(d.Reader)
for scan.Scan() {
wc := strings.Split(scan.Text(), "\t")
pos := 1
if len(wc) >= 3 {
pos, _ = strconv.Atoi(wc[2])
} else if len(wc) < 2 {
continue
}
d.insert(wc[0], wc[1], pos)
}
}

// 向 matcher 中添加一个词条
func (d *Dict) insert(word, code string, pos int) {
if d.Single && utf8.RuneCountInString(word) != 1 {
return
}
d.Matcher.Insert(word, code, pos)
d.Length++
}

// 输出赛码表
func Output(dict []*Entry, path string) {
// 判断文件是否存在,若存在则直接退出
_, err := os.Stat(path)
if err == nil {
fmt.Printf("赛码表已经存在:%s\n", path)
return
}
// 按照词长排序
slices.SortStableFunc(dict, func(i, j *Entry) int {
return cmp.Compare(
utf8.RuneCountInString(i.Word),
utf8.RuneCountInString(j.Word),
)
})
var buf bytes.Buffer
buf.Grow(len(dict))
for _, entry := range dict {
buf.WriteString(entry.Word)
buf.WriteByte('\t')
buf.WriteString(entry.Code)
if entry.Pos != 1 {
buf.WriteByte('\t')
buf.WriteString(strconv.Itoa(entry.Pos))
}
buf.WriteByte('\n')
}

err = os.WriteFile(path, buf.Bytes(), 0644)
if err != nil {
fmt.Println("输出赛码表失败:", err)
} else {
fmt.Println("输出赛码表成功:", path)
}
}
Loading

0 comments on commit e4f134c

Please sign in to comment.