Skip to content

Instantly share code, notes, and snippets.

@imranansari
Forked from brydavis/comprehend.go
Created July 23, 2020 19:40
Show Gist options
  • Save imranansari/3c5884aa66c45fc1eeac4d57228d6148 to your computer and use it in GitHub Desktop.
Save imranansari/3c5884aa66c45fc1eeac4d57228d6148 to your computer and use it in GitHub Desktop.
Example of AWS Comprehend with Go
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/comprehend"
)
func main() {
// Create Session
// sess := session.Must(session.NewSession())
// Create a Session with a custom region
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
}))
// Create a Comprehend client from just a session.
client := comprehend.New(sess)
// Create a Comprehend client with additional configuration
// client := comprehend.New(sess, aws.NewConfig().WithRegion("us-west-2"))
// Example sending a request using the DetectSentimentRequest method.
listTexts := []string{
"Type something here... keep going",
"Type something here... keep going",
"Type something here... keep going",
"Type something here... keep going",
"Type something here... keep going",
}
for _, text := range listTexts {
fmt.Println("\n\n")
fmt.Println(text)
{
params := comprehend.DetectSentimentInput{}
params.SetLanguageCode("en")
params.SetText(text)
req, resp := client.DetectSentimentRequest(&params)
err := req.Send()
if err == nil { // resp is now filled
// fmt.Println(*resp.Sentiment)
fmt.Println(*resp)
} else {
fmt.Println(err)
}
}
{
params := comprehend.DetectEntitiesInput{}
params.SetLanguageCode("en")
params.SetText(text)
req, resp := client.DetectEntitiesRequest(&params)
err := req.Send()
if err == nil { // resp is now filled
// fmt.Println(*resp.Sentiment)
fmt.Println(*resp)
} else {
fmt.Println(err)
}
}
{
params := comprehend.DetectKeyPhrasesInput{}
params.SetLanguageCode("en")
params.SetText(text)
req, resp := client.DetectKeyPhrasesRequest(&params)
err := req.Send()
if err == nil { // resp is now filled
// fmt.Println(*resp.Sentiment)
fmt.Println(*resp)
} else {
fmt.Println(err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment