-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatcher.js
165 lines (132 loc) · 4.51 KB
/
matcher.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
var _ = require('underscore');
var dictionary = require('./dictionary');
var regexpPhraseBuilder = function(){
var args = _.toArray(arguments);
var string = _.map(args, function(arg){
if (_.isArray(arg)){
return [
"(",
arg.join("|"),
")"
].join("");
}
return arg;
}).join(" ");
// optional matchers already take into account whitespace, so remove
// the extra space
string = string.replace(/(\\W\+\)\??) /g, "\$1");
return string;
};
var optionalMatcherBuilder = function(words, optional){
return [
"(?:(?:",
words.join("|"),
")\\W+)",
optional ? "?" : ""
].join("")
};
var matchers = {
adverbMatcher: optionalMatcherBuilder(dictionary.adverbs, true),
pronounMatcher: optionalMatcherBuilder(dictionary.pronouns, true),
swearWordMatcher: optionalMatcherBuilder(dictionary.swearWords, true),
fuckingMatcher: optionalMatcherBuilder(dictionary.fucking, true)
};
var phraseMatcher = (function(){
var phrases = [
regexpPhraseBuilder(
["drove", "dive", "drives"], // drove
"into", // into
dictionary.articles, // a
dictionary.nouns.concat("bike"), // bike
"lane" // lane
),
regexpPhraseBuilder(
dictionary.motorVehicles, // car
dictionary.verbs, // ran over
dictionary.articles, // a
dictionary.nouns // biker
),
regexpPhraseBuilder(
["door", "doored"], // doored
dictionary.articles, // a
dictionary.nouns // cyclist
),
regexpPhraseBuilder(
dictionary.nouns, // cyclist
dictionary.linkingVerbs, // got
["door", "doored"] // doored
),
regexpPhraseBuilder(
dictionary.auxVerbs, // I'll
matchers.fuckingMatcher, // fucking
"run", // run
matchers.pronounMatcher, // these
matchers.swearWordMatcher, // faggot
dictionary.nouns, // cyclists
"over" // over
)
];
return new RegExp(phrases.join("|"), "i");
})();
var innocuousPhrases = (function(){
var phrases = [
regexpPhraseBuilder(
"bicycle", "kick"
)
];
return new RegExp(phrases.join("|"), "i");
})();
module.exports = {
aggressiveFragments: function(){
var self = this;
var fragments = _.map(dictionary.verbs, function(verb){
var murdererAction = ["\\b", matchers.fuckingMatcher, matchers.adverbMatcher, matchers.fuckingMatcher, verb, "\\b\\W+", matchers.pronounMatcher, matchers.swearWordMatcher].join("");
return _.map(dictionary.nouns, function(noun){
return [murdererAction, "\\b", noun, "\\b"].join("");
});
});
return _.flatten(fragments);
},
aggressivePhrases: function(){
var self = this;
var aggressiveFragments = this.aggressiveFragments();
var phrases = _.map(dictionary.auxVerbs, function(auxVerb){
return _.map(aggressiveFragments, function(aggressiveFragment){
return ["\\b", auxVerb, "\\b\\W+", aggressiveFragment].join("");
});
});
return _.flatten(phrases);
},
isTweetARetweet: function(tweetBody){
var lowercaseBody = tweetBody.toLowerCase();
return lowercaseBody.match(/\b(rt|mt)\b ?@/i); // no retweets
},
isTweetAQuote: function(tweetBody){
var lowercaseBody = tweetBody.toLowerCase();
return lowercaseBody.match(/["|'|“|”]@.*/);
},
isTweetInnocuous: function(tweetBody){
var lowercaseBody = tweetBody.toLowerCase();
return lowercaseBody.match(innocuousPhrases);
},
doesTweetMatchAPhrase: function(tweetBody){
var lowercaseBody = tweetBody.toLowerCase();
return tweetBody.match(phraseMatcher);
},
doesTweetMatchKeywords: function(tweetBody){
var lowercaseBody = tweetBody.toLowerCase();
return lowercaseBody.match(this.matcher())
},
doesTweetMatch: function(tweetBody){
// we aren't interested in retweeting retweets or quotes
if (this.isTweetARetweet(tweetBody)) return false;
if (this.isTweetAQuote(tweetBody)) return false;
if (this.isTweetInnocuous(tweetBody)) return false;
if (this.doesTweetMatchAPhrase(tweetBody)) return true;
if (this.doesTweetMatchKeywords(tweetBody)) return true;
return false;
},
matcher: function(){
return this._matcher = this._matcher || new RegExp(this.aggressivePhrases().join("|"), "i");
}
};