Skip to content

Commit

Permalink
fix: 单字匹配模式选重错误
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Mar 14, 2024
1 parent 730750c commit d66f50e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
9 changes: 9 additions & 0 deletions frontend/src/components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ watch(
},
);
watch(
() => dict.single,
() => {
if (dict.single) {
dict.algo = Algorithm.Ordered;
}
},
);
function addDict(dict: Dict): void {
if (dict.source === "local") {
dict.name = dict.path.replace(/(.+)(\\|\/)(.+)\.txt/g, "$3");
Expand Down
32 changes: 11 additions & 21 deletions pkg/matcher/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,28 @@ import (
"unicode/utf8"
)

type codePos struct {
code string
pos int
}

type Single struct {
dict map[rune]*struct {
code string
pos int
}
dict map[rune]*codePos
}

func NewSingle() *Single {
s := new(Single)
s.dict = make(map[rune]*struct {
code string
pos int
}, 1024)
s.dict = make(map[rune]*codePos, 1024)
return s
}

func (s *Single) Insert(word, code string, pos int) {
char, _ := utf8.DecodeRuneInString(word)
cp, ok := s.dict[char]
if ok {
if len(cp.code) < len(code) {
// 同一个字取码长较短的
s.dict[char].pos = pos
if _, ok := s.dict[char]; !ok {
s.dict[char] = &codePos{
code: code,
pos: pos,
}
return
}
s.dict[char] = &struct {
code string
pos int
}{
code: code,
pos: pos,
}
}

Expand Down

0 comments on commit d66f50e

Please sign in to comment.