add the sentiment service

This commit is contained in:
Asim Aslam
2021-06-02 11:38:56 +01:00
parent be790b28cc
commit 358de467cd
17 changed files with 513 additions and 0 deletions

2
go.mod
View File

@@ -6,6 +6,8 @@ require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/PuerkitoBio/goquery v1.6.1
github.com/SlyMarbo/rss v1.0.1
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 // indirect
github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10
github.com/disintegration/imaging v1.6.2
github.com/getkin/kin-openapi v0.26.0
github.com/gojuno/go.osrm v0.1.1-0.20200217151037-435fc3e1d3d4

4
go.sum
View File

@@ -61,6 +61,10 @@ github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/caddyserver/certmagic v0.10.6 h1:sCya6FmfaN74oZE46kqfaFOVoROD/mF36rTQfjN7TZc=
github.com/caddyserver/certmagic v0.10.6/go.mod h1:Y8jcUBctgk/IhpAzlHKfimZNyXCkfGgRTC0orl8gROQ=
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk=
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o=
github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 h1:6dGQY3apkf7lG3a1UFhS6grlo009buPFVy79RvNVUF4=
github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10/go.mod h1:JWoVf4GJxCxM3iCiZSVoXNMV+JFG49L+ou70KK3HTvQ=
github.com/cenkalti/backoff/v4 v4.0.0 h1:6VeaLF9aI+MAUQ95106HwWzYZgJJpZ4stumjj6RFYAU=
github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

2
sentiment/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
sentiment

3
sentiment/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD sentiment /sentiment
ENTRYPOINT [ "/sentiment" ]

27
sentiment/Makefile Normal file
View File

@@ -0,0 +1,27 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
.PHONY: proto
proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/sentiment.proto
.PHONY: docs
docs:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/sentiment.proto
@redoc-cli bundle api-sentiment.json
.PHONY: build
build:
go build -o sentiment *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t sentiment:latest

6
sentiment/README.md Normal file
View File

@@ -0,0 +1,6 @@
Real time sentiment analysis
# Sentiment Service
The sentiment service provides rudimentary sentiment analysis on text

12
sentiment/examples.json Normal file
View File

@@ -0,0 +1,12 @@
{
"analyze": [{
"title": "Analyze a piece of text",
"description": "Analyze and score a piece of text",
"request": {
"text": "whoa this is cool"
},
"response": {
"score": 1
}
}]
}

3
sentiment/generate.go Normal file
View File

@@ -0,0 +1,3 @@
package main
//go:generate make proto

View File

@@ -0,0 +1,31 @@
package handler
import (
"context"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/services/sentiment/model"
pb "github.com/micro/services/sentiment/proto"
)
type Sentiment struct{}
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")
}
rsp.Score = model.Analyze(req.Text)
// TODO: more complex word scoring
return nil
}

