Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ __debug_bin
base/
.idea/
models/
include/
include/
ModelGenerator/

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ clean:
format:
# go install mvdan.cc/gofumpt@latest
gofumpt -l -w .

8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
2 changes: 1 addition & 1 deletion kiwi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions kiwi_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 .}]
}
37 changes: 29 additions & 8 deletions kiwi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}

Expand All @@ -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,
Expand All @@ -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())
}

Expand All @@ -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())
}
Loading