Created
June 19, 2018 17:23
-
-
Save robhinds/9bd466ec6efe08a9e870f8564267522f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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