-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
428 lines (343 loc) · 10.3 KB
/
process.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
const { LanguageServiceClient } = require('@google-cloud/language');
const { retrieveGitignore } = require("./gitignore.js");
const fs = require('fs');
class Suggestion {
constructor(commands, commitMessage = 0) {
this.commands = commands;
this.commitMessage = commitMessage;
}
}
const verbCommandDict = {
commit: function(files) {
return new Suggestion([
"git add " + files.join(" "),
"git commit -m \"$MESSAGE\""
], 1);
},
push: function() {
return new Suggestion([
"git push -u origin $(git rev-parse --abbrev-ref HEAD)"
]);
},
setname: function(name) {
return new Suggestion([`git config user.name "${name}"`]);
},
setemail: function(email) {
return new Suggestion([`git config user.email ${email}`]);
},
gitignore: function() {
return new Suggestion([`git add .gitignore`]);
},
sync: function() {
return new Suggestion([`git push -u origin $(git rev-parse --abbrev-ref HEAD) && git pull || { git pull --no-edit && git push -u origin $(git rev-parse --abbrev-ref HEAD); }`]);
},
pull: function() {
return new Suggestion(["git pull"]);
},
log: function() {
return new Suggestion(["git log"]);
},
diff: function() {
return new Suggestion(["git diff"]);
},
status: function() {
return new Suggestion(["git status"]);
},
stash: function() {
return new Suggestion(["git stash"]);
},
createbranch: function(name) {
return new Suggestion([`git checkout -b ${name}`]);
},
switchbranch: function(name) {
return new Suggestion(["git fetch --all", `git checkout ${name}`]);
},
initrepo: function() {
return new Suggestion(["git init"]);
},
merge: function(branchname) {
return new Suggestion([`git merge ${branchname}`]);
}
}
// Creates a client
const client = new LanguageServiceClient("./apikey.json");
async function tokenalyzeSyntax(text) {
// Prepares a document, representing the provided text
const document = {
content: text,
type: 'PLAIN_TEXT',
};
// Detects syntax in the document
var tokens;
await client.analyzeSyntax({ document })
.then(results => {
const syntax = results[0];
tokens = syntax.tokens;
})
.catch(err => console.error(err));
return tokens;
}
async function tokenalyzeEntities(text) {
const document = {
content: text,
type: 'PLAIN_TEXT',
};
var entities;
await client
.analyzeEntities({ document })
.then(results => {
entities = results[0].entities;
})
.catch(err => {
console.error('ERROR:', err);
});
return entities;
}
const automl = require('@google-cloud/automl');
const automl_client = new automl.v1beta1.PredictionServiceClient({
// optional auth parameters.
});
const formattedName = automl_client.modelPath('gitdo-222813', 'us-central1', 'TCN2677408050797652826');
async function predict(text) {
var results;
const request = {
name: formattedName,
payload: {'textSnippet': {'content': text, 'mimeType': 'text/plain' }},
};
await automl_client.predict(request)
.then(responses => {
results = responses[0];
})
.catch(err => {
console.error(err);
});
return results;
}
function getVerbNounPairs(tokens) {
verbNounPairs = {};
let i = 0;
tokens.forEach(token => {
if (token.partOfSpeech.tag == "NOUN") {
if (token.text.content.toLowerCase() == "push") {
verbNounPairs[i] = [];
}
let cur = token;
let index;
while (index != (index = cur.dependencyEdge.headTokenIndex)) {
if (tokens[index].partOfSpeech.tag == "VERB") {
// if (cur.partOfSpeech.tag == "NOUN") {
if (index in verbNounPairs) {
verbNounPairs[index].push(token);
} else {
verbNounPairs[index] = [];
verbNounPairs[index].push(token);
}
// }
}
cur = tokens[index];
}
} else if (token.partOfSpeech.tag == "VERB") {
if (!(i in verbNounPairs)) {
verbNounPairs[i] = [];
}
}
i++;
});
return verbNounPairs;
}
let predicted = {};
function getPredictedSuggestion(prediction) {
let commands = [];
for (p of prediction.payload) {
if (predicted[p.displayName] != true && p.classification.score > 0.7) {
predicted[p.displayName] = true;
switch (p.displayName) {
case "log": {
commands.push(verbCommandDict.log());
break;
}
case "pull": {
commands.push(verbCommandDict.pull());
break;
}
case "status": {
commands.push(verbCommandDict.status());
break;
}
case "push": {
commands.push(verbCommandDict.push());
break;
}
case "stash": {
commands.push(verbCommandDict.stash());
break;
}
case "diff": {
commands.push(verbCommandDict.diff());
break;
}
}
}
}
return reduceSuggestions(commands);
}
async function verbNounPairToCommand(verb, nouns, text, file_mapping) {
verb = verb.toLowerCase();
switch (verb) {
case "add" :
// continues to commit
case "commit":
let files = nouns;
let addedAllImages = false;
if (nouns.length == 0) {
files = ["-A"];
} else if (nouns.indexOf("everything") > -1 || nouns.indexOf("all") > -1) {
files = ["-A"];
} else {
let new_arr = [];
for (var file of files) {
if (file in file_mapping) {
new_arr.push(file_mapping[file])
} else {
if (!addedAllImages && (file.toLowerCase() == "images" || file.toLowerCase() == "pictures")) {
addedAllImages = true;
fs.readdirSync("./").forEach(file => {
if (/.+\.(ase|art|bmp|blp|cd5|cit|cpt|cr2|cut|dds|dib|djvu|egt|exif|gif|gpl|grf|icns|ico|iff|jng|jpeg|jpg|jfif|jp2|jps|lbm|max|miff|mng|msp|nitf|ota|pbm|pc1|pc2|pc3|pcf|pcx|pdn|pgm|PI1|PI2|PI3|pict|pct|pnm|pns|ppm|psb|psd|pdd|psp|px|pxm|pxr|qfx|raw|rle|sct|sgi|rgb|int|bw|tga|tiff|tif|vtf|xbm|xcf|xpm|3dv|amf|ai|awg|cgm|cdr|cmx|dxf|e2d|egt|eps|fs|gbr|odg|svg|stl|vrml|x3d|sxd|v2d|vnd|wmf|emf|art|xar|png|webp|jxr|hdp|wdp|cur|ecw|iff|lbm|liff|nrrd|pam|pcx|pgf|sgi|rgb|rgba|bw|int|inta|sid|ras|sun|tga)/.test(file)) {
new_arr.push(file);
}
});
} else {
new_arr.push(file);
}
}
}
files = new_arr
}
return verbCommandDict.commit(files);
case "push":
predicted.push = true;
return verbCommandDict.push();
case "set":
suggestions = [];
if (nounsContains(nouns, "email")) {
var regexEmail = /[a-z0-9\._%+!$&*=^|~#%'`?{}/\-]+@([a-z0-9\-]+\.){1,}([a-z]{2,16})/;
suggestions.push(verbCommandDict.setemail(text.match(regexEmail)[0]));
}
if (nounsContains(nouns, "name")) {
const entities = await tokenalyzeEntities(text);
suggestions.push(verbCommandDict.setname(getName(entities)));
}
return reduceSuggestions(suggestions);
case "create":
return createSuggestion(nouns);
case "make":
return createSuggestion(nouns);
case "switch":
return changeBranchSuggestion(nouns);
case "change":
return changeBranchSuggestion(nouns);
case "go":
return changeBranchSuggestion(nouns);
case "enter":
return changeBranchSuggestion(nouns);
case "ignore":
if (await retrieveGitignore(nouns.map(n => n.toLowerCase()))) {
return verbCommandDict.gitignore();
}
case "sync":
predicted.pull = true;
predicted.push = true;
return verbCommandDict.sync();
case "merge":
return verbCommandDict.merge();
default:
if (nouns.filter(n => n.toLowerCase() == "sync").length > 0) {
predicted.pull = true;
predicted.push = true;
return verbCommandDict.sync();
}
return new Suggestion([]);
}
}
function createSuggestion(nouns) {
suggestions = [];
let i = 0;
for (n of nouns) {
if (n.toLowerCase() == "branch") {
suggestions.push(verbCommandDict.createbranch(nouns[i + 1]));
predicted.log = true;
}
if (n.toLowerCase() == "repository" || n.toLowerCase() == "repo" || n.toLowerCase() == "project" ) {
suggestions.push(verbCommandDict.initrepo(nouns[i + 1]));
}
}
return reduceSuggestions(suggestions);
}
function changeBranchSuggestion(nouns) {
suggestions = [];
let i = 0;
for (n of nouns) {
if (n.toLowerCase() == "branch") {
suggestions.push(verbCommandDict.switchbranch(nouns[i + 1]));
predicted.log = true;
}
}
return reduceSuggestions(suggestions);
}
function nounsContains(nouns, word) {
for (var n of nouns) {
if (n.toLowerCase() == word) {
return true;
}
}
return false;
}
function getName(entities) {
for (var e of entities) {
if (e.type == "PERSON") {
return e.name;
}
}
throw "Name not found in text.";
}
function reduceSuggestions(suggestions) {
let commands = [];
let commitMessage = 0;
for (let suggestion of suggestions) {
commands = commands.concat(suggestion.commands);
commitMessage += suggestion.commitMessage;
}
return new Suggestion(commands, commitMessage);
}
async function processCommand(text) {
let text_tokens = text.split(/(,|\.)?( |$)/);
let file_mapping = {};
let new_text = [];
let i = 0;
for (t of text_tokens) {
if (/^[\w,\s-]+\.[A-Za-z]+/.test(t)) {
let file = "file" + i.toString();
file_mapping[file] = t;
i++;
new_text.push(file);
} else {
new_text.push(t);
}
}
text = new_text.join(" ") + ".";
const tokensPromise = tokenalyzeSyntax(text);
const predictedPromise = predict(text);
const tokens = await tokensPromise;
verbNounPairs = getVerbNounPairs(tokens);
let suggestions = [];
for (verbId in verbNounPairs) {
nouns = verbNounPairs[verbId].map(noun => noun.text.content);
const new_suggestion = await verbNounPairToCommand(tokens[verbId].text.content, nouns, text, file_mapping);
suggestions.push(new_suggestion);
}
const predicted = await predictedPromise;
suggestions.push(getPredictedSuggestion(predicted));
return reduceSuggestions(suggestions);
}
module.exports = { Suggestion, processCommand };
// stuff