-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
301 lines (263 loc) · 7.97 KB
/
parse.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
var Parser = require("simple-text-parser"),
parser = new Parser();
var fs = require("fs");
var Q = require("q");
var node_ner = require('node-ner');
var DBPSQ = require("./sparql.js");
var wordfilter = require("wordfilter");
var ner = new node_ner({
install_path: '~/Downloads/stanford-ner-2014-10-26/'
});
var config = require("./config.json");
var foursquare = (require('foursquarevenues'))(
config.client_id,
config.client_secret
);
var allNames = require("./names.json");
var result;
var outputData = [];
//via https://gist.github.com/karbassi/6216484
String.prototype.smarten = function () {
return this
/* opening singles */
.replace(/(^|[-\u2014\s(\["])'/g, "$1\u2018")
/* closing singles & apostrophes */
.replace(/'/g, "\u2019")
/* opening doubles */
.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c")
/* closing doubles */
.replace(/"/g, "\u201d")
/* em-dashes */
.replace(/--/g, "\u2014");
};
/****
//
// NAMES!
//
****/
function isNamed(str) {
for (var name in allNames) {
if(allNames[name] == str) {
return true;
}
}
return false;
}
function addNames(arr) {
for(var n in arr) {
if(!isNamed(arr[n]) && !arr[n].match(/^\d+$/) && !isNamed(arr[n].split(" ")[0])) {
allNames.push(arr[n]);
}
}
}
function removeNamesFromArray(arr) {
var newarr = [];
for(var n in arr) {
if(!isNamed(arr[n])) {
newarr.push(arr[n]);
}
}
return newarr;
}
function shuffleNames(arr) {
var newNames = [];
for (var n in arr) {
var newName = allNames[Math.floor(Math.random() * allNames.length)];
newNames.push({
original: arr[n],
swapped: newName
})
}
return newNames;
}
/****
//
// REGEXES!
//
****/
var RegExps = {
// this finds a date in the format
// YYYY-MM-DD or similar.
date: /[\d-]+\r\n\r\n/,
// this finds whole entries.
// (date + newline) + (any text/newlines, lazy) + lookahead(date + newline OR end of string)
entry: /[\d-]+\r\n\r\n[\s\S]+?(?=([\d-]+\r\n\r\n)|$)/g,
properNoun: /\w (([A-Z][a-z]+[\s|\W])+(?:[A-Z][a-z]+[\s|\W]|of[\s|\W]|[A-Z^I][\s|\W])*)/g
}
//find only the elements in the firstArray that do not exist in the secondArray.
function crossReference(firstArray, secondArray) {
var newArray = [];
for(var i = 0; i < firstArray.length; i++) {
if( secondArray.indexOf(firstArray[i]) == -1 ) {
newArray.push(firstArray[i]);
}
}
return newArray;
}
function findProperNouns(entryText) {
var allArray = [],
matchesArray;
while( (matchesArray = RegExps.properNoun.exec(entryText)) !== null ) {
var value = matchesArray[1].substring(0, matchesArray[1].length-1);
// console.log(value);
allArray.push(value);
}
return allArray;
}
function findProperNounsWithNER(results, currentIndex, allEntryPromises) {
var workingEntry = results[currentIndex];
var entryText = workingEntry.text;
var promisesArray = [];
fs.writeFileSync("temp.txt", entryText);
ner.fromFile('temp.txt', function(entities) {
console.log("NER success!");
console.log(entities);
var newPeople, newOrgs, newLocs;
var capitalizedWords = findProperNouns(entryText);
capitalizedWords = removeNamesFromArray(capitalizedWords);
if(entities.PERSON) {
addNames(entities.PERSON);
capitalizedWords = crossReference(capitalizedWords, entities.PERSON);
var newPeople = shuffleNames(entities.PERSON);
}
if(entities.ORGANIZATION) {
var cleanOrgs = removeNamesFromArray(entities.ORGANIZATION);
capitalizedWords = crossReference(capitalizedWords, entities.ORGANIZATION);
promisesArray.push(replaceWithFoursquareVenues(cleanOrgs));
}
if(entities.LOCATION) {
var cleanLocs = removeNamesFromArray(entities.LOCATION);
capitalizedWords = crossReference(capitalizedWords, entities.LOCATION);
promisesArray.push(replaceWithFoursquareVenues(cleanLocs));
}
promisesArray.push(replaceViaWikipedia(capitalizedWords));
var finalPromise = Q.all(promisesArray).then(function (results) {
if(newPeople) {
results.push(newPeople);
}
for(var replacements in results) {
for(var swap in results[replacements]) {
var obj = results[replacements][swap];
var replacementRegex = new RegExp(obj.original+'(?=[\\s\\W])', 'g');
var filteredWord = wordfilter.blacklisted(obj.swapped) ? obj.original : obj.swapped;
filteredWord = decodeURIComponent(filteredWord);
workingEntry.text = workingEntry.text.replace(replacementRegex, filteredWord);
}
}
//replace all numbers
workingEntry.text = replaceNumbers(workingEntry.text);
return workingEntry;
});
if(currentIndex < results.length - 1) {
allEntryPromises.push(finalPromise);
findProperNounsWithNER(results, currentIndex+1, allEntryPromises);
} else {
allEntryPromises.push(finalPromise);
Q.all(allEntryPromises).then(function (allEntries) {
console.log("DONE");
fs.writeFileSync("outputText/outputText.json", JSON.stringify(allEntries));
})
.catch(function (error) {
console.log("ERROR!!!----")
console.log(error);
})
}
});
}
function replaceNumbers(text) {
var matches = text.match(/\d+/g),
newText = text;
for(sequence in matches) {
var n = matches[sequence],
newNumber = "";
//safeguard against times...wonky but safe
if(n == "10" || n == "11" || n == "12") {
continue;
}
for(var i = 0; i < n.length; i++) {
newNumber += "1234567890".split("")[Math.floor(Math.random()*10)];
}
var newText = text.replace(n, newNumber);
}
return newText;
}
function replaceViaWikipedia(thingsArray) {
var promisesArray = [];
for(var i in thingsArray) {
var wPromise = DBPSQ.requestEntry(thingsArray[i]);
promisesArray.push(wPromise);
} //end for loop
return Q.all(promisesArray);
}
function replaceWithFoursquareVenues(thingsArray) {
var promisesArray = [];
var originalName;
for(var i in thingsArray) {
originalName = thingsArray[i];
var params = {
"ll": "40.7127, -74.0059",
"query": originalName
};
var fsPromise = foursquare.getVenues(params)
.then(function(venues) {
var r = JSON.parse(venues).response;
if (r.venues.length > 0) {
return r.venues[0].id;
} else {
throw new Error("couldn't find a matching venue in Foursquare");
}
})
.then(foursquare.getSimilarVenue)
.then(function(venues){
var r = JSON.parse(venues).response;
if(r.similarVenues.count > 0) {
var rand = Math.floor(Math.random() * r.similarVenues.count);
var newName = r.similarVenues.items[rand].name;
console.log(originalName + "-->" + newName);
return {
original: originalName,
swapped: newName
};
} else {
return {
original: originalName,
swapped: originalName
};
}
})
.catch(function(error){
console.log(error.message);
return Q({
original: originalName,
swapped: originalName
});
});
promisesArray.push(fsPromise);
}//end for loop
return Q.all(promisesArray);
}
parser.addRule(RegExps.entry, function(entry) {
//clean up newlines within entry
//separate the date
var date = entry.match(RegExps.date)[0];
entry = entry.replace(RegExps.date, '');
entry = entry.replace(/\r\n\r\n/g, '\n');
entry = entry.smarten();
entry = entry.trim();
return {
type: "entry",
date: date.substring(0, date.length - 4),
text: entry.trim()
};
});
fs.readFile('sourceText/ohlife_20141012-sanitized.txt', 'utf8', function (err, data) {
if (err) {
result = err;
}
result = parser.toTree(data);
findProperNounsWithNER(result, 0, []);
// var n = DBPSQ.requestEntry("Bastion");
// n.then(function (d) {
// console.log(d);
// })
});