natural-language-api-google/utils/sentiment.js

38 lines
902 B
JavaScript

// Imports the Google Cloud client library
const language = require('@google-cloud/language').v2
// Creates a client
const client = new language.LanguageServiceClient()
const sentiment = async (text) => {
// Prepares a document, representing the provided text
const document = {
content: text,
type: 'PLAIN_TEXT',
}
let status = null
try {
// Detects the sentiment of the document
const [result] = await client.analyzeSentiment({ document })
const sentiment = result.documentSentiment
if (sentiment.score <= -0.25)
status = 'negative'
else if (sentiment.score <= 0.25)
status = 'neutral'
else
status = 'positive'
} catch (error) {
console.log(`Error in sentiment fuction on sentiment.js file: ${error}`)
}
return status
}
module.exports = sentiment