-
Notifications
You must be signed in to change notification settings - Fork 0
/
Presne.js
327 lines (298 loc) · 13 KB
/
Presne.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
/*
* Pre-SNE v0.0.0
* Presne.js / Code.gs
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Pre-SNE');
DocumentApp.getUi().showSidebar(ui);
}
function runPreSNE() {
var result = {};
// Get selected text, otherwise the whole document.
var text = getSelectedText();
if (text === null)
text = DocumentApp.getActiveDocument().getBody().getText();
else
text = text.join("\n");
result['contents'] = getCorrections(text);
return result;
}
function getCorrections(text) {
var errors = [];
var houses = ['Adams', 'Eliot', 'Mather', 'Cabot', 'Kirkland', 'Pforzheimer', 'Currier',
'Leverett', 'Quincy', 'Dudley', 'Lowell', 'Winthrop', 'Dunster'];
var states = [['Alabama', 'Ala.'], ['Arizona', 'Ariz.'], ['Arkansas', 'Ark.'],
['California', 'Calif.'], ['Colorado', 'Colo.'], ['Connecticut', 'Conn.'],
['Delaware', 'Del.'], ['Florida', 'Fla.'], ['Georgia', 'Ga.'], ['Illinois', 'Ill.'],
['Indiana', 'Ind.'], ['Kansas', 'Kan.'], ['Kentucky', 'Ky.'], ['Louisiana', 'La.'],
['Maryland', 'Md.'], ['Michigan', 'Mich.'],
['Minnesota', 'Minn.'], ['Mississippi', 'Miss.'], ['Missouri', 'Mo.'],
['Montana', 'Mont.'], ['Nebraska', 'Neb.'], ['Nevada', 'Nev.'],
['New Hampshire', 'N.H.'], ['New Jersey', 'N.J.'], ['New Mexico', 'N.M.'],
['New York', 'N.Y.'], ['North Carolina', 'N.C.'], ['North Dakota', 'N.D.'],
['Oklahoma', 'Okla.'], ['Oregon', 'Ore.'], ['Pennsylvania', 'Pa.'],
['Rhode Island', 'R.I.'], ['South Carolina', 'S.C.'], ['South Dakota', 'S.D.'],
['Tennessee', 'Tenn.'], ['Vermont', 'Vt.'], ['Virginia', 'Va.'],
['Washington', 'Wa.'], ['West Virginia', 'W.Va.'], ['Wisconsin', 'Wis.'],
['Wyoming', 'Wyo.']];
var months = [['January', 'Jan.'], ['February', 'Feb.'], ['March', 'Mar.'],
['April', 'Apr.'], ['August', 'Aug.'], ['September', 'Sept.'],
['October', 'Oct.'], ['November', 'Nov.'], ['December', 'Dec.']];
// Punctuation errors.
check(errors, replacementError(text, '‘', /‘\d\d/, 'Quotation Error',
'Quotation mark should be facing the other direction!'));
// First reference errors.
check(errors, firstReference(text, 'Administrative Board', 'Ad Board'));
check(errors, firstReference(text, 'Harvard University Dining Services', 'HUDS'));
check(errors, firstReference(text, 'Harvard Lampoon, a semi-secret Sorrento Square social organization that used to occasionally publish a so-called humor magazine', 'Lampoon'));
check(errors, firstReference(text, 'Undergraduate Council', 'UC'));
check(errors, firstReference(text, 'University Health Services', 'UHS'));
check(errors, firstReference(text, 'Malkin Athletic Center', 'the Mac'));
check(errors, firstReference(text, 'Board of Overseers', 'the Overseers'));
check(errors, firstReference(text, 'Board of Overseers', 'The Overseers'));
houses.forEach(function(house) {
check(errors, firstReference(text, house + ' House', house));
});
// Simple substitution errors.
check(errors, shouldBe(text, 'Hist. and Lit.', 'Hist and Lit', /\bHist\. and Lit\./));
check(errors, shouldBe(text, 'Arco', 'ARCO', null));
check(errors, shouldBe(text, 'vice-president', 'vice president', null));
check(errors, shouldBe(text, 'Raza', 'RAZA', null));
check(errors, shouldBe(text, '%', 'percent', null));
check(errors, shouldBe(text, 'Native American', 'American Indian', null));
check(errors, shouldBe(text, 'utility entry port', 'manhole', null));
check(errors, shouldBe(text, 'first year student', 'first-year student', null));
check(errors, shouldBe(text, 'liveable', 'livable', null));
check(errors, shouldBe(text, 'member of the Board of Overseers', 'Overseer', null));
check(errors, shouldBe(text, 'Brattle Theater', 'Brattle Theatre', null));
check(errors, shouldBe(text, 'Sanders Theater', 'Sanders Theatre', null));
check(errors, shouldBe(text, 'Cantabridgians', 'Cantabrigians', null));
check(errors, shouldBe(text, 'Massachusetts Hall', 'Mass. Hall', null));
check(errors, shouldBe(text, 'Asian-American-Association', 'Asian American Association', null));
check(errors, shouldBe(text, 'Massachusetts Avenue', 'Mass Ave.', null));
check(errors, shouldBe(text, 'Massachusetts Bay Transportation Authority', 'MBTA', null));
check(errors, shouldBe(text, 'Massachusetts Institute of Technology', 'MIT', null));
check(errors, shouldBe(text, 'American Federation of Labor and Congress of Industrial Organizations', 'AFL-CIO', null));
check(errors, shouldBe(text, 'Federal Bureau of Investigation', 'FBI', null));
check(errors, shouldBe(text, 'Central Intelligence Agency', 'CIA', null));
check(errors, shouldBe(text, 'Reserve Officers\' Training Corps', 'ROTC', null));
check(errors, shouldBe(text, 'actress', 'actor', null));
check(errors, shouldBe(text, 'advisor', 'adviser', null));
check(errors, shouldBe(text, 'alumna', 'alum', null));
check(errors, shouldBe(text, 'alumnae', 'alums', null));
check(errors, shouldBe(text, 'alumnus', 'alum', null));
check(errors, shouldBe(text, 'alumni', 'alums', null));
check(errors, shouldBe(text, 'administration', 'Administration', null));
check(errors, shouldBe(text, 'Street', 'St.', /\d+\s\w+\sStreet/));
check(errors, shouldBe(text, 'Boulevard', 'Blvd.', /\d+\s\w+\sBoulevard/));
check(errors, shouldBe(text, 'Road', 'Rd.', /\d+\s\w+\sRoad/));
check(errors, shouldBe(text, 'St.', 'Street', /[^\d]\s\w+\sSt\./));
check(errors, shouldBe(text, 'Blvd.', 'Boulevard', /[^\d]\s\w+\sBlvd\./));
check(errors, shouldBe(text, 'Rd.', 'Road', /[^\d]\s\w+\sRd\./));
states.forEach(function(state) {
check(errors, shouldBe(text, state[0], state[1], null));
});
months.forEach(function(month) {
check(errors, shouldBe(text, month[0], month[1], new RegExp(month[0] + " \\d")));
var numMatch = new RegExp(month[1] + " (\\d+)(?:st|nd|rd|th)");
var match = numMatch.exec(text);
if (match !== null) {
check(errors, {
'title': 'Date Formatting',
'text': textAround(text, match.index, match[0].length),
'contents': match[0] + ' should be ' + month[1] + ' ' + match[1]
});
}
});
// Time
var time = /(\d\d?)?:(\d\d)?\s?(am|pm|AM|PM)\b/;
var match = time.exec(text);
if (match !== null) {
if (!match[3].localeCompare("am") || !match[3].localCompare("AM"))
var correct = match[1] + ':' + match[2] + ' a.m.';
else
var correct = match[1] + ':' + match[2] + ' p.m.';
// If we have a :00 time.
if (!match[2].localeCompare("00")) {
if (!match[1].localeCompare("12")) {
if (!match[3].localeCompare("am") || !match[3].localCompare("AM"))
correct = "midnight";
else
correct = "noon";
} else {
if (!match[3].localeCompare("am") || !match[3].localCompare("AM"))
correct = match[1] + ' a.m.';
else
correct = match[1] + ' p.m.';
}
}
check(errors, {
'title': 'Time Formatting',
'text': textAround(text, match.index, match[0].length),
'contents': match[0] + ' should be ' + correct
});
}
// Avoid usage errors.
check(errors, avoidUse(text, 'pro-life', /[Pp]ro-life/));
check(errors, avoidUse(text, 'pro-choice', /[Pp]ro-choice/));
check(errors, avoidUse(text, 'for her part', /[Ff]or her part/));
check(errors, avoidUse(text, 'for his part', /[Ff]or his part/));
check(errors, avoidUse(text, 'Asked whether', null));
check(errors, avoidUse(text, 'it was learned', null));
check(errors, avoidUse(text, 'The Crimson has learned', null));
check(errors, avoidUse(text, 'CSA', null));
// Preferable use errors.
check(errors, preferableUse(text, 'approximately', 'about', /[Aa]pproximately/));
// Cautious use errors.
check(errors, cautiousUse(text, 'grumbled', null));
check(errors, cautiousUse(text, 'sniffed', null));
check(errors, cautiousUse(text, 'huffed', null));
check(errors, cautiousUse(text, 'businessman', null));
return generateErrors(errors);
}
/*
* Handles first reference errors, where some phrase should be
* some other phrase on first reference.
*/
function firstReference(text, first, subsequent) {
var firstRegex = new RegExp('\\b' + first + '\\b');
var subsequentRegex = new RegExp('\\b' + subsequent + '\\b');
var subsequentMatch = subsequentRegex.exec(text);
var firstMatch = firstRegex.exec(text);
if (subsequentMatch !== null && (firstMatch == null ||
firstMatch.index > subsequentMatch.index))
return {
'title': 'First Reference',
'text': textAround(text, subsequentMatch.index, subsequentMatch[0].length),
'contents': 'Refer to \'' + subsequent + '\' as \'' + first + '\' on first reference'
}
else
return null;
}
function replacementError(text, wrong, wrongRegex, title, message) {
Logger.log(wrong);
if (wrongRegex === null)
wrongRegex = new RegExp('\\b' + wrong + '\\b');
var wrongMatch = wrongRegex.exec(text);
if (wrongMatch !== null)
return {
'title': title,
'text': textAround(text, wrongMatch.index, wrongMatch[0].length),
'contents': message
}
else
return null;
}
/*
* Handles when one phrase should be one other phrase.
*/
function shouldBe(text, wrong, right, wrongRegex) {
return replacementError(text, wrong, wrongRegex,
'Usage Error',
'\'' + wrong + '\' should be \'' + right + '\'');
}
/*
* Handles a phrase to be avoided.
*/
function avoidUse(text, wrong, wrongRegex) {
return replacementError(text, wrong, wrongRegex,
'Avoid Use',
'Avoid use of \'' + wrong + '\'');
}
function preferableUse(text, wrong, right, wrongRegex) {
return replacementError(text, wrong, wrongRegex,
'Preferable Use',
'\'' + right + '\' is preferable to \'' + wrong + '\'');
}
function cautiousUse(text, wrong, wrongRegex) {
return replacementError(text, wrong, wrongRegex,
'Cautious Use',
'Be cautious with your use of \'' + wrong + '\', generally avoided');
}
/*
* Adds error to existing errors if there is an error.
*/
function check(errors, error) {
if (error !== null)
errors.push(error);
}
function generateErrors(errors) {
var contents = '';
for (i in errors) {
contents += '<div style="color:red;font-weight:bold;">';
contents += errors[i].title;
contents += '</div>';
if (errors[i].text !== undefined) {
contents += '<div style="font-style:oblique;font-size:80%;">';
contents += errors[i].text;
contents += '</div>';
}
contents += errors[i].contents;
contents += '<br/><br/>';
}
if (contents.length == 0) {
contents = '<div style="color:green;font-weight:bold;">Looks good!</div>';
}
return contents;
}
/*
* Gets text around a particular index.
* Gets interval characters before and after.
*/
function textAround(text, index, length) {
var interval = 16;
contents = '';
if (index > interval)
contents += '...';
contents += text.substring(index - interval, index + length + interval);
if (index + length + interval < text.length)
contents += '...';
return contents;
}
/*
* Function to get selected text.
* Adapted from Google.
* https://developers.google.com/apps-script/quickstart/docs
*/
function getSelectedText() {
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
var text = [];
var elements = selection.getSelectedElements();
for (var i = 0; i < elements.length; i++) {
if (elements[i].isPartial()) {
var element = elements[i].getElement().asText();
var startIndex = elements[i].getStartOffset();
var endIndex = elements[i].getEndOffsetInclusive();
text.push(element.getText().substring(startIndex, endIndex + 1));
} else {
var element = elements[i].getElement();
// Only translate elements that can be edited as text; skip images and
// other non-text elements.
if (element.editAsText) {
var elementText = element.asText().getText();
// This check is necessary to exclude images, which return a blank
// text element.
if (elementText != '') {
text.push(elementText);
}
}
}
}
if (text.length == 0) {
return null;
}
return text;
} else {
return null;
}
}