-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexhorter.js
417 lines (365 loc) · 12 KB
/
exhorter.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
var StandardError = require('standard-error');
var async = require('async');
var queue = require('queue-async');
var callNextTick = require('call-next-tick');
var _ = require('lodash');
var betterKnow = require('better-know-a-tweet');
var translator = require('./translator');
var defaultProbable = require('probable');
var knownLanguages = require('./data/known-translator-languages');
var getImagesFromTweet = require('get-images-from-tweet');
var AnalyzeTweetImages = require('./analyze-tweet-images');
var sb = require('standard-bail')();
var log = require('./logger').info;
var GetWord2VecNeighbors = require('get-w2v-google-news-neighbors');
var iscool = require('iscool')();
var config = require('./config');
var createWordnok = require('wordnok').createWordnok;
var wordnok = createWordnok({
apiKey: config.wordnikAPIKey
});
function createExhorter(opts) {
var chronicler = opts.chronicler;
var behavior = opts.behavior;
var canIChimeIn = opts.canIChimeIn;
var nounfinder = opts.nounfinder;
var maxCommonnessForTopic = opts.maxCommonnessForTopic;
var maxCommonnessForImageTopic = opts.maxCommonnessForImageTopic;
var nounCountThreshold = opts.nounCountThreshold;
var tributeDemander = opts.tributeDemander;
var prepPhrasePicker = opts.prepPhrasePicker;
var figurePicker = opts.figurePicker;
var decorateWithEmojiOpts = opts.decorateWithEmojiOpts;
var probable = opts.probable;
var w2vNeighborChance = opts.w2vNeighborChance ? opts.w2vNeighborChance : 0;
if (!probable) {
probable = defaultProbable;
}
var analyzeTweetImagesOpts = {};
if (opts.getImageAnalysis) {
analyzeTweetImagesOpts.getImageAnalysis = opts.getImageAnalysis;
}
var analyzeTweetImages = AnalyzeTweetImages(analyzeTweetImagesOpts);
var getWord2VecNeighbors = GetWord2VecNeighbors({
gnewsWord2VecURL: config.gnewsWord2VecURL,
nounfinder: nounfinder,
probable: probable,
wordnok: wordnok,
nounLikePhrasesOnly: true,
nounWordsOnly: true
});
function getExhortationForTweet(tweet, exhortationDone) {
// +|
// WHOA! ++|
// \ +++|
// ++++|
// |
// vvvvvvvvvv
// vvvvvvvv
// +-++---------
// o | || || |
// | || || |
// | || || |
// +-+-----+++-+
// o| || || |
// | || || |
// | || || o
// +--++-+++o+
// | || || || |
// | || || || | o
// o |o|| || || o
// oo o || || |
// ooo o o |o || |
// Each step in the waterfall passes the tweet (plus anything else
// necessary) to the next step.
// If any step returns an error, we bail on generating an exhortation.
async.waterfall(
[
waterfallKickoff,
isNotTweetOfSelf,
isNotARetweetOfSelf,
checkThatTweetWasNotRepliedTo,
findLastReplyDateForUser,
replyDateWasNotTooRecent,
statusContainsTextThatIsOKToReplyTo,
getNounsFromTweet,
filterToNouns,
filterOutOldNouns,
maybeGetNearestNeighborNouns,
checkThatNounThresholdIsMet,
makeExhortationFromNouns
],
finalDone
);
function waterfallKickoff(done) {
callNextTick(done, null, tweet);
}
function finalDone(error, tweet, exhortation, topics) {
if (error) {
exhortationDone(error);
} else {
exhortationDone(error, tweet, exhortation, topics);
}
}
}
function findLastReplyDateForUser(tweet, done) {
chronicler.whenWasUserLastRepliedTo(
tweet.user.id_str,
function passLastReplyDate(error, date) {
// Don't pass on the error – `whenWasUserLastRepliedTo` can't find a
// key, it returns a NotFoundError. For us, that's expected.
if (error && error.type === 'NotFoundError') {
error = null;
date = new Date(0);
}
done(error, tweet, date);
}
);
}
function checkThatTweetWasNotRepliedTo(tweet, done) {
chronicler.tweetWasRepliedTo(tweet.user.id_str, function stopIfReplied(
error,
wasRepliedTo
) {
var realError;
if (wasRepliedTo) {
realError = createErrorForTweet(tweet, {
message: 'Tweet was already replied to.'
});
} else {
// If chronicler can't find the key, it will return an error for that.
// To us, that's not an error.
realError = null;
}
done(realError, tweet);
});
}
function replyDateWasNotTooRecent(tweet, date, done) {
if (typeof date !== 'object') {
date = new Date(date);
}
var hoursElapsed = (Date.now() - date.getTime()) / (60 * 60 * 1000);
if (hoursElapsed > behavior.hoursToWaitBetweenRepliesToSameUser) {
// Pass the tweet down the waterfall.
done(null, tweet);
} else {
// TODO: Consider making this a status object instead of an error.
done(
createErrorForTweet(tweet, {
message: 'Replied too recently.',
hoursSinceLastReply: hoursElapsed
})
);
}
}
function isNotARetweetOfSelf(tweet, done) {
var error = null;
if (betterKnow.isRetweetOfUser('godtributes', tweet)) {
error = createErrorForTweet(tweet, {
message: 'This is a retweet of myself.'
});
}
// Passes on the tweet. Error indicates if the tweet was a RT.
callNextTick(done, error, tweet);
}
function isNotTweetOfSelf(tweet, done) {
var error = null;
if (betterKnow.isTweetOfUser('godtributes', tweet)) {
error = createErrorForTweet(tweet, {
message: 'This is one of my tweets.'
});
}
// Passes on the tweet. Error indicates if the tweet was a self-tweet.
callNextTick(done, error, tweet);
}
function statusContainsTextThatIsOKToReplyTo(tweet, done) {
var textIsOK = canIChimeIn(tweet.text);
if (!textIsOK) {
log('Is NOT OK to respond to', tweet.text);
}
var error = null;
if (!textIsOK) {
error = createErrorForTweet(tweet, {
message: 'Contents unsafe to respond to.',
text: tweet.text
});
}
callNextTick(done, error, tweet);
}
function getNounsFromTweet(tweet, done) {
var mediaURLs = getImagesFromTweet(tweet);
if (
behavior.enableImageAnalysis &&
mediaURLs &&
mediaURLs.length > 0 &&
probable.roll(3) === 1
) {
log('Looking through image:', mediaURLs[0]);
analyzeTweetImages(tweet, sb(getNounsFromReport, done));
} else {
nounfinder.getNounsFromText(tweet.text, function passNouns(
finderError,
nouns
) {
var error = null;
if (!nouns || nouns.length < 1) {
error = createErrorForTweet(tweet, {
message: 'No nouns found.',
nounFinderError: finderError
});
}
done(error, tweet, nouns, false);
});
}
function getNounsFromReport(report, done) {
done(null, tweet, report.nouns, true);
}
}
function filterToNouns(tweet, nouns, isUsingTweetImage, done) {
var maxCommonness = maxCommonnessForTopic;
if (isUsingTweetImage) {
maxCommonness = maxCommonnessForImageTopic;
}
nounfinder.filterNounsForInterestingness(
nouns,
maxCommonness,
function filterDone(finderError, filteredNouns) {
var error = null;
if (!filteredNouns || filteredNouns.length < 1) {
error = createErrorForTweet(tweet, {
message: 'Filtered ALL nouns from text.',
nounFinderError: finderError
});
}
done(error, tweet, filteredNouns, isUsingTweetImage);
}
);
}
function filterOutOldNouns(tweet, nouns, isUsingTweetImage, done) {
var q = queue();
nouns.forEach(function queueNounRecordCheck(noun) {
q.defer(chronicler.topicWasUsedInReplyToUser, noun, tweet.user.id_str);
q.defer(chronicler.topicWasUsedInTribute, noun);
});
// Here, `unused` means we haven't used it yet, and thus, we can use them
// potentially.
q.awaitAll(function buildListOfUnusedNouns(lookupError, usedFlags) {
var unusedNouns = [];
for (var i = 0; i < usedFlags.length; i += 2) {
if (!usedFlags[i] && !usedFlags[i + 1]) {
unusedNouns.push(nouns[i / 2]);
}
}
var error = null;
if (lookupError || !unusedNouns || unusedNouns.length < 1) {
error = createErrorForTweet(tweet, {
message: 'No new material for user.',
userId: tweet.user.id,
chroniclerError: lookupError
});
}
done(error, tweet, unusedNouns, isUsingTweetImage);
});
}
function checkThatNounThresholdIsMet(tweet, nouns, neighbors, done) {
var error = null;
if (nouns.length + neighbors.length < nounCountThreshold) {
error = createErrorForTweet(tweet, {
message: "There aren't enough usable nouns to work with."
});
}
callNextTick(done, error, tweet, nouns, neighbors);
}
// Assumes nouns has at least one element.
function maybeGetNearestNeighborNouns(tweet, nouns, isUsingTweetImage, done) {
if (!isUsingTweetImage && probable.roll(100) < w2vNeighborChance) {
getWord2VecNeighbors(nouns, passNouns);
} else {
callNextTick(done, null, tweet, nouns, []);
}
function passNouns(error, neighbors) {
if (error) {
done(error);
} else if (!neighbors) {
// Send along original nouns.
done(null, tweet, nouns, []);
} else {
done(null, tweet, nouns, neighbors.filter(iscool));
}
}
}
// Assumes nouns has at least one element.
function makeExhortationFromNouns(tweet, nouns, neighbors, done) {
var tweetLocale = 'en';
if (tweet.lang) {
tweetLocale = tweet.lang;
}
var selectedNouns;
if (neighbors.length > 0) {
selectedNouns = [
probable.pickFromArray(nouns),
probable.pickFromArray(neighbors)
];
} else {
selectedNouns = probable.shuffle(nouns).slice(0, 2);
}
var primaryTribute = tributeDemander.makeDemandForTopic(
decorateWithEmojiOpts({
topic: selectedNouns[0],
prepositionalPhrase: prepPhrasePicker.getPrepPhrase(),
tributeFigure: figurePicker.getMainTributeFigure()
})
);
var secondaryTribute;
if (selectedNouns.length > 1) {
secondaryTribute = tributeDemander.makeDemandForTopic(
decorateWithEmojiOpts({
topic: selectedNouns[1],
prepositionalPhrase: prepPhrasePicker.getPrepPhrase(),
tributeFigure: figurePicker.getSecondaryTributeFigure()
})
);
}
var addressClause = '@' + tweet.user.screen_name + ' ';
var exhortation = primaryTribute;
if (secondaryTribute) {
exhortation += '! ' + secondaryTribute;
}
if (tweetLocale === 'en' && probable.roll(100) === 0) {
tweetLocale = translator.pickRandomTranslationLocale({
excludeLocale: 'en'
});
}
if (tweetLocale !== 'en' && knownLanguages.indexOf(tweetLocale) !== -1) {
translator.translate(exhortation, 'en', tweetLocale, returnTranslation);
} else {
callNextTick(
done,
null,
tweet,
addressClause + exhortation,
selectedNouns
);
}
function returnTranslation(error, translation) {
if (error) {
done(error, tweet, addressClause + exhortation, selectedNouns);
} else {
done(error, tweet, addressClause + translation, selectedNouns);
}
}
}
function createErrorForTweet(tweet, overrides) {
return new StandardError(
_.defaults(overrides, {
id: tweet.id_str,
screen_name: tweet.user.screen_name
// time: tweet.time
})
);
}
return {
getExhortationForTweet: getExhortationForTweet
};
}
module.exports = createExhorter;