mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
add sentiment analaysis mvp
This commit is contained in:
34
sentiment/handler/sentiment.go
Normal file
34
sentiment/handler/sentiment.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user