diff --git a/cmd/server/demo.go b/cmd/server/demo.go index d022d74..45247bd 100644 --- a/cmd/server/demo.go +++ b/cmd/server/demo.go @@ -7,12 +7,10 @@ import ( "errors" "fmt" "html/template" - "io" "net/http" - "os/exec" "strings" - "time" + "github.com/goccy/go-graphviz" "github.com/ikawaha/kagome/v2/tokenizer" ) @@ -27,11 +25,6 @@ var ( demoT = template.Must(template.New("demo").Parse(demoHTML)) ) -const ( - graphvizCmd = "dot" - cmdTimeout = 25 * time.Second -) - // TokenizeDemoHandler represents the tokenizer demo server struct. type TokenizeDemoHandler struct { tokenizer *tokenizer.Tokenizer @@ -80,31 +73,21 @@ func toRecords(tokens []tokenizer.Token) []record { } func (h *TokenizeDemoHandler) analyzeGraph(ctx context.Context, sen string, mode tokenizer.TokenizeMode) (records []record, svg string, err error) { - if _, err := exec.LookPath(graphvizCmd); err != nil { - return nil, "", errors.New("circo/graphviz is not installed in your $PATH") - } - ctx, cancel := context.WithTimeout(ctx, cmdTimeout) - defer cancel() var b bytes.Buffer - cmd := exec.CommandContext(ctx, graphvizCmd, "-Tsvg") - r0, w0 := io.Pipe() - cmd.Stdin = r0 - cmd.Stdout = &b - cmd.Stderr = Stderr - if err := cmd.Start(); err != nil { - return nil, "", fmt.Errorf("process done with error, %w", err) + tokens := h.tokenizer.AnalyzeGraph(&b, sen, mode) + graph, err := graphviz.ParseBytes(b.Bytes()) + if err != nil { + return nil, "", err } - tokens := h.tokenizer.AnalyzeGraph(w0, sen, mode) - if err := w0.Close(); err != nil { - return nil, "", fmt.Errorf("pipe close error, %w", err) + g, err := graphviz.New(ctx) + if err != nil { + return nil, "", err } - if err := cmd.Wait(); err != nil { - return nil, "", fmt.Errorf("process done with error, %w", err) + b.Reset() + if err := g.Render(ctx, graph, graphviz.SVG, &b); err != nil { + return nil, "", fmt.Errorf("render error: %w", err) } svg = b.String() - if pos := strings.Index(svg, " 0 { - svg = svg[pos:] - } records = toRecords(tokens) return records, svg, nil } diff --git a/cmd/server/demo_test.go b/cmd/server/demo_test.go index 331d718..9917995 100644 --- a/cmd/server/demo_test.go +++ b/cmd/server/demo_test.go @@ -6,7 +6,6 @@ import ( "io" "net/http" "net/http/httptest" - "os/exec" "reflect" "strings" "testing" @@ -60,9 +59,6 @@ func TestTokenizeDemoHandler_ServeHTTP(t *testing.T) { }) t.Run("w/ lattice", func(t *testing.T) { - if _, err := exec.LookPath(graphvizCmd); err != nil { - t.Skipf("graphviz command not found, %v", err) - } req := httptest.NewRequest(http.MethodPost, `/?s=ねこです&r=Search&lattice=true`, nil) w := httptest.NewRecorder() (&TokenizeDemoHandler{tokenizer: tnz}).ServeHTTP(w, req) @@ -86,9 +82,6 @@ func TestTokenizeDemoHandler_ServeHTTP(t *testing.T) { } func TestTokenizeDemoHandler_analyzeGraph(t *testing.T) { - if _, err := exec.LookPath(graphvizCmd); err != nil { - t.Skipf("graphviz command not found, %v", err) - } tnz, err := tokenizer.New(loadTestDict(t)) if err != nil { t.Fatalf("unexpected error, %v", err) @@ -116,11 +109,8 @@ func TestTokenizeDemoHandler_analyzeGraph(t *testing.T) { }; !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %v", got, want) } - if !strings.HasPrefix(svg, "