Message.search(searchOptions, function(error, result){ if (error) { log.error('|homepage.getHomepage| Error getting classification -> ' + keyword, widget); utility.errorResponseJSON(res, 'Error getting classification'); } else { // Start with the mood as neutral (at 50) var moodValue = 50; if(result.total == 0) { return res.send(JSON.stringify({ result: moodValue })); } else { // Add each message's stemmed & stop-word-filtered content to the training set. var filter = new StopwordsFilter(); var classifier = new natural.BayesClassifier(); natural.PorterStemmer.attach(); for(var i=0; i<result.messages.length; i++) { var filteredContent = filter.filter(result.messages[i].content, 'string'); var stemmedContent = filteredContent.tokenizeAndStem(); classifier.addDocument(stemmedContent, String(result.messages[i].mood)); } classifier.train(); // Next, classify the given keyword var stemmedKeyword = natural.PorterStemmer.stem(keyword); var keywordClassifications = classifier.getClassifications(stemmedKeyword); // Get the sum of all classification scores for the keyword (so we can convert to a percentage) var scoreSum= 0; for(var i=0; i<keywordClassifications.length; i++) { scoreSum += (keywordClassifications[i].value * 100); } // Calculate the percentage match for each mood out of 100 var moodPercentages = { '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, }; for(var i=0; i<keywordClassifications.length; i++) { var adjustedValue = ((keywordClassifications[i].value * 100) * 100) / scoreSum; moodPercentages[keywordClassifications[i].label] = adjustedValue; } // Apply the adjusted values to the starting point of 50, to get the point on the scale. moodValue -= moodPercentages['1']/2, 10; moodValue -= moodPercentages['2']/4, 10; moodValue += moodPercentages['4']/4, 10; moodValue += moodPercentages['5']/2, 10; // If the score is zero, change to 5. Just so that red shows a little if(moodValue <= 0) { moodValue = 5; } return res.send(JSON.stringify({ result: moodValue })); } } });
Message.search(options, function(error, result){ if (error) { log.info('|message.search| Unknown -> ' + error, widget); } else { var filter = new StopwordFilter(); // Add each message's stop-word-filtered text to a single string var fullResults = ''; for(var i=0; i<result.messages.length; i++) { var filteredContent = filter.filter(result.messages[i].content, 'string'); fullResults += filteredContent + ' '; } var limit = 50; var count = 0; var topKeywords = []; // Determine the term frequency and add the top 20 results to an array var frequencyCounter = new TermFrequency(); frequencyCounter.addDocument(fullResults); frequencyCounter.listTerms(0).forEach(function(result) { if(count < limit) { topKeywords.push(result.term); count++ } }); // Save the results to the inbox Inbox.findOne({ _id: inboxID, _account: accountID }) .exec( function (error, inbox) { if (error) { log.error('|Message.resummarizeKeywords.Inbox.findOne| Unknown -> ' + error, widget); utility.errorResponseJSON(res, 'Error getting inbox'); } else if(!inbox) { log.info('|Message.resummarizeKeywords.Inbox.findOne| Inbox not found -> ' + inboxID, widget); utility.errorResponseJSON(res, 'Invalid inbox'); } else { inbox.topKeywords = topKeywords; inbox.save(function(error, updatedInbox){ if(error) { log.error('|Message.resummarizeKeywords.Inbox.save| Error saving top keywords for inbox -> ' + inboxID, widget); } else { log.info('|Message.resummarizeKeywords.Inbox.save| Top keywords saved for inbox -> ' + inboxID, widget); } }); } } ); } });