moderator
Package to provide easy integration with AWS Comprehend APIs.
Amazon Comprehend is a platform within Amazon Web Services (AWS) that uses machine learning to find insights in unstructured text. You can either use pre-trained text analysis models or customize your own to extract specific pieces of information, identify sentiment, and find topics in a collection of documents.
Basic Usage
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
moderator "github.com/vdparikhrh/moderator"
)
type Server struct {
client moderator.Moderator
}
type Request struct {
Text string `json:"text"`
}
func main() {
var s Server
s.client = moderator.New("en")
http.HandleFunc("/", s.AnalyzeText)
fmt.Printf("Starting server for text analysis...\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
func (s *Server) AnalyzeText(w http.ResponseWriter, r *http.Request) {
var request Request
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "Invalid Body", http.StatusInternalServerError)
return
}
msg := s.client.Analyze(request.Text, true)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(msg)
}
Sample Request
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"text": "Hello Vishal Parikh, I am John. Your AnyCompany Financial Services, LLC credit card account 1111-0000-1111-0008 has a minimum payment of $24.53"
}'
Sample Response
{
"MessageID": "459ad748-789b-4f3b-909d-716878948bff",
"text": "Hello Vishal Parikh, I am John. Your AnyCompany Financial Services, LLC credit card account 1111-0000-1111-0008 has a minimum payment of $24.53",
"pii": {
"Entities": [
{
"BeginOffset": 6,
"EndOffset": 19,
"Score": 0.9999790787696838,
"Type": "NAME"
},
{
"BeginOffset": 26,
"EndOffset": 30,
"Score": 0.9999895095825195,
"Type": "NAME"
},
{
"BeginOffset": 92,
"EndOffset": 111,
"Score": 0.9990487694740295,
"Type": "BANK_ACCOUNT_NUMBER"
}
]
},
"languages": {
"Languages": [
{
"LanguageCode": "en",
"Score": 0.9806605577468872
}
]
},
"sentiment": {
"Sentiment": "NEUTRAL",
"SentimentScore": {
"Mixed": 0.0000050544572332000826,
"Negative": 0.0004280143475625664,
"Neutral": 0.9985914826393127,
"Positive": 0.0009754511411301792
}
},
"entities": {
"Entities": [
{
"BeginOffset": 6,
"EndOffset": 19,
"Score": 0.9996585845947266,
"Text": "Vishal Parikh",
"Type": "PERSON"
},
{
"BeginOffset": 26,
"EndOffset": 30,
"Score": 0.9987456798553467,
"Text": "John",
"Type": "PERSON"
},
{
"BeginOffset": 37,
"EndOffset": 71,
"Score": 0.9970827698707581,
"Text": "AnyCompany Financial Services, LLC",
"Type": "ORGANIZATION"
},
{
"BeginOffset": 92,
"EndOffset": 111,
"Score": 0.9832412004470825,
"Text": "1111-0000-1111-0008",
"Type": "OTHER"
},
{
"BeginOffset": 137,
"EndOffset": 143,
"Score": 0.9973769783973694,
"Text": "$24.53",
"Type": "QUANTITY"
}
]
},
"key_phrases": {
"KeyPhrases": [
{
"BeginOffset": 6,
"EndOffset": 19,
"Score": 0.9719115495681763,
"Text": "Vishal Parikh"
},
{
"BeginOffset": 26,
"EndOffset": 30,
"Score": 0.9995399713516235,
"Text": "John"
},
{
"BeginOffset": 32,
"EndOffset": 66,
"Score": 0.9826791286468506,
"Text": "Your AnyCompany Financial Services"
},
{
"BeginOffset": 68,
"EndOffset": 111,
"Score": 0.8073769211769104,
"Text": "LLC credit card account 1111-0000-1111-0008"
},
{
"BeginOffset": 116,
"EndOffset": 133,
"Score": 0.9997748732566833,
"Text": "a minimum payment"
}
]
},
"syntax": {
"SyntaxTokens": [
{
"BeginOffset": 0,
"EndOffset": 5,
"PartOfSpeech": {
"Score": 0.9827689528465271,
"Tag": "INTJ"
},
"Text": "Hello",
"TokenId": 1
},
{
"BeginOffset": 6,
"EndOffset": 12,
"PartOfSpeech": {
"Score": 0.9977828860282898,
"Tag": "PROPN"
},
"Text": "Vishal",
"TokenId": 2
},
{
"BeginOffset": 13,
"EndOffset": 19,
"PartOfSpeech": {
"Score": 0.9993051290512085,
"Tag": "PROPN"
},
"Text": "Parikh",
"TokenId": 3
},
{
"BeginOffset": 19,
"EndOffset": 20,
"PartOfSpeech": {
"Score": 0.9999982118606567,
"Tag": "PUNCT"
},
"Text": ",",
"TokenId": 4
},
{
"BeginOffset": 21,
"EndOffset": 22,
"PartOfSpeech": {
"Score": 0.9997889399528503,
"Tag": "PRON"
},
"Text": "I",
"TokenId": 5
},
{
"BeginOffset": 23,
"EndOffset": 25,
"PartOfSpeech": {
"Score": 0.9863438606262207,
"Tag": "VERB"
},
"Text": "am",
"TokenId": 6
},
{
"BeginOffset": 26,
"EndOffset": 30,
"PartOfSpeech": {
"Score": 0.9998950958251953,
"Tag": "PROPN"
},
"Text": "John",
"TokenId": 7
},
{
"BeginOffset": 30,
"EndOffset": 31,
"PartOfSpeech": {
"Score": 0.9999862909317017,
"Tag": "PUNCT"
},
"Text": ".",
"TokenId": 8
},
{
"BeginOffset": 32,
"EndOffset": 36,
"PartOfSpeech": {
"Score": 0.9985802173614502,
"Tag": "PRON"
},
"Text": "Your",
"TokenId": 9
},
{
"BeginOffset": 37,
"EndOffset": 47,
"PartOfSpeech": {
"Score": 0.9924488067626953,
"Tag": "PROPN"
},
"Text": "AnyCompany",
"TokenId": 10
},
{
"BeginOffset": 48,
"EndOffset": 57,
"PartOfSpeech": {
"Score": 0.9987910389900208,
"Tag": "PROPN"
},
"Text": "Financial",
"TokenId": 11
},
{
"BeginOffset": 58,
"EndOffset": 66,
"PartOfSpeech": {
"Score": 0.9981212019920349,
"Tag": "PROPN"
},
"Text": "Services",
"TokenId": 12
},
{
"BeginOffset": 66,
"EndOffset": 67,
"PartOfSpeech": {
"Score": 0.9999691247940063,
"Tag": "PUNCT"
},
"Text": ",",
"TokenId": 13
},
{
"BeginOffset": 68,
"EndOffset": 71,
"PartOfSpeech": {
"Score": 0.9854297637939453,
"Tag": "PROPN"
},
"Text": "LLC",
"TokenId": 14
},
{
"BeginOffset": 72,
"EndOffset": 78,
"PartOfSpeech": {
"Score": 0.9992117881774902,
"Tag": "NOUN"
},
"Text": "credit",
"TokenId": 15
},
{
"BeginOffset": 79,
"EndOffset": 83,
"PartOfSpeech": {
"Score": 0.9995114803314209,
"Tag": "NOUN"
},
"Text": "card",
"TokenId": 16
},
{
"BeginOffset": 84,
"EndOffset": 91,
"PartOfSpeech": {
"Score": 0.8321436643600464,
"Tag": "NOUN"
},
"Text": "account",
"TokenId": 17
},
{
"BeginOffset": 92,
"EndOffset": 111,
"PartOfSpeech": {
"Score": 0.9857684969902039,
"Tag": "NUM"
},
"Text": "1111-0000-1111-0008",
"TokenId": 18
},
{
"BeginOffset": 112,
"EndOffset": 115,
"PartOfSpeech": {
"Score": 0.990443766117096,
"Tag": "VERB"
},
"Text": "has",
"TokenId": 19
},
{
"BeginOffset": 116,
"EndOffset": 117,
"PartOfSpeech": {
"Score": 0.9999935626983643,
"Tag": "DET"
},
"Text": "a",
"TokenId": 20
},
{
"BeginOffset": 118,
"EndOffset": 125,
"PartOfSpeech": {
"Score": 0.8807594180107117,
"Tag": "ADJ"
},
"Text": "minimum",
"TokenId": 21
},
{
"BeginOffset": 126,
"EndOffset": 133,
"PartOfSpeech": {
"Score": 0.9999884366989136,
"Tag": "NOUN"
},
"Text": "payment",
"TokenId": 22
},
{
"BeginOffset": 134,
"EndOffset": 136,
"PartOfSpeech": {
"Score": 0.9999018907546997,
"Tag": "ADP"
},
"Text": "of",
"TokenId": 23
},
{
"BeginOffset": 137,
"EndOffset": 138,
"PartOfSpeech": {
"Score": 0.9999983310699463,
"Tag": "SYM"
},
"Text": "$",
"TokenId": 24
},
{
"BeginOffset": 138,
"EndOffset": 143,
"PartOfSpeech": {
"Score": 0.9999815225601196,
"Tag": "NUM"
},
"Text": "24.53",
"TokenId": 25
}
]
}
}