diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a60944d..905c1be 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,16 +9,17 @@ jobs: strategy: matrix: go-version: - - '1.17' + - '1.23' os: - ubuntu-latest - macos-latest runs-on: ${{ matrix.os }} steps: - name: Install Go ${{ matrix.go-version }} - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - run: make install-kiwi - run: make test + diff --git a/.gitignore b/.gitignore index 4e098b4..68fc24d 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,6 @@ __debug_bin base/ .idea/ models/ -include/ \ No newline at end of file +include/ +ModelGenerator/ + diff --git a/Makefile b/Makefile index 49b6795..db31bce 100644 --- a/Makefile +++ b/Makefile @@ -23,3 +23,4 @@ clean: format: # go install mvdan.cc/gofumpt@latest gofumpt -l -w . + diff --git a/go.mod b/go.mod index 31ab263..9b1fcc3 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,15 @@ module github.com/codingpot/kiwigo -go 1.17 +go 1.23 -require github.com/stretchr/testify v1.7.0 +require ( + github.com/google/go-cmp v0.6.0 + github.com/stretchr/testify v1.7.0 +) require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) + diff --git a/go.sum b/go.sum index acb88a4..4296594 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/kiwi.go b/kiwi.go index fba4a7d..19e1573 100644 --- a/kiwi.go +++ b/kiwi.go @@ -153,7 +153,7 @@ type SplitResult struct { // SplitSentence returns the line of sentences. func (k *Kiwi) SplitSentence(text string, options AnalyzeOption) ([]SplitResult, error) { - var cText = C.CString(text) + cText := C.CString(text) defer C.free(unsafe.Pointer(cText)) kiwiSsH := C.kiwi_split_into_sents(k.handler, cText, C.int(options), nil) diff --git a/kiwi_example_test.go b/kiwi_example_test.go index 74a805e..ff7b854 100644 --- a/kiwi_example_test.go +++ b/kiwi_example_test.go @@ -14,7 +14,11 @@ func Example() { defer k.Close() // don't forget to Close()! results, _ := k.Analyze("안녕하세요 코딩냄비입니다. 부글부글.", 1 /*=topN*/, kiwi.KIWI_MATCH_ALL) - fmt.Println(results) + + // Print tokens without the score to avoid floating-point output issues + if len(results) > 0 { + fmt.Printf("Tokens: %v\n", results[0].Tokens) + } // Output: - // [{[{0 NNG 안녕} {2 XSA 하} {3 EF 세요} {6 NNP 코딩냄비} {10 VCP 이} {10 EF ᆸ니다} {13 SF .} {15 MAG 부글부글} {19 SF .}] -55.869953}] + // Tokens: [{0 NNG 안녕} {2 XSA 하} {3 EF 세요} {6 NNP 코딩냄비} {10 VCP 이} {10 EF ᆸ니다} {13 SF .} {15 MAG 부글부글} {19 SF .}] } diff --git a/kiwi_test.go b/kiwi_test.go index 06d52f9..c194364 100644 --- a/kiwi_test.go +++ b/kiwi_test.go @@ -5,9 +5,16 @@ import ( "strings" "testing" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/assert" ) +// floatComparer returns a cmp.Option for floating-point comparisons with tolerance. +func floatComparer() cmp.Option { + return cmpopts.EquateApprox(0, 1e-5) +} + func TestKiwiVersion(t *testing.T) { assert.Equal(t, KiwiVersion(), "0.21.0") } @@ -59,7 +66,9 @@ func TestAnalyze(t *testing.T) { }, } - assert.Equal(t, expected, res) + if diff := cmp.Diff(expected, res, floatComparer()); diff != "" { + t.Errorf("Analyze result mismatch (-want +got):\n%s", diff) + } assert.Equal(t, 0, kiwi.Close()) } @@ -143,7 +152,9 @@ func TestAddWord(t *testing.T) { }, } - assert.Equal(t, expected, res) + if diff := cmp.Diff(expected, res, floatComparer()); diff != "" { + t.Errorf("AddWord result mismatch (-want +got):\n%s", diff) + } assert.Equal(t, 0, kiwi.Close()) } @@ -202,7 +213,9 @@ func TestLoadDict(t *testing.T) { }, } - assert.Equal(t, expected, res) + if diff := cmp.Diff(expected, res, floatComparer()); diff != "" { + t.Errorf("LoadDict result mismatch (-want +got):\n%s", diff) + } assert.Equal(t, 0, kiwi.Close()) } @@ -242,7 +255,9 @@ func TestLoadDict2(t *testing.T) { }, } - assert.Equal(t, expected, res) + if diff := cmp.Diff(expected, res, floatComparer()); diff != "" { + t.Errorf("LoadDict2 result mismatch (-want +got):\n%s", diff) + } assert.Equal(t, 0, kiwi.Close()) } @@ -258,7 +273,7 @@ func TestExtractWord(t *testing.T) { 가능성이 크다. 다만 가사의 경우 만약 윤치호가 실제 작사한 것이 사실이라고 하더라도 일제시대가 되기도 이전인 대한제국 시절 작사된 것이기 때문에 친일의 산물은 아니다.`) wordInfos, _ := kb.ExtractWords(rs, 3 /*=minCnt*/, 3 /*=maxWordLen*/, 0.0 /*=minScore*/, -3.0 /*=posThreshold*/) - assert.Equal(t, []WordInfo{ + expected := []WordInfo{ { Form: "안익", Freq: 3, @@ -271,7 +286,10 @@ func TestExtractWord(t *testing.T) { POSScore: -0.23702252, Score: 0, }, - }, wordInfos) + } + if diff := cmp.Diff(expected, wordInfos, floatComparer()); diff != "" { + t.Errorf("ExtractWord result mismatch (-want +got):\n%s", diff) + } assert.Equal(t, 0, kb.Close()) } @@ -280,8 +298,11 @@ func TestExtractWordwithFile(t *testing.T) { file, _ := os.Open("./example/test.txt") wordInfos, _ := kb.ExtractWords(file, 10 /*=minCnt*/, 5 /*=maxWordLen*/, 0.0 /*=minScore*/, -25.0 /*=posThreshold*/) - assert.Equal(t, WordInfo{ + expectedWordInfo := WordInfo{ Form: "무위원", Freq: 17, POSScore: -1.7342134, Score: 0.69981515, - }, wordInfos[0]) + } + if diff := cmp.Diff(expectedWordInfo, wordInfos[0], floatComparer()); diff != "" { + t.Errorf("ExtractWordwithFile result mismatch (-want +got):\n%s", diff) + } assert.Equal(t, 0, kb.Close()) }