moderator

package module
v0.0.0-...-0ba2d5d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

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.

Go Reference

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
	}
	
	// Takes text as input parameters
	// Second parameter is translate if the text is not in EN 
	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
            }
        ]
    }
}

Documentation

Overview

Package provides 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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Duration         int64                                    `json:"duration"`
	MessageID        string                                   `json:"message_id"`
	Text             string                                   `json:"text"`
	TranslatedText   string                                   `json:"translated_text"`
	DetectedLanguage string                                   `json:"detected_language`
	Sentiment        *comprehend.DetectSentimentOutput        `json:"sentiment"`
	PII              *comprehend.DetectPiiEntitiesOutput      `json:"pii"`
	Languages        *comprehend.DetectDominantLanguageOutput `json:"languages"`
	Entities         *comprehend.DetectEntitiesOutput         `json:"entities"`
	KeyPhrases       *comprehend.DetectKeyPhrasesOutput       `json:"key_phrases"`
	Syntax           *comprehend.DetectSyntaxOutput           `json:"syntax"`
}

type Moderator

type Moderator struct {
	Client     comprehendiface.ComprehendAPI
	Translator *translate.Translate
	Language   *string
	Messages   chan string
}

func New

func New(lang string) Moderator

func (*Moderator) Analyze

func (m *Moderator) Analyze(text string, translateText bool) Message

func (*Moderator) GetEntities

func (m *Moderator) GetEntities(text string) (*comprehend.DetectEntitiesOutput, error)

Detect Entities — Detect textual references to the names of people, places, and items as well as references to dates and quantities. PERSON | LOCATION | ORGANIZATION | COMMERCIAL_ITEM | EVENT | DATE | QUANTITY | TITLE | OTHER

func (*Moderator) GetKeyPhrases

func (m *Moderator) GetKeyPhrases(text string) (*comprehend.DetectKeyPhrasesOutput, error)

Detect Key Phrases — Find key phrases such as "good morning" in a document or set of documents.

func (*Moderator) GetLanguages

Detect the Dominant Language — Examine text to determine the dominant language.

func (*Moderator) GetPII

Detect Personally Identifiable Information (PII) — Analyze documents to detect personal data that could be used to identify an individual, such as an address, bank account number, or phone number.

func (*Moderator) GetSentiment

func (m *Moderator) GetSentiment(text string) (*comprehend.DetectSentimentOutput, error)

Determine Sentiment — Analyze documents and determine the dominant sentiment of the text.

func (*Moderator) GetSyntax

func (m *Moderator) GetSyntax(text string) (*comprehend.DetectSyntaxOutput, error)

Analyze Syntax — Parse the words in your text and show the speech syntax for each word and enable you to understand the content of the document.

Directories

Path Synopsis
A sample example to interact with the moderator package
A sample example to interact with the moderator package

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL