Skip to content

Instantly share code, notes, and snippets.

@robhinds
Created June 19, 2018 17:23
Show Gist options
  • Save robhinds/9bd466ec6efe08a9e870f8564267522f to your computer and use it in GitHub Desktop.
Save robhinds/9bd466ec6efe08a9e870f8564267522f to your computer and use it in GitHub Desktop.
object AwsSentimentService extends SentimentService {
override def getSentiment(s: String): Future[Sentiment] = {
val awsCreds = DefaultAWSCredentialsProviderChain.getInstance
val comprehendClient = AmazonComprehendClientBuilder.standard.withCredentials(awsCreds).withRegion("eu-west-1").build
Future {
val req = new DetectSentimentRequest().withText(s).withLanguageCode("en")
val result = comprehendClient.detectSentiment(req)
println(result)
Sentiment(result.getSentiment, getSentimentScore(result))
}
}
def getSentimentScore(r: DetectSentimentResult): Int = r.getSentiment match {
case "POSITIVE" => (r.getSentimentScore.getPositive * 100).toInt
case "NEGATIVE" => (r.getSentimentScore.getNegative * -100).toInt
case _ => 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment