-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstem.go
70 lines (61 loc) · 1.76 KB
/
stem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2023 Tamás Gulácsi. All rights reserved.
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
kithttp "github.com/go-kit/kit/transport/http"
)
var stemConvertServer = kithttp.NewServer(
stemConvertEP,
stemConvertDecode,
stemConvertEncode,
kithttp.ServerBefore(defaultBeforeFuncs...),
kithttp.ServerAfter(kithttp.SetContentType("application/json")),
)
var _ = stemConvertServer
var errNotImplemented = errors.New("not implemented")
func stemConvertEP(ctx context.Context, request interface{}) (response interface{}, err error) {
_ = stem
return nil, errNotImplemented
}
func stemConvertDecode(ctx context.Context, r *http.Request) (interface{}, error) {
return nil, errNotImplemented
}
func stemConvertEncode(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return errNotImplemented
}
// stem the words in the io.Reader, using the given language, using hunspell.
func stem(ctx context.Context, r io.Reader, language string) ([][2]string, error) {
if language == "" {
return nil, errors.New("language must be set")
}
var buf, errBuf bytes.Buffer
cmd := exec.CommandContext(ctx, "hunspell", "-d", language, "-s")
cmd.Env = append(os.Environ(), "LANG="+language+".UTF-8")
cmd.Stdin = r
cmd.Stdout = &buf
cmd.Stderr = &errBuf
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("%v (%s): %w", cmd.Args, errBuf.String(), err)
}
var res [][2]string
for _, line := range bytes.Split(buf.Bytes(), []byte("\n")) {
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
pre, post, found := bytes.Cut(line, []byte{' '})
if !found {
res = append(res, [2]string{string(line), ""})
continue
}
res = append(res, [2]string{string(pre), string(post)})
}
return res, nil
}