Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created April 3, 2018 06:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brydavis/ef5ea6e87a1c49902e27b7a7450162a9 to your computer and use it in GitHub Desktop.
Save brydavis/ef5ea6e87a1c49902e27b7a7450162a9 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