Skip to content

Commit 4da4fa7

Browse files
committed
update Golang example
1 parent 894313f commit 4da4fa7

File tree

9 files changed

+58
-7
lines changed

9 files changed

+58
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The following results are for 123 unique utf-8 Bible text files in 23 languages
1919
5. Elixir 1.13.2 = 7.82s
2020
6. Ruby 3.1.0 = 8.31s
2121

22-
Golang 1.17 = UNDER REFACTORING, stay tuned
22+
Golang 1.17.6 = UNDER REFACTORING, stay tuned
2323
</pre>
2424

2525
### Conclusion

example-golang/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/coverage.out

example-golang/Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ build:
88
run: build
99
./${BINARY_NAME}
1010

11+
run-sort: build
12+
./${BINARY_NAME} -n 10 -s
13+
1114
test:
15+
@go test ./... -v
16+
17+
coverage:
1218
@go test ./... -v -coverprofile=coverage.out
13-
19+
20+
1421
cover: test
1522
@go tool cover -html=coverage.out
1623

1724
clean:
1825
@go clean
1926
rm -f coverage.out
2027
rm -f ./${BINARY_NAME}
21-
rm -rf ./words
28+
rm -rf ./words
29+

example-golang/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44

55
```
66
make build
7-
./main [-n=NUMBER_OF_WORKERS, integer] [-s]
7+
./main -n 8
88
```
9+
10+
<pre>
11+
Usage of ./main:
12+
-n int
13+
Number of workers to run (zero to match the number of available CPUs) (default 10)
14+
-s Sort results
15+
</pre>

example-golang/app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Run(srcDir, outDir string, numWorkers int, sortResults bool) error {
4141
}
4242

4343
src := file[:len(file)-3] + "txt"
44-
dst := filepath.Join(outDir, "extracted-words-for-"+spec.Code+".txt")
44+
dst := filepath.Join(outDir, spec.Lang+"-"+spec.Code+".txt")
4545

4646
wg.Add(1)
4747
go extract(src, dst, "POLISH_CI", sortResults, sem, &wg)

example-golang/app/extract.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,17 @@ func writeResults(w io.Writer, words []string) error {
196196

197197
return nil
198198
}
199+
200+
func ExtractUniqueWords(content string, lang string) ([]string, error) {
201+
r := strings.NewReader(content)
202+
words, err := collectWords(r)
203+
if err != nil {
204+
_, _ = fmt.Fprintf(os.Stderr, `collectWords error: %s`, err)
205+
return nil, err
206+
}
207+
less := collate.IndexString(lang)
208+
sort.Slice(words, func(i, j int) bool {
209+
return less(words[i], words[j])
210+
})
211+
return words, nil
212+
}

example-golang/app_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
"wordextractor/app"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func Test_ExtractUniqueWords(t *testing.T) {
13+
text := "ćma cześć ser. śmiech!żółw zebra-łuk len Ćma Żółw ser"
14+
expected := []string{"cześć", "ćma", "len", "łuk", "ser", "śmiech", "zebra", "żółw"}
15+
given, err := app.ExtractUniqueWords(text, "pl")
16+
if err != nil {
17+
_, _ = fmt.Fprintf(os.Stderr, `ExtractUniqueWords error: %s`, err)
18+
return
19+
}
20+
assert.Equal(t, expected, given, "text should be tokenized into unique words")
21+
}

example-golang/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/hipertracker/word_extractor
1+
module wordextractor
22

33
go 1.17
44

example-golang/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"runtime"
99
"time"
1010

11-
"github.com/hipertracker/word_extractor/app"
11+
"wordextractor/app"
1212
)
1313

1414
const (

0 commit comments

Comments
 (0)