Skip to content

Commit

Permalink
feat: auto_select_pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed May 19, 2024
1 parent 12f6260 commit 1e5741d
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 217 deletions.
2 changes: 1 addition & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Config) convert() {
Text: &data.Text{Path: c.Path},
Format: c.Format,
SelectKeys: c.SelectKeys,
Push: c.PushStart,
Pattern: fmt.Sprintf("^.{%d,}$", c.PushStart),
Overwrite: c.Overwrite,
}
smq.AddDict(dict)
Expand Down
1 change: 0 additions & 1 deletion frontend/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ declare module '@vue/runtime-core' {
NDrawerContent: typeof import('naive-ui')['NDrawerContent']
NFlex: typeof import('naive-ui')['NFlex']
NInput: typeof import('naive-ui')['NInput']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NMessageProvider: typeof import('naive-ui')['NMessageProvider']
NRadio: typeof import('naive-ui')['NRadio']
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
Expand Down
32 changes: 16 additions & 16 deletions frontend/src/components/Main.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
import { ClipboardOutline as ClipboardIcon } from "@vicons/ionicons5";
import { Data } from "./Data";
// 文本
Expand Down Expand Up @@ -84,8 +83,8 @@ enum SpacePreference {
interface Dict extends TextConfig {
/** 码表格式 */
format: DictFormat;
/** 起顶码长 */
push: number;
/** 顶屏模式 */
pattern: string;
/** 选重键 */
keys: string;
/** 是否只用码表里的单字 */
Expand All @@ -109,10 +108,6 @@ const formatOptions = [
label: "多多",
value: DictFormat.Duoduo,
},
{
label: "小小 | 极点",
value: DictFormat.Xiaoxiao,
},
{
label: "冰凌",
value: DictFormat.Bingling,
Expand All @@ -121,6 +116,10 @@ const formatOptions = [
label: "chai",
value: DictFormat.Chai,
},
{
label: "小小 | 极点",
value: DictFormat.Xiaoxiao,
},
];
const algoOptions = [
Expand All @@ -131,12 +130,13 @@ const algoOptions = [
{
label: "贪心匹配",
value: Algorithm.Greedy,
disabled: false,
},
{
label: "最短码长(慢)",
value: Algorithm.Dynamic,
disabled: true,
},
// {
// label: "最短码长(慢)",
// value: Algorithm.Dynamic,
// disabled: true,
// },
];
const spaceOptions = [
Expand All @@ -157,7 +157,7 @@ const spaceOptions = [
const dict = reactive({
source: "local",
format: DictFormat.Default,
push: 4,
pattern: "^.{4,}$",
keys: "_;'",
single: false,
algo: Algorithm.Ordered,
Expand Down Expand Up @@ -403,9 +403,9 @@ async function race() {
</n-radio-group>
</div>
<div class="line">
<span class="name">起顶码长</span>
<n-input-number
v-model:value="dict.push"
<span class="name">顶屏模式</span>
<n-input
v-model:value="dict.pattern"
:min="0"
style="width: 230px"
:disabled="dict.format === DictFormat.Default || dict.format === DictFormat.Jisu"
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/Show.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { Data, New2Old } from "./Data";
import { Close as CloseIcon } from "@vicons/ionicons5";
import { Data } from "./Data";
const props = defineProps<{
result: Data[];
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.5.8 h1:8BCzJdSvUbaDuRba4YVh+SKMGcAAKdkcF3SVFbrHAtQ=
github.com/jedib0t/go-pretty/v6 v6.5.8/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+UV8OU=
github.com/jedib0t/go-pretty/v6 v6.5.9/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down Expand Up @@ -65,16 +63,12 @@ github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -87,22 +81,16 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
34 changes: 0 additions & 34 deletions pkg/data/chai.go

This file was deleted.

26 changes: 17 additions & 9 deletions pkg/data/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"fmt"
"regexp"
"sync"
"time"

Expand All @@ -16,8 +17,9 @@ type Dict struct {
Text *Text
// default, jisu, duoduo|bingling, jidian
Format string
// 起顶码长
Push int
// 顶屏模式
Pattern string
pattern *regexp.Regexp
// 选重键
SelectKeys string
selectKeys []string
Expand Down Expand Up @@ -53,6 +55,12 @@ func (d *Dict) Init() {
}
}
now := time.Now()
// 顶屏模式
var err error
d.pattern, err = regexp.Compile(d.Pattern)
if err != nil {
d.pattern, _ = regexp.Compile("^.{4,}$")
}
// 选重键
d.selectKeys = make([]string, 0, 10)
for i := range len(d.SelectKeys) {
Expand Down Expand Up @@ -84,16 +92,16 @@ func (d *Dict) Init() {
switch d.Format {
case "default", "":
d.load()
case "jisu", "js":
dict = d.loadJisu()
case "duoduo", "dd", "rime":
dict = d.loadTSV(true)
dict = d.read1("duoduo")
case "bingling", "bl":
dict = d.loadTSV(false)
case "xiaoxiao", "xx", "jidian", "jd":
dict = d.loadXiao()
dict = d.read1("bingling")
case "chai":
dict = d.loadChai()
dict = d.read1("chai")
case "jisu", "js":
dict = d.read1("jisu")
case "xiaoxiao", "xx", "jidian", "jd":
dict = d.read2()
default:
logger.Fatal("码表格式不正确", "format", d.Format)
}
Expand Down
99 changes: 99 additions & 0 deletions pkg/data/dict1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package data

import (
"bufio"
"strconv"
"strings"
)

// 读取每行只有一个词条的码表
func (d *Dict) read1(format string) []*Entry {
ret := make([]*Entry, 0, 1e5)
// 统计编码出现的次数
stat := make(map[string]int)
// 逐行读取码表
scan := bufio.NewScanner(d.Text.reader)
var word, code string
var pos int
for scan.Scan() {
values := strings.Split(scan.Text(), "\t")
if len(values) < 2 {
continue
}
switch format {
case "duoduo":
word, code = values[0], values[1]
case "bingling":
word, code = values[1], values[0]
case "chai":
if len(values) < 5 {
continue
}
word, code = values[0], values[3]
pos, _ = strconv.Atoi(values[4])
code = d.addSuffix(code, pos)
case "jisu":
word, code = values[0], values[1]
// 带空格 a_ aa_
if len(code) > 1 && code[len(code)-1] == '_' {
pos = 1
break
}
code, pos = findSuffixInteger(code)
// 不带数字 akdb ksdw
if pos == 1 {
break
}
// 数字选重 a1 aa3
code = d.addSuffix(code, pos)
}
switch format {
case "duoduo", "bingling":
stat[code]++
pos = stat[code]
code = d.addSuffix(code, pos)
}
ret = append(ret, &Entry{word, code, pos})
d.insert(word, code, pos)
}
return ret
}

// 为编码加上选重键,pos 是编码出现的次数,最小为 1
func (d *Dict) addSuffix(code string, pos int) string {
// 选重
if pos > 1 {
// 缓存 选重数字转为字符串
for pos > len(d.selectKeys)-2 {
d.selectKeys = append(d.selectKeys, strconv.Itoa(len(d.selectKeys)+1))
}
return code + d.selectKeys[pos-1]
}
// 首选
if d.pattern.MatchString(code) {
return code
}
return code + "_"
}

// 查找末尾数字,返回前缀和后缀
func findSuffixInteger(s string) (string, int) {
var prefix, suffix string
for i := len(s) - 1; i >= 0; i-- {
if s[i] >= '0' && s[i] <= '9' {
suffix = string(s[i]) + suffix
} else {
prefix = s[:i+1]
break
}
}
if suffix == "" {
return s, 1
}

i, _ := strconv.Atoi(suffix)
if i <= 0 {
i = 10
}
return prefix, i
}
4 changes: 2 additions & 2 deletions pkg/data/xiaoxiao.go → pkg/data/dict2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

// 小小|极点
func (d *Dict) loadXiao() []*Entry {
// 读取一行有多个词条的码表
func (d *Dict) read2() []*Entry {
var cap int = 1e5
if d.Text.size > 0 {
cap = d.Text.size / 32
Expand Down
Loading

0 comments on commit 1e5741d

Please sign in to comment.