-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebtask.js
More file actions
131 lines (102 loc) · 4.22 KB
/
Copy pathwebtask.js
File metadata and controls
131 lines (102 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//globals
var admin = require('firebase-admin');
var ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
var tone_analyzer = null;
var helping_words = ["the", "of", "and", "in", "to", "a", "on", "was", "at", "as", "by", "is", "for", "it", "its", "were", "are", "with", "from", "than", "an", "had", "or", "be", "that", "about", "he", "you", "what", "like", "also", "more", "your", "me", "if", "when", "which", "would", "because", "they", "do", "get", "no", "my", "how", "thats", "then", "got", "be", "have", "so", "i", "one", "there", "their", "theyre", "any", "us", "most", "day", "give", "these", "want", "up", "this", "just", "yeah", "dont", "but", "not", "oh", "we", "out", "him", "his", "two", "being", "could", "well", "why", "still", "thing", "ive", "can", "has", "am", "are", "is", "was", "were", "be", "being", "been", "have", "has", "had", "shall", "will", "do", "does", "did", "may", "must", "might", "can", "could", "would", "should"];
var top_limit = 8;
const username, password, key;
//secret for firebase saved in webtask as enviornment variable
// var key = {
// "type": "....",
// "project_id": "....",
// "private_key_id": "....",
// "private_key": "....",
// "client_email": "....",
// "client_id": "....",
// "auth_uri": "....",
// "token_uri": "....",
// "auth_provider_x509_cert_url": "....",
// "client_x509_cert_url": "...."
// };
//initalize Fireabase and tone analyzer
//dont create another app if there is already an app, will throw an error
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert(key),
databaseURL: "https://{your-firebase-host}.firebaseio.com"
});
}
//IBM Watson Tone Analyzer
function initToneAnalyzer(username, password) {
tone_analyzer = new ToneAnalyzerV3({
username: username,
password: password,
version_date: '2016-05-19'
});
}
//program entry point
module.exports = function(ctx, cb) {
var event = ctx.body.event;
//only need text messages to get sentimental feelings
if (event.type != 'message') return;
//firebase database references
var db = admin.database();
var moods = db.ref("moods");
var words = db.ref("words");
var counter = db.ref("counter");
//set globals
username = ctx.secrets.username;
password = ctx.secrets.password;
key = ctx.secrets.key;
initToneAnalyzer(username, password);
tone_analyzer.tone({ text: event.text }, function(err, tone) {
var data = tone.document_tone.tone_categories.filter(x => x.category_id == "emotion_tone")[0].tones;
moodTableUpdate(data, moods, counter);
frequencyTableUpdate(event.text, words);
counterUpdate(counter);
});
cb(null);
};
// add new mood to running avergage
function moodTableUpdate(mood_obj, ref, counter_ref) {
counter_ref.once('value').then(function(snapshot) {
var count = snapshot.val().count;
count = count > top_limit ? top_limit : count;
ref.once('value').then(function(snapshot) {
var mood = snapshot.val() || {};
var new_mood = {};
for (var i = 0; i < mood_obj.length; i++) {
var m = mood_obj[i];
new_mood[m.tone_id] = runningAverage(mood[m.tone_id] || 0, m.score, count);
}
console.log(new_mood);
ref.set(new_mood);
});
});
}
// count frequency of words for word cloud
function frequencyTableUpdate(sentence, ref) {
var words = sentence.match(/\S+/g) || [];
words = words.filter(function(word){
return (word.length > 2) && (helping_words.indexOf(word.toLowerCase()) < 0);
});
words.forEach(function(word){
word = word.toLowerCase();
ref.push().set(word);
});
}
// for every message update counter, helps with average and activity
function counterUpdate(ref) {
ref.once('value').then(function(snapshot) {
var count = snapshot.val().count;
if (count === undefined || count === "" || count < 0) {
usersRef.set({ count: 0 });
} else {
ref.set({ count: count + 1 });
}
});
}
//utils
function runningAverage(avg, n_v, n) {
return avg + (n_v - avg) / (n+1);
}