-
Notifications
You must be signed in to change notification settings - Fork 3
/
templater.js
257 lines (230 loc) · 8.95 KB
/
templater.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
var fs = require('fs');
var async = require("async");
var Inflect = require('i')();
var Article = require('indefinite-article');
module.exports = function Templater (callback) {
// SYNTAX:
// Add s- to a key to make it the subject
// Surround something with _ to add it to the extra search terms
// Convention: plural things have plural keys
// E.g. [[nouns]] -> cats
// [[noun]] -> cat
// Every template should include an s- key.
// PREDICTIVE INDEFINITE ARTICLES:
// *A* tries to add an appropriate indefinite article.
// E.g. *A* [[s-noun]] -> A Cat
// *A* [[a s-noun]] -> An Apple
//
//
// OPTIONALS:
// {{first|second|...}}
// SPECIAL CASES:
// num: Just a number
// tnum: Number optionally preceded by "The"
// xnum: Random number that the system doesn't care about
var templates = {};
this.loadTestTitles = function(){
templates = ["_Test_ *A* [[s-noun]] _{{one option|two option}}_"];
};
this.loadBuzzTitles = function(){
templates = [
"The [[num]] [[sup-adj]] [[s-nouns]] In The World",
"The [[num]] [[sup-adj]] [[s-nouns]] Of Last _{{Summer|Winter|Fall|Spring}}_",
"The [[num]] [[sup-adj]] [[s-nouns]] Of The 90's",
"The [[num]] [[sup-adj]] [[s-nouns]] Of The Last [[x-num]] Years",
"The [[num]] [[sup-adj]] [[s-nouns]] Of This Century",
"The [[num]] [[sup-adj]] [[s-nouns]] Only _[[people]]_ Will Understand",
"The [[num]] [[sup-adj]] [[s-nouns]] That Will Make You Laugh Every Time",
"[[num]] Things [[s-people]] Should Be Allowed To Complain About",
"[[num]] Times [[s-nouns]] Are The Worst And You Just Can't Even",
"[[num]] [[s-people]] With Excellent _New Year_'s Resolutions",
"[[num]] _[[people]]_ With [[crazy]] [[s-nouns]]",
"[[num]] [[s-nouns]] For _[[people]]_ That {{Should Really|Really Shouldn't}} Exist",
"[[t-num]] Things [[s-people]] Know To Be {{True|False}}",
"[[t-num]] [[adj]] [[s-nouns]] That Will Make You Ask \"{{Why?|How?|What?|Where?|Who?}}\"",
"[[t-num]] {{[[adj]]|[[sup-adj]]}} [[s-nouns]] You {{Probably Didn't Know|Might've Encountered}}",
"[[t-num]] [[crazy]] Things [[s-people]] Know To Be True",
"[[t-num]] [[s-people]] Who Are Clearly Being Raised _[[adj]]_",
"[[t-num]] [[s-people]] Who Are Having *A* Really {{Rough|Fun}} Day",
"[[t-num]] [[s-people]] Who Are Too Clever For Their Own Good",
"[[t-num]] [[s-people]] Who Completely Screwed Up Their One Job",
"[[t-num]] [[s-people]] Who Have Performed For _[[people]]_",
"[[t-num]] [[s-people]] Who Need To Be Banned From Celebrating _{{Halloween|Christmas|Easter|4th of July}}_",
"[[t-num]] [[s-people]] Who Will Make You Feel Like *A* Genius",
"[[t-num]] [[s-nouns]] That Scream _World Domination_",
"[[t-num]] [[s-people]] You Must Do In Your [[age]] According To _[[famous-person]]_",
"If [[s-tv-show-character]] Had _Instagram_",
"[[t-num]] Times [[s-tv-show-character]] Summed Up You And Your BFF",
"[[s-famous-person]] Receives *A* _[[noun]]_, Is Overcome With Joy",
"[[t-num]] [[s-people]] You Actually Cannot Resist _{{Kissing|Hugging}}_"
];
};
this.loadQuizTitles = function(){
templates = [
"What Character From [[s-showTitle]] Are You?",
"Which [[s-showTitle]] Characters Are You?",
"Which [[s-showTitle]] Character Is Your Soulmate?",
"Which [[s-showTitle]] Character Is Your Kindred Spirit?"
];
};
this.loadQuizPhotoQuestions = function(){
templates = [
"What's Your Favorite [[s-noun]]?",
"What's Your Dream [[s-noun]]?",
"Which [[s-noun]] Resonates With You The Most?",
"Pick *A* [[s-noun]]",
"Which [[s-noun]] Is Most Attractive?",
"Choose *A* [[s-noun]]",
"What Type Of [[s-noun]] Really Puts You In The Mood",
"Pick What Represents Your [[sup-adj]] [[s-noun]] The Best"
];
};
this.loadQuizNounQuestions = function(){
templates = [
"Which Of The Following Would You Rather Eat?",
"Which Looks The Most Appetizing?",
"Which One Is The [[sup-adj]] Of The Following?",
"Which Seems Like It Would Feel The Best?",
"Which Do You Value Most?",
"Which Is The [[sup-adj]]?",
"Which Of The Following is Worth Your Life?",
"Which Is The Best Aphrodisiac?",
"Choose *A* Synonym For [[noun]]",
"What's [[num]] + [[num]]?",
"What Are Your Thoughts On [[s-noun]]?",
"Is [[s-noun]] *A* Childhood Dream?",
];
};
this.loadQuizPeopleQuestions = function() {
templates = [
"Choose Your Future Career",
"Who Do You Hate The Most?",
"Pick The [[sup-adj]] Co-Worker",
"Who Would You Rather [[verb]]?",
"Who's The [[sup-adj]]?",
"What's Your Drunk Alter Ego?",
"What's [[s-tv-show-character]]'s Real Job?"
];
};
this.loadQuizYesNoQuestions = function() {
templates = [
"Do You Usually [[verb]] [[s-noun]] In The Morning?",
"Do You Hate [[s-people]]?",
"Is [[s-noun]] Your Favorite Food",
"Would You Ever Eat [[s-nouns]]",
"Is [[s-famous-person]] Actually [[adj]]"
];
};
this.getRand = function(key) {
return dicts[key][rand(0,dicts[key].length)];
};
var dicts = {};
var minNum = 7;
var maxNum = 30;
function loadFile(key, file, callback){
fs.readFile(file, 'utf8', function (err,data) {
if (err) {
return console.error(err);
}
dicts[key] = data.replace(/\r/g, "").split("\n");
callback();
});
}
this.loadKey = function(key, list){
dicts[key] = list;
};
var __listsToLoad = [
["sup-adj", "wordlists/sup-adj.txt"],
["adj", "wordlists/adj.txt"],
["nouns", "wordlists/nouns.txt"],
["noun", "wordlists/noun.txt"],
["people", "wordlists/people.txt"],
["crazy", "wordlists/crazy.txt"],
["verb", "wordlists/verb.txt"],
["famous-person", "wordlists/famous-person.txt"],
["tv-show-character", "wordlists/tv-show-character.txt"],
["age", "wordlists/age.txt"],
];
async.mapLimit(__listsToLoad, 5, function(arr, callback) {
loadFile(arr[0], arr[1], callback);
}, function(err) {
if(err) return console.error(err);
if(callback) callback();
});
function genFromTemplate(template){
var match;
var inner;
var ret = {};
ret.num = rand(minNum, maxNum);
ret.extra = [];
while (!!(match = template.match(/\[\[[^\]]+]]/))){
match = match[0];
inner = match.substr(2, match.length-4);
if (inner === "num"){
ret.num = rand(minNum, maxNum);
template = replaceMatch(template, match, ""+ret.num);
} else if (inner === "t-num"){
ret.num = rand(minNum, maxNum);
template = replaceMatch(template, match, "The "+ret.num);
} else if (inner === "x-num"){
template = replaceMatch(template, match, ""+rand(minNum,maxNum));
} else if (inner.indexOf("s-") === 0){
inner = inner.substring(2,inner.length);
ret.subj = dicts[inner][rand(0,dicts[inner].length)];
template = replaceMatch(template, match, ret.subj);
} else if (inner.indexOf("x-") === 0){
inner = inner.substring(2,inner.length);
var repl = dicts[inner][rand(0,dicts[inner].length)];
ret.extra.push(repl);
template = replaceMatch(template, match, repl);
} else {
var repl2 = dicts[inner][rand(0,dicts[inner].length)];
template = replaceMatch(template, match, repl2);
}
}
while (!!(match = template.match(/{{[^}]+}}/))){
match = match[0];
inner = match.substr(2, match.length-4);
var opts = inner.split("|");
template = replaceMatch(template, match, opts[rand(0,opts.length)]);
}
while (!!(match = template.match(/_[^_]+_/))){
match = match[0];
inner = match.substr(1, match.length-2);
ret.extra.push(inner);
template = replaceMatch(template, match, inner);
}
var index;
while ((index = template.indexOf("*A*")) !== -1){
match = template.substring(index+3);
inner = Inflect.titleize(Article(match));
template = template.slice(0,index) + inner + template.slice(index + 3);
}
ret.title = template;
return ret;
}
this.generateName = function(){
var template = templates[rand(0,templates.length)];
var ret = genFromTemplate(template);
ret.articleName = ret.title.toLowerCase().replace(/ /g, "-").replace(/[\"\'\?]/g, "");
return ret;
};
function rand(min, max){
return Math.floor(Math.random() * (max-min))+min;
}
function replaceMatch(fullString, match, substitute){
var start = fullString.indexOf(match);
var newStr = fullString.slice(0,start) + substitute +
fullString.slice(start + match.length, fullString.length);
return newStr;
}
};
// var Templater = require("./templater");
// var temp = new Templater(function () {
// console.log("loaded");
// temp.loadTestTitles();
// //temp.loadBuzzTitles();
// for (var i = 0; i < 100; i++){
// console.log(temp.generateName().title);
// }
// });