24
sentiment/main.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"github.com/micro/services/sentiment/handler"
pb "github.com/micro/services/sentiment/proto"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
)
func main() {
// Create service
srv := service.New(
service.Name("sentiment"),
)
// Register handler
pb.RegisterSentimentHandler(srv.Server(), new(handler.Sentiment))
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
sentiment/micro.mu Normal file
View File

@@ -0,0 +1 @@
service sentiment

38
sentiment/model/model.go Normal file
View File

@@ -0,0 +1,38 @@
package model
import (
"github.com/cdipaolo/sentiment"
"github.com/micro/micro/v3/service/logger"
)
var (
model *sentiment.Models
)
func init() {
// load sentiment analysis tool
md, err := sentiment.Restore()
if err != nil {
logger.Fatal(err)
}
model = &md
}
func Analyze(text string) float64 {
an := model.SentimentAnalysis(text, sentiment.English)
// no words, just return whats scored
if len(an.Words) == 0 {
return float64(an.Score)
}
// take each word score then divide by num words
var total float64
for _, word := range an.Words {
total += float64(word.Score)
}
// get the overall score
return total / float64(len(an.Words))
}

View File

@@ -0,0 +1,224 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.15.6
// source: proto/sentiment.proto
package sentiment
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Analyze and score a piece of text
type Request struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// The text to analyze
Text string `protobuf:"bytes,1,opt,name=text,proto3" json:"text,omitempty"`
// The language. Defaults to english.
Lang string `protobuf:"bytes,2,opt,name=lang,proto3" json:"lang,omitempty"`
}
func (x *Request) Reset() {
*x = Request{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_sentiment_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Request) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Request) ProtoMessage() {}
func (x *Request) ProtoReflect() protoreflect.Message {
mi := &file_proto_sentiment_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Request.ProtoReflect.Descriptor instead.
func (*Request) Descriptor() ([]byte, []int) {
return file_proto_sentiment_proto_rawDescGZIP(), []int{0}
}
func (x *Request) GetText() string {
if x != nil {
return x.Text
}
return ""
}
func (x *Request) GetLang() string {
if x != nil {
return x.Lang
}
return ""
}
type Response struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// The score of the text
Score float64 `protobuf:"fixed64,1,opt,name=score,proto3" json:"score,omitempty"`
}
func (x *Response) Reset() {
*x = Response{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_sentiment_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response) ProtoMessage() {}
func (x *Response) ProtoReflect() protoreflect.Message {
mi := &file_proto_sentiment_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response.ProtoReflect.Descriptor instead.
func (*Response) Descriptor() ([]byte, []int) {
return file_proto_sentiment_proto_rawDescGZIP(), []int{1}
}
func (x *Response) GetScore() float64 {
if x != nil {
return x.Score
}
return 0
}
var File_proto_sentiment_proto protoreflect.FileDescriptor
var file_proto_sentiment_proto_rawDesc = []byte{
0x0a, 0x15, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x6e,
0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6d, 0x65,
0x6e, 0x74, 0x22, 0x31, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a,
0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78,
0x74, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x61, 0x6e, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x6c, 0x61, 0x6e, 0x67, 0x22, 0x20, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x01,
0x52, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x32, 0x41, 0x0a, 0x09, 0x53, 0x65, 0x6e, 0x74, 0x69,
0x6d, 0x65, 0x6e, 0x74, 0x12, 0x34, 0x0a, 0x07, 0x41, 0x6e, 0x61, 0x6c, 0x79, 0x7a, 0x65, 0x12,
0x12, 0x2e, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x71, 0x75,
0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x6e, 0x74, 0x2e,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x13, 0x5a, 0x11, 0x2e, 0x2f,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x6e, 0x74, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_sentiment_proto_rawDescOnce sync.Once
file_proto_sentiment_proto_rawDescData = file_proto_sentiment_proto_rawDesc
)
func file_proto_sentiment_proto_rawDescGZIP() []byte {
file_proto_sentiment_proto_rawDescOnce.Do(func() {
file_proto_sentiment_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_sentiment_proto_rawDescData)
})
return file_proto_sentiment_proto_rawDescData
}
var file_proto_sentiment_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_proto_sentiment_proto_goTypes = []interface{}{
(*Request)(nil), // 0: sentiment.Request
(*Response)(nil), // 1: sentiment.Response
}
var file_proto_sentiment_proto_depIdxs = []int32{
0, // 0: sentiment.Sentiment.Analyze:input_type -> sentiment.Request
1, // 1: sentiment.Sentiment.Analyze:output_type -> sentiment.Response
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_proto_sentiment_proto_init() }
func file_proto_sentiment_proto_init() {
if File_proto_sentiment_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_sentiment_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Request); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_sentiment_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_sentiment_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_sentiment_proto_goTypes,
DependencyIndexes: file_proto_sentiment_proto_depIdxs,
MessageInfos: file_proto_sentiment_proto_msgTypes,
}.Build()
File_proto_sentiment_proto = out.File
file_proto_sentiment_proto_rawDesc = nil
file_proto_sentiment_proto_goTypes = nil
file_proto_sentiment_proto_depIdxs = nil
}

View File

@@ -0,0 +1,93 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/sentiment.proto
package sentiment
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Sentiment service
func NewSentimentEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Sentiment service
type SentimentService interface {
Analyze(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error)
}
type sentimentService struct {
c client.Client
name string
}
func NewSentimentService(name string, c client.Client) SentimentService {
return &sentimentService{
c: c,
name: name,
}
}
func (c *sentimentService) Analyze(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error) {
req := c.c.NewRequest(c.name, "Sentiment.Analyze", in)
out := new(Response)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Sentiment service
type SentimentHandler interface {
Analyze(context.Context, *Request, *Response) error
}
func RegisterSentimentHandler(s server.Server, hdlr SentimentHandler, opts ...server.HandlerOption) error {
type sentiment interface {
Analyze(ctx context.Context, in *Request, out *Response) error
}
type Sentiment struct {
sentiment
}
h := &sentimentHandler{hdlr}
return s.Handle(s.NewHandler(&Sentiment{h}, opts...))
}
type sentimentHandler struct {
SentimentHandler
}
func (h *sentimentHandler) Analyze(ctx context.Context, in *Request, out *Response) error {
return h.SentimentHandler.Analyze(ctx, in, out)
}

View File

@@ -0,0 +1,23 @@
syntax = "proto3";
package sentiment;
option go_package = "./proto;sentiment";
service Sentiment {
rpc Analyze(Request) returns (Response) {};
}
// Analyze and score a piece of text
message Request {
// The text to analyze
string text = 1;
// The language. Defaults to english.
string lang = 2;
}
message Response {
// The score of the text
double score = 1;
}

8
sentiment/publicapi.json Normal file
View File

@@ -0,0 +1,8 @@
{
"name": "sentiment",
"icon": "🤔",
"category": "web",
"pricing": {
"Sentiment.Analyze": 100
}
}

12
sentiment/usage.md Normal file
View File

@@ -0,0 +1,12 @@
# Sentiment Service
The sentiment service provides rudimentary sentiment analysis on text
## Usage
```
$ micro sentiment analyze --text "This is great"
{
"score": 1
}
```