Closed dsxack closed 3 years ago
Hi @dsxack, thank you for this report. Unfortunately, I'm not able to reproduce the panic on my development machine running macOS 10.15. Does it occur every time you run the program or just occasionally? Can you give me any other hint about how to reproduce it?
For what it's worth, here is the output of my go env
:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/pemistahl/Library/Caches/go-build"
GOENV="/Users/pemistahl/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/pemistahl/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/pemistahl/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.5/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/pemistahl/Documents/git-repositories/lingua-go/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ld/j3qxrhnn5nq2qk0yjplz2flc0000gn/T/go-build2115905117=/tmp/go-build -gno-record-gcc-switches -fno-common"
Hi @pemistahl I've reproduced it in docker. I will write script and put it here.
@pemistahl script to reproduce with docker:
#!/usr/bin/env bash
# debug bash commands
set -x
# make project dir
mkdir test && cd test;
# write main.go
cat <<EOF > main.go
package main
import (
"fmt"
"github.com/pemistahl/lingua-go"
)
func main() {
languages := []lingua.Language{
lingua.English,
lingua.French,
lingua.German,
lingua.Spanish,
}
detector := lingua.NewLanguageDetectorBuilder().
FromLanguages(languages...).
Build()
confidenceValues := detector.ComputeLanguageConfidenceValues("languages are awesome")
for _, elem := range confidenceValues {
fmt.Printf("%s: %.2f\n", elem.Language(), elem.Value())
}
// Output:
// English: 1.00
// French: 0.79
// German: 0.75
// Spanish: 0.72
}
EOF
# run docker container, init go mod, get dependencies and run.
docker run --rm -it -v $(pwd):/app golang bash -c 'set -x; \
cd /app \
&& go mod init testing \
&& go get github.com/pemistahl/lingua-go \
&& go run main.go'
I think panic happens because you use relative path from current working dir here:
func loadJson(language Language, ngramLength int) []byte {
ngramName := getNgramNameByLength(ngramLength)
isoCode := strings.ToLower(language.IsoCode639_1().String())
zipFilePath := fmt.Sprintf("language-models/%s/%ss.json.zip", isoCode, ngramName) # <=== path that relative from working dir here, not relative module sources
zipFile, _ := zip.OpenReader(zipFilePath)
defer zipFile.Close()
jsonFile, _ := zipFile.File[0].Open()
defer jsonFile.Close()
jsonFileContent, _ := io.ReadAll(jsonFile)
return jsonFileContent
}
I know three options to use resources in go modules:
_, path, _, _ := runtime.Caller(0)
. I think it variant is bad because resources will not be available from compiled binary. This problem also applies to the current code.go:embed
but it requires go 1.16. It is ok because your library already requires go 1.16go:embed
I can prepare MR for both of variants if you want help)
@pemistahl
Does it occur every time you run the program or just occasionally?
It occurs every time. But if I add lingua.Russian
and put phrase языки это круто
into detector.ComputeLanguageConfidenceValues
it works without panic and prints:
Russian: 1.00
Code to reproduce:
go.mod
go env:
Expect: no panics
Actual: