Skip to content

Commit 207e68b

Browse files
authored
Merge pull request #38 from codingpot/fix-floating-point-comparisons
fix(test): use go-cmp for robust floating-point comparisons
2 parents 4684fa7 + dc0cba1 commit 207e68b

File tree

8 files changed

+52
-17
lines changed

8 files changed

+52
-17
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ jobs:
99
strategy:
1010
matrix:
1111
go-version:
12-
- '1.17'
12+
- '1.23'
1313
os:
1414
- ubuntu-latest
1515
- macos-latest
1616
runs-on: ${{ matrix.os }}
1717
steps:
1818
- name: Install Go ${{ matrix.go-version }}
19-
uses: actions/setup-go@v2
19+
uses: actions/setup-go@v5
2020
with:
2121
go-version: ${{ matrix.go-version }}
22-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v4
2323
- run: make install-kiwi
2424
- run: make test
25+

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ __debug_bin
2020
base/
2121
.idea/
2222
models/
23-
include/
23+
include/
24+
ModelGenerator/
25+

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ clean:
2323
format:
2424
# go install mvdan.cc/gofumpt@latest
2525
gofumpt -l -w .
26+

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
module github.com/codingpot/kiwigo
22

3-
go 1.17
3+
go 1.23
44

5-
require github.com/stretchr/testify v1.7.0
5+
require (
6+
github.com/google/go-cmp v0.6.0
7+
github.com/stretchr/testify v1.7.0
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.0 // indirect
912
github.com/pmezard/go-difflib v1.0.0 // indirect
1013
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1114
)
15+

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

kiwi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ type SplitResult struct {
153153

154154
// SplitSentence returns the line of sentences.
155155
func (k *Kiwi) SplitSentence(text string, options AnalyzeOption) ([]SplitResult, error) {
156-
var cText = C.CString(text)
156+
cText := C.CString(text)
157157
defer C.free(unsafe.Pointer(cText))
158158

159159
kiwiSsH := C.kiwi_split_into_sents(k.handler, cText, C.int(options), nil)

kiwi_example_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func Example() {
1414
defer k.Close() // don't forget to Close()!
1515

1616
results, _ := k.Analyze("안녕하세요 코딩냄비입니다. 부글부글.", 1 /*=topN*/, kiwi.KIWI_MATCH_ALL)
17-
fmt.Println(results)
17+
18+
// Print tokens without the score to avoid floating-point output issues
19+
if len(results) > 0 {
20+
fmt.Printf("Tokens: %v\n", results[0].Tokens)
21+
}
1822
// Output:
19-
// [{[{0 NNG 안녕} {2 XSA 하} {3 EF 세요} {6 NNP 코딩냄비} {10 VCP 이} {10 EF ᆸ니다} {13 SF .} {15 MAG 부글부글} {19 SF .}] -55.869953}]
23+
// Tokens: [{0 NNG 안녕} {2 XSA 하} {3 EF 세요} {6 NNP 코딩냄비} {10 VCP 이} {10 EF ᆸ니다} {13 SF .} {15 MAG 부글부글} {19 SF .}]
2024
}

kiwi_test.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/google/go-cmp/cmp"
9+
"github.com/google/go-cmp/cmp/cmpopts"
810
"github.com/stretchr/testify/assert"
911
)
1012

13+
// floatComparer returns a cmp.Option for floating-point comparisons with tolerance.
14+
func floatComparer() cmp.Option {
15+
return cmpopts.EquateApprox(0, 1e-5)
16+
}
17+
1118
func TestKiwiVersion(t *testing.T) {
1219
assert.Equal(t, KiwiVersion(), "0.21.0")
1320
}
@@ -59,7 +66,9 @@ func TestAnalyze(t *testing.T) {
5966
},
6067
}
6168

62-
assert.Equal(t, expected, res)
69+
if diff := cmp.Diff(expected, res, floatComparer()); diff != "" {
70+
t.Errorf("Analyze result mismatch (-want +got):\n%s", diff)
71+
}
6372
assert.Equal(t, 0, kiwi.Close())
6473
}
6574

@@ -143,7 +152,9 @@ func TestAddWord(t *testing.T) {
143152
},
144153
}
145154

146-
assert.Equal(t, expected, res)
155+
if diff := cmp.Diff(expected, res, floatComparer()); diff != "" {
156+
t.Errorf("AddWord result mismatch (-want +got):\n%s", diff)
157+
}
147158
assert.Equal(t, 0, kiwi.Close())
148159
}
149160

@@ -202,7 +213,9 @@ func TestLoadDict(t *testing.T) {
202213
},
203214
}
204215

205-
assert.Equal(t, expected, res)
216+
if diff := cmp.Diff(expected, res, floatComparer()); diff != "" {
217+
t.Errorf("LoadDict result mismatch (-want +got):\n%s", diff)
218+
}
206219
assert.Equal(t, 0, kiwi.Close())
207220
}
208221

@@ -242,7 +255,9 @@ func TestLoadDict2(t *testing.T) {
242255
},
243256
}
244257

245-
assert.Equal(t, expected, res)
258+
if diff := cmp.Diff(expected, res, floatComparer()); diff != "" {
259+
t.Errorf("LoadDict2 result mismatch (-want +got):\n%s", diff)
260+
}
246261
assert.Equal(t, 0, kiwi.Close())
247262
}
248263

@@ -258,7 +273,7 @@ func TestExtractWord(t *testing.T) {
258273
가능성이 크다. 다만 가사의 경우 만약 윤치호가 실제 작사한 것이 사실이라고 하더라도 일제시대가 되기도 이전인 대한제국 시절 작사된 것이기
259274
때문에 친일의 산물은 아니다.`)
260275
wordInfos, _ := kb.ExtractWords(rs, 3 /*=minCnt*/, 3 /*=maxWordLen*/, 0.0 /*=minScore*/, -3.0 /*=posThreshold*/)
261-
assert.Equal(t, []WordInfo{
276+
expected := []WordInfo{
262277
{
263278
Form: "안익",
264279
Freq: 3,
@@ -271,7 +286,10 @@ func TestExtractWord(t *testing.T) {
271286
POSScore: -0.23702252,
272287
Score: 0,
273288
},
274-
}, wordInfos)
289+
}
290+
if diff := cmp.Diff(expected, wordInfos, floatComparer()); diff != "" {
291+
t.Errorf("ExtractWord result mismatch (-want +got):\n%s", diff)
292+
}
275293
assert.Equal(t, 0, kb.Close())
276294
}
277295

@@ -280,8 +298,11 @@ func TestExtractWordwithFile(t *testing.T) {
280298
file, _ := os.Open("./example/test.txt")
281299

282300
wordInfos, _ := kb.ExtractWords(file, 10 /*=minCnt*/, 5 /*=maxWordLen*/, 0.0 /*=minScore*/, -25.0 /*=posThreshold*/)
283-
assert.Equal(t, WordInfo{
301+
expectedWordInfo := WordInfo{
284302
Form: "무위원", Freq: 17, POSScore: -1.7342134, Score: 0.69981515,
285-
}, wordInfos[0])
303+
}
304+
if diff := cmp.Diff(expectedWordInfo, wordInfos[0], floatComparer()); diff != "" {
305+
t.Errorf("ExtractWordwithFile result mismatch (-want +got):\n%s", diff)
306+
}
286307
assert.Equal(t, 0, kb.Close())
287308
}

0 commit comments

Comments
 (0)