2023-10-09 17:22:48 +00:00
|
|
|
// 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
|
2023-10-10 13:58:54 +00:00
|
|
|
status = 'positive'
|
2023-10-09 17:22:48 +00:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error in sentiment fuction on sentiment.js file: ${error}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = sentiment
|