add sentiment analaysis mvp

This commit is contained in:
Asim Aslam
2021-02-27 21:45:31 +00:00
parent f800662806
commit 5c3b835c9f
13 changed files with 891 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
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
}