-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgolac.go
102 lines (83 loc) · 2.03 KB
/
golac.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package golac // import "github.com/vulcangz/golac"
import (
"bytes"
"context"
"errors"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
)
// LocalExec is responsible to run PaddleHub LAC process.
type LocalExec struct {
LacCmd string
LacArgs []string
Command string
ClassPath string
Module string
Option string
ctx context.Context
}
// NewLocalExec returns a pointer of LocalExec
func NewLocalExec(ctx context.Context) *LocalExec {
if ctx == nil {
ctx = context.Background()
}
return &LocalExec{
LacCmd: "hub",
LacArgs: []string{},
Command: "run",
Module: "lac",
Option: "--input_text", // or --input_file
ctx: ctx,
}
}
// Run Use PaddleHub lexical analysis model LAC for word segmentation.
func (c *LocalExec) Run(text string) (response string, err error) {
if text == "" {
err := errors.New("ERROR: The number of values in input file is inconsistent with expectations.")
return "", err
}
// build arguments
args := c.LacArgs
if c.Command != "" {
args = append(args, c.Command)
}
if c.Module != "" {
args = append(args, c.Module)
}
if c.Option == "--input_text" {
args = append(args,
"--input_text",
text,
)
} else if c.Option == "--input_file" {
args = append(args,
"--input_file",
text,
)
} else {
err := errors.New("ERROR: Invalid command option.")
log.Warnf("Failed to execute command. [%s]", err.Error())
return "", err
}
log.Debugf("Run command [%s]", args)
// execute command
cmd := exec.CommandContext(c.ctx, c.LacCmd, args...)
log.Debugf("Run command [%s]", strings.Join(cmd.Args, " "))
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
err := errors.New(err.Error() + ": " + stderr.String())
log.Warnf("Failed to execute command. [%s]", err.Error())
return "", err
}
response = stdout.String()
if stdout.Len() > 0 {
log.Debugf("Success to execute command. [%s]", stdout.String())
} else {
log.Debugf("Success to execute command.")
}
return response, nil
}