Files
services/sentiment/handler/sentiment.go
2021-02-27 21:45:31 +00:00

35 lines
705 B
Go

package handler
import (
"context"
"github.com/cdipaolo/sentiment"
"github.com/micro/micro/v3/service/errors"
pb "github.com/micro/services/sentiment/proto"
)
type Sentiment struct {
Model *sentiment.Models
}
func (e *Sentiment) Analyze(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
if len(req.Text) == 0 {
return errors.BadRequest("sentiment.analyze", "text is blank")
}
if len(req.Lang) == 0 {
req.Lang = "english"
}
if req.Lang != "english" {
return errors.BadRequest("sentiment.analyze", "only support english")
}
an := e.Model.SentimentAnalysis(req.Text, sentiment.English)
rsp.Score = float64(an.Score)
// TODO: more complex word scoring
return nil
}