Skip to content

Commit

Permalink
make temp files for test;
Browse files Browse the repository at this point in the history
  • Loading branch information
MapoMagpie committed Feb 8, 2024
1 parent a93f5d4 commit 0d946d7
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 132 deletions.
75 changes: 43 additions & 32 deletions dict/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"log"
"strings"
"time"

"github.com/junegunn/fzf/src/util"
Expand Down Expand Up @@ -152,41 +153,42 @@ func (e *Entry) WriteLine() []byte {
return bs
}

// ParseInput \n
// "你\t好" > "你", "好", ""
// "你 好" > "你", "好", ""
// "你 好" > "你", "好", ""
// "你\t 好" > "你", "好", ""
// "你 好\t 1" > "你", "好", "1"
// "你好 nau 1" > "你好", "nau", "1"
// "nau 你好 1" > "你好", "nau", "1"
// " nau 你好 1 " > "你好", "nau", "1"
func ParseInput(raw string) [3]string {
pair := [3]string{}
for j, l, i := 0, 0, 0; i <= len(raw); i++ {
if i == len(raw) || raw[i] == '\t' || raw[i] == ' ' {
if l == i {
l = i + 1
continue
// Parse input string to a pair of strings
// 0: 表(汉字) 1: 码(字母) 2: 权重
// 支持乱序输入,如 "你好 nau 1" 或 "nau 1 你好"
func ParseInput(raw string) (pair [3]string) {
pair = [3]string{}
// split by '\t' or ' '
splits := strings.Fields(raw)
for i := 0; i < len(splits); i++ {
item := strings.TrimSpace(splits[i])
if len(item) == 0 {
continue
}
if isNumber(item) {
pair[2] = item
continue
}
if isAscii(item) {
pair[1] = item
} else {
space := " "
if pair[0] == "" {
space = ""
}
pair[j] = raw[l:i]
l = i + 1
j++
pair[0] = pair[0] + space + item
}
}
notAsciiIndex := 0
for i, p := range pair {
if !isAscii(p) {
notAsciiIndex = i
break
return
}

func isNumber(str string) bool {
for _, r := range str {
if r < '0' || r > '9' {
return false
}
}
if notAsciiIndex != 0 {
t := pair[notAsciiIndex]
pair[notAsciiIndex] = pair[0]
pair[0] = t
}
return pair
return true
}

func isAscii(str string) bool {
Expand All @@ -198,15 +200,24 @@ func isAscii(str string) bool {
return true
}

// Parse bytes as a couple of strings([]byte) separated by '\t'
// e.g. "你好 nau" > ["你好", "nau"]
// not like ParseInput, this function simply split by '\t'
func ParsePair(raw []byte) [][]byte {
pair := make([][]byte, 0)
for i, j := 0, 0; i < len(raw); i++ {
if raw[i] == '\t' {
pair = append(pair, bytes.TrimSpace(raw[j:i]))
item := bytes.TrimSpace(raw[j:i])
if len(item) > 0 {
pair = append(pair, item)
}
j = i + 1
}
if i == len(raw)-1 && j <= i {
pair = append(pair, bytes.TrimSpace(raw[j:]))
item := bytes.TrimSpace(raw[j:])
if len(item) > 0 {
pair = append(pair, item)
}
}
}
return pair
Expand Down
119 changes: 70 additions & 49 deletions dict/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,60 @@ import (
"context"
"fmt"
"reflect"
"sort"
"strings"
"testing"
)

func TestDictionary_Search(t *testing.T) {
func Test_Dictionary_Search(t *testing.T) {
type args struct {
key []rune
fes []*FileEntries
}
fes1 := &FileEntries{
Entries: []*Entry{
NewEntry([]byte("helle world"), "", 0, 0),
NewEntry([]byte("hi, did eve alive?"), "", 0, 0),
NewEntry([]byte("你好"), "", 0, 0),
NewEntry([]byte("helle world"), "", 1, 0),
NewEntry([]byte("hi, did eve alive?"), "", 2, 0),
NewEntry([]byte("你好"), "", 3, 0),
},
}
fes2 := LoadItems("../rime/xkjd/xkjd6.dict.yaml")
fmt.Println(len(fes2))
tests := []struct {
name string
args args
want []*Entry
}{
{"3", args{[]rune("wor"), []*FileEntries{fes1}}},
{"1", args{[]rune("hel"), []*FileEntries{fes1}}},
{"2", args{[]rune("你"), []*FileEntries{fes1}}},
{"load", args{[]rune("hmxa"), fes2}},
{"case1", args{[]rune("wor"), []*FileEntries{fes1}}, []*Entry{fes1.Entries[0]}},
{"case2", args{[]rune("hel"), []*FileEntries{fes1}}, []*Entry{fes1.Entries[0], fes1.Entries[1]}},
{"case3", args{[]rune("你"), []*FileEntries{fes1}}, []*Entry{fes1.Entries[2]}},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
dict := NewDictionary(tt.args.fes, &CacheMatcher{})
ctx := context.Background()
ch := make(chan []*MatchResult)
fmt.Println("searching for", string(tt.args.key))
go dict.Search(tt.args.key, ch, ctx)
go func() {
dict.Search(tt.args.key, ch, ctx)
close(ch)
}()
for ret := range ch {
fmt.Println(ret)
fmt.Println("ret", ret)
entries := make([]*Entry, 0)
for _, r := range ret {
entries = append(entries, r.Entry)
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].seek < entries[j].seek
})
if !reflect.DeepEqual(entries, tt.want) {
t.Errorf("Search() = %v, want %v", entries, tt.want)
}
}
})
}
}

func TestParseInput(t *testing.T) {
func Test_ParseInput(t *testing.T) {
type args struct {
raw string
}
Expand All @@ -53,15 +66,19 @@ func TestParseInput(t *testing.T) {
args args
want [3]string
}{
{"1", args{"你\t好"}, [3]string{"你", "好", ""}},
{"1", args{"你 好"}, [3]string{"你", "好", ""}},
{"1", args{"你 好"}, [3]string{"你", "好", ""}},
{"1", args{"你\t 好"}, [3]string{"你", "好", ""}},
{"1", args{"你 好\t 1"}, [3]string{"你", "好", "1"}},
{"1", args{"你好 nau 1"}, [3]string{"你好", "nau", "1"}},
{"1", args{"nau 你好 1"}, [3]string{"你好", "nau", "1"}},
{"1", args{" nau 你好 1 "}, [3]string{"你好", "nau", "1"}},
{"case1", args{"你\t好"}, [3]string{"你 好", "", ""}},
{"case2", args{"你 好"}, [3]string{"你 好", "", ""}},
{"case3", args{"你 好"}, [3]string{"你 好", "", ""}},
{"case4", args{"你\t 好"}, [3]string{"你 好", "", ""}},
{"case5", args{"你 好\t 1"}, [3]string{"你 好", "", "1"}},
{"case6", args{"你好 nau 1"}, [3]string{"你好", "nau", "1"}},
{"case7", args{"nau 你好 1"}, [3]string{"你好", "nau", "1"}},
{"case8", args{" nau 你好 1 "}, [3]string{"你好", "nau", "1"}},
{"case9", args{"nau hi你好ya 1 "}, [3]string{"hi你好ya", "nau", "1"}},
{"case10", args{"nau hi 你好 ya 1i "}, [3]string{"你好", "1i", ""}},
}
fields := strings.Fields("你\t好")
fmt.Println(fields, len(fields))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseInput(tt.args.raw); !reflect.DeepEqual(got, tt.want) {
Expand All @@ -71,55 +88,59 @@ func TestParseInput(t *testing.T) {
}
}

func TestParsePair(t *testing.T) {
func Test_ParsePair(t *testing.T) {
type args struct {
raw []byte
raw string
}
tests := []struct {
name string
args args
want [][]byte
want []string
}{
{
name: "case1",
args: args{
[]byte("你好 nau"),
},
want: [][]byte{
[]byte("你好"),
[]byte("nau"),
},
args: args{"你好 nau"},
want: []string{"你好", "nau"},
},
{
name: "case2",
args: args{
[]byte("你好\t\n"),
},
want: [][]byte{
[]byte("你好"),
},
args: args{"你好\t\n"},
want: []string{"你好"},
},
{
name: "case3",
args: args{
[]byte("你好 nau"),
},
want: [][]byte{
[]byte("你好 nau"),
},
args: args{"你好 nau"},
want: []string{"你好 nau"},
},
{
name: "case4",
args: args{
[]byte(" "),
},
want: [][]byte{},
args: args{" "},
want: []string{},
},
{
name: "case5",
args: args{"你 好 nau 1"},
want: []string{"你 好 nau 1"},
},
{
name: "case6",
args: args{"你 好\tnau\t1"},
want: []string{"你 好", "nau", "1"},
},
{
name: "case7",
args: args{"你 好\t \tnau \t1"},
want: []string{"你 好", "nau", "1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParsePair(tt.args.raw); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParsePair() = %v, want %v", got, tt.want)
want := make([][]byte, len(tt.want))
for i, s := range tt.want {
want[i] = []byte(s)
}
if got := ParsePair([]byte(tt.args.raw)); !reflect.DeepEqual(got, want) {
t.Errorf("ParsePair() = %v, want %v", got, want)
}
})
}
Expand Down
Loading

0 comments on commit 0d946d7

Please sign in to comment.