-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngram_group_parser.user.js
260 lines (227 loc) · 6.48 KB
/
ngram_group_parser.user.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
// ==UserScript==
// @name edrdg N-gram group parser
// @namespace edrdg-scripts
// @version 2023.12.15.2
// @author Stephen Kraus
// @match *://*.edrdg.org/~jwb/ngramcounts*
// @icon https://www.edrdg.org/favicon.ico
// @grant none
// @run-at document-end
// @homepageURL https://github.com/stephenmk/edrdg-scripts
// @updateURL https://github.com/stephenmk/edrdg-scripts/raw/main/ngram_group_parser.user.js
// ==/UserScript==
'use strict';
const omissibleBraces = "()()[][]";
const alternativeBraces = "〈〉<><>{}{}";
const delimiters = "/,、.。;; ";
function partsToSearchTerms(parts) {
let delimitedTerms = [];
let terms = [""];
for (const part of parts) {
if (part.isDelimiter) {
delimitedTerms = delimitedTerms.concat(terms);
terms = [""];
} else if (part.groupType === "omissible") {
const partTerms = partsToSearchTerms(part);
let newTerms = terms;
for (const partTerm of partTerms) {
newTerms = newTerms.concat(terms.map(term => term + partTerm));
}
terms = newTerms;
} else if (part.groupType === "alternative") {
const partTerms = partsToSearchTerms(part);
let newTerms = [];
for (const partTerm of partTerms) {
newTerms = newTerms.concat(terms.map(term => term + partTerm));
}
terms = newTerms;
} else {
terms = terms.map(term => term + part);
}
}
if (terms.length !== 1 || terms[0] !== "") {
delimitedTerms = delimitedTerms.concat(terms);
}
return delimitedTerms;
}
function partsToNormalizedText(parts) {
if (typeof parts === "string") {
return parts;
} else if (parts.isDelimiter) {
return delimiters[0];
}
const subTexts = [];
for (const part of parts) {
subTexts.push(partsToNormalizedText(part));
}
let lBrace, rBrace;
if (parts.groupType === "omissible") {
lBrace = omissibleBraces[0];
rBrace = omissibleBraces[1];
} else if (parts.groupType === "alternative") {
lBrace = alternativeBraces[0];
rBrace = alternativeBraces[1];
} else {
lBrace = "";
rBrace = "";
}
const subText = subTexts.join("");
const text = `${lBrace}${subText}${rBrace}`;
return text;
}
function groupTable() {
const queryTable = document.querySelector("#groupTable");
if (queryTable !== null) {
return queryTable;
}
const headCell1 = document.createElement("th");
const headCell2 = document.createElement("th");
const headCell3 = document.createElement("th");
headCell1.innerText = "Term Count"
headCell2.innerText = "Group Expression"
headCell3.classList.add("button-cell")
const table = document.createElement("table");
table.id = "groupTable";
const headRow = table.insertRow();
headRow.append(headCell1, headCell2, headCell3);
document.body.appendChild(table);
return table;
}
function insertIntoTable(parts) {
const countCell = document.createElement("td");
countCell.innerText = partsToSearchTerms(parts).length;
countCell.classList.add("count-cell");
const expCell = document.createElement("td");
expCell.innerText = partsToNormalizedText(parts);
const copyButton = document.createElement("button");
copyButton.type = "button";
copyButton.innerText = "Copy";
copyButton.addEventListener('click', copyGroupText);
const copyCell = document.createElement("td");
copyCell.appendChild(copyButton);
copyCell.classList.add("button-cell");
const table = groupTable();
const row = table.insertRow();
row.append(countCell, expCell, copyCell);
}
function copyGroupText(e) {
const tableRow = e.target.parentElement.parentElement;
const text = tableRow.cells[1].innerText;
if (navigator?.clipboard?.writeText) {
navigator.clipboard.writeText(text);
}
}
function pushAtDepth(parts, part, depth) {
if (depth > 0) {
pushAtDepth(parts[parts.length - 1], part, depth - 1);
} else if (Array.isArray(part)) {
parts.push(part);
} else if (part.isDelimiter) {
parts.push(part);
parts.push("");
} else if (part.isContinuation) {
parts.push("");
} else {
parts[parts.length - 1] += part;
}
}
function textContainsBraces(text, braces) {
for (const character of text) {
if (braces.indexOf(character) !== -1) {
return true;
}
}
return false;
}
function parseGroups(text) {
const braces = omissibleBraces + alternativeBraces;
if (!textContainsBraces(text, braces)) {
return null;
}
const delimiterPart = {
isDelimiter: true
};
const continuationPart = {
isContinuation: true
};
const braceStack = [];
const parts = [""];
for (const character of text) {
const bracePosition = braces.indexOf(character);
if (bracePosition === -1) {
// inside or outside braces
if (delimiters.indexOf(character) === -1) {
pushAtDepth(parts, character, braceStack.length)
} else {
pushAtDepth(parts, delimiterPart, braceStack.length)
}
} else if (bracePosition % 2 === 0) {
// open brace
const part = [""];
if (omissibleBraces.indexOf(character) === -1) {
part.groupType = "alternative";
} else {
part.groupType = "omissible";
}
pushAtDepth(parts, part, braceStack.length);
braceStack.push(bracePosition + 1);
} else if (braceStack.pop() !== bracePosition) {
// failure: unbalanced braces
return null;
} else {
// close brace
pushAtDepth(parts, continuationPart, braceStack.length);
}
}
if (braceStack.length === 0) {
return parts
} else {
// failure: unbalanced braces
return null;
}
}
function parseGroupListener() {
const textBox = document.querySelector("input");
const text = textBox.value;
const parts = parseGroups(text);
if (parts === null) {
return;
}
const terms = partsToSearchTerms(parts);
textBox.value = terms.join(";");
console.log(parts);
insertIntoTable(parts);
}
function main() {
if (document.getElementById("group-parser-style")) {
return;
}
const parseButton = document.createElement("button");
parseButton.type = "button";
parseButton.innerText = "Expand Groups";
parseButton.addEventListener('click', parseGroupListener);
const breaks = document.querySelectorAll("br");
const finalBreak = breaks[breaks.length - 1];
finalBreak.before(" ");
finalBreak.before(parseButton);
const style = document.createElement('style');
style.id = "group-parser-style";
style.innerText = `
.count-cell {
text-align: right;
}
.button-cell {
border: none;
padding-left: 10px;
}
td {
border: 1px solid;
padding: 5px;
}
table {
max-width: 800px;
border-collapse: collapse;
}`;
document.head.appendChild(style);
}
main();