@@ -26,14 +26,18 @@ function analyzeSentiment() {
26
26
27
27
/**
28
28
* Analyzes the sentiment of recent emails in the inbox and labels threads with
29
- * negative sentiment as "UPSET TONE 😡" .
29
+ * the appropriate sentiment label .
30
30
*/
31
31
function analyzeAndLabelEmailSentiment ( ) {
32
- const labelName = "UPSET TONE 😡" ;
32
+ const positiveLabelName = "HAPPY TONE 😊" ;
33
+ const neutralLabelName = "NEUTRAL TONE 😐" ;
34
+ const negativeLabelName = "UPSET TONE 😡" ;
33
35
const maxThreads = 10 ;
34
36
35
37
// 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 ) ;
37
41
38
42
// Get the first 'maxThreads' threads from the inbox.
39
43
const threads = GmailApp . getInboxThreads ( 0 , maxThreads ) ;
@@ -45,25 +49,25 @@ function analyzeAndLabelEmailSentiment() {
45
49
// Process each message within the thread.
46
50
for ( const message of messages ) {
47
51
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 ;
52
66
}
53
67
}
54
68
}
55
69
}
56
70
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
-
67
71
/**
68
72
* Create sample emails
69
73
*/
0 commit comments