Created
May 31, 2018 08:09
-
-
Save rominirani/1c257f009d2777ed41740761b6de1f10 to your computer and use it in GitHub Desktop.
Apps Script method to invoke the Cloud Natural Language API
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
function retrieveSentimentCF (line) { | |
var apiEndpoint = 'YOUR_CLOUDFUNCTIONS_HTTPS_ENDPOINT'; | |
var reviewData = { | |
review_text: line | |
}; | |
var cfCallOptions = { | |
method : 'post', | |
contentType: 'application/json', | |
payload : JSON.stringify(reviewData) | |
}; | |
// Invoke the Cloud Function | |
var response = UrlFetchApp.fetch(apiEndpoint, cfCallOptions); | |
var data = JSON.parse(response); | |
var sentiment = 0.0; | |
if (data && data.score && data.magnitude) { | |
sentiment = data.score; | |
} | |
return sentiment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment