Skip to content

Commit c9b7745

Browse files
authored
feat: Add positive and negative sentiment labels (#520)
1 parent a1c5100 commit c9b7745

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

gmail-sentiment-analysis/Gmail.gs

+21-17
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ function analyzeSentiment() {
2626

2727
/**
2828
* Analyzes the sentiment of recent emails in the inbox and labels threads with
29-
* negative sentiment as "UPSET TONE 😡".
29+
* the appropriate sentiment label.
3030
*/
3131
function analyzeAndLabelEmailSentiment() {
32-
const labelName = "UPSET TONE 😡";
32+
const positiveLabelName = "HAPPY TONE 😊";
33+
const neutralLabelName = "NEUTRAL TONE 😐";
34+
const negativeLabelName = "UPSET TONE 😡";
3335
const maxThreads = 10;
3436

3537
// Get the label, or create it if it doesn't exist.
36-
const label = GmailApp.getUserLabelByName(labelName) || GmailApp.createLabel(labelName);
38+
const positiveLabel = GmailApp.getUserLabelByName(positiveLabelName) || GmailApp.createLabel(positiveLabelName);
39+
const neutralLabel = GmailApp.getUserLabelByName(neutralLabelName) || GmailApp.createLabel(neutralLabelName);
40+
const negativeLabel = GmailApp.getUserLabelByName(negativeLabelName) || GmailApp.createLabel(negativeLabelName);
3741

3842
// Get the first 'maxThreads' threads from the inbox.
3943
const threads = GmailApp.getInboxThreads(0, maxThreads);
@@ -45,25 +49,25 @@ function analyzeAndLabelEmailSentiment() {
4549
// Process each message within the thread.
4650
for (const message of messages) {
4751
const emailText = message.getPlainBody();
48-
const isUpset = isNegativeSentiment(emailText);
49-
50-
if (isUpset) {
51-
thread.addLabel(label);
52+
const sentiment = processSentiment(emailText);
53+
54+
switch (sentiment) {
55+
case 'positive':
56+
thread.addLabel(positiveLabel);
57+
break;
58+
case 'neutral':
59+
thread.addLabel(neutralLabel);
60+
break;
61+
case 'negative':
62+
thread.addLabel(negativeLabel);
63+
break;
64+
default:
65+
break;
5266
}
5367
}
5468
}
5569
}
5670

57-
/**
58-
* Determines if the given text has a negative sentiment.
59-
*
60-
* @param {string} text - The text to analyze.
61-
* @returns {boolean} True if the sentiment is negative, false otherwise.
62-
*/
63-
function isNegativeSentiment(text) {
64-
return processSentiment(text) === 'negative';
65-
}
66-
6771
/**
6872
* Create sample emails
6973
*/

0 commit comments

Comments
 (0)