Skip to content

Commit

Permalink
Merge pull request #163 from go-ego/en-pr
Browse files Browse the repository at this point in the history
Add: add more examples and Update README.md
  • Loading branch information
vcaesar authored Jan 2, 2023
2 parents bbd1be4 + dd249f2 commit bfd624e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,64 @@ go get -u github.com/go-ego/gse
```go
package main

import (
_ "embed"
"fmt"

"github.com/go-ego/gse"
)

//go:embed testdata/test_en2.txt
var testDict string

//go:embed testdata/test_en.txt
var testEn string

var (
text = "To be or not to be, that's the question!"
test1 = "Hiworld, Helloworld!"
)

func main() {
var seg1 gse.Segmenter
seg1.DictSep = ","
err := seg1.LoadDict("./testdata/test_en.txt")
if err != nil {
fmt.Println("Load dictionary error: ", err)
}

s1 := seg1.Cut(text)
fmt.Println("seg1 Cut: ", s1)
// seg1 Cut: [to be or not to be , that's the question!]

var seg2 gse.Segmenter
seg2.AlphaNum = true
seg2.LoadDict("./testdata/test_en_dict3.txt")

s2 := seg2.Cut(test1)
fmt.Println("seg2 Cut: ", s2)
// seg2 Cut: [hi world , hello world !]

var seg3 gse.Segmenter
seg3.AlphaNum = true
seg3.DictSep = ","
err = seg3.LoadDictEmbed(testDict + "\n" + testEn)
if err != nil {
fmt.Println("loadDictEmbed error: ", err)
}
s3 := seg3.Cut(text + test1)
fmt.Println("seg3 Cut: ", s3)
// seg3 Cut: [to be or not to be , that's the question! hi world , hello world !]

// example2()
}
```

Example2:

```go
package main

import (
"fmt"
"regexp"
Expand Down
46 changes: 46 additions & 0 deletions examples/en/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
package main

import (
_ "embed"
"fmt"

"github.com/go-ego/gse"
)

//go:embed testdata/test_en2.txt
var testDict string

//go:embed testdata/test_en.txt
var testEn string

var (
text = "To be or not to be, that's the question!"
test1 = "Hiworld, Helloworld!"
)

func main() {
var seg1 gse.Segmenter
seg1.DictSep = ","
err := seg1.LoadDict("./testdata/test_en.txt")
if err != nil {
fmt.Println("Load dictionary error: ", err)
}

s1 := seg1.Cut(text)
fmt.Println("seg1 Cut: ", s1)
// seg1 Cut: [to be or not to be , that's the question!]

var seg2 gse.Segmenter
seg2.AlphaNum = true
seg2.LoadDict("./testdata/test_en_dict3.txt")

s2 := seg2.Cut(test1)
fmt.Println("seg2 Cut: ", s2)
// seg2 Cut: [hi world , hello world !]

var seg3 gse.Segmenter
seg3.AlphaNum = true
seg3.DictSep = ","
err = seg3.LoadDictEmbed(testDict + "\n" + testEn)
if err != nil {
fmt.Println("loadDictEmbed error: ", err)
}
s3 := seg3.Cut(text + test1)
fmt.Println("seg3 Cut: ", s3)
// seg3 Cut: [to be or not to be , that's the question! hi world , hello world !]

// example2()
}

func example2() {
seg, err := gse.New("zh,../../testdata/test_en_dict3.txt", "alpha")
fmt.Println("new gse error: ", err)
// var seg gse.Segmenter
Expand Down
4 changes: 4 additions & 0 deletions examples/en/testdata/test_en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
to be, 10, x
or, 10, c
not to be, 5, x
that's the question!, 10, x
3 changes: 3 additions & 0 deletions examples/en/testdata/test_en2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hi, 32
hello, 20
world, 20, n
3 changes: 3 additions & 0 deletions examples/en/testdata/test_en_dict3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hi 32
hello 20
world 20 n
3 changes: 3 additions & 0 deletions testdata/test_en2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hi, 32
hello, 20
world, 20, n

0 comments on commit bfd624e

Please sign in to comment.