-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_tools.js
180 lines (171 loc) · 6.25 KB
/
my_tools.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
// my_tools.js
// ========
const fs = require('fs');
const path = require('path');
const database = require('./utils/database');
class Token {
constructor(uid, id4nlp, id4identifier, line, text, classname, charStart=0) {
// https://stackoverflow.com/questions/192048/can-an-html-element-have-multiple-ids
// <p id="foo" xml:id="bar">
this.uid = uid; // always incremental and unique (given also to useless space tokens)
this.id4nlp = id4nlp; // id that separate natural language chunks of the same identifiers
this.id4identifier = id4identifier; // id that treat a variable as single token
this.line = line; // id related to the line
this.charStart = charStart; // position of the first start
this.text = text; // textual content of the token
this.classname = classname; // class of the token
}
}
module.exports = {
/**
* Save the mouse trace (movements) as a file.
*
* @param fileID id of the file
* @param content content of the file
* @param baseFolder path where to save your file
* @returns
*/
saveHumanAttentionLog: function (fileId, content, baseFolder='./data/human_attention_log') {
// create file name
var d = new Date();
var currentTimeEvent = d.getTime();
var fileName = fileId + "-" + currentTimeEvent + '.json';
// save file
fs.writeFile(path.join(baseFolder, fileName), content, function (err) {
if (err) return console.log(err);
console.log('Successfully saved');
});
},
/**
* Prepare tokens to be displayed.
*
* @param tokens list raw tokens
* @param tokenClass default class given to all code token
* @returns objTokens list augmented tokens with all info to be printed
*/
prepareCodeBoxText: function (tokens, tokenClass) {
const objTokens = [];
let uid = 0;
let id4nlp = 0;
let id4identifier = 0;
let sameIdentifier = false;
let line = 0;
// true if we are within two <id></id> tokens
for(let i = 0; i < tokens.length; i++) {
t = tokens[i];
if (t == "<id>") {
id4identifier++;
sameIdentifier = true;
} else if (t == "</id>") {
id4identifier++;
sameIdentifier = false;
} else {
// if not a "meta" token then...
// add token as it is
tObj = new Token(uid, id4nlp, id4identifier, line, t, tokenClass);
objTokens.push(tObj);
// increment for the next
uid++;
id4nlp++;
if (sameIdentifier == false) id4identifier++;
}
}
const spacedObjToken = [];
const punctList = ['[',']','{','}','(',')','!',':',',', '.', ';'];
const punctNewLineList = ['{','}',';'];
// add the first sentence start token by defaults
spacedObjToken.push(objTokens[0])
line = 0;
// iteration for spaces and new lines
for(let i = 0; i < (objTokens.length - 1); i++) {
let currentToken = objTokens[i];
let nextToken = objTokens[i + 1];
if (currentToken['id4identifier'] == nextToken['id4identifier']) {
// transform upper case first letter
nextToken['text'] = nextToken['text'].charAt(0).toUpperCase() + nextToken['text'].slice(1);
nextToken['line'] = line;
// add underscore if same identifier
spacedObjToken.push(nextToken);
} else if (!punctList.includes(currentToken['text']) && !punctList.includes(nextToken['text'])) {
// add empty space
// console.log(">" + currentToken['text'] + "< add space >" + nextToken['text'] + "<");
tObj = new Token("", "", "", line, " ", "t");
spacedObjToken.push(tObj);
nextToken['line'] = line;
spacedObjToken.push(nextToken);
} else if (punctNewLineList.includes(currentToken['text'])) {
// add new line
tObj = new Token("", "", "", line, "\n", "t");
spacedObjToken.push(tObj);
// increase line counting
line++;
nextToken['line'] = line;
spacedObjToken.push(nextToken);
} else {
nextToken['line'] = line;
spacedObjToken.push(nextToken);
}
}
let charStart = 0;
let oldLine = 0;
// Add the charStart for every token
for (i = 0; i < spacedObjToken.length; i++) {
let t = spacedObjToken[i];
if (t['line'] == oldLine) {
t['charStart'] = charStart;
} else {
// reset if new line starts
t['charStart'] = 0;
charStart = 0;
}
let len = t['text'].toString().length;
charStart += parseInt(len);
oldLine = t['line'];
}
return spacedObjToken;
// return tokens.join(" ").replace(/[\{] /g, '{\n').replace(/[}] /g, '}\n').replace(/[;] /g, ';\n');
},
/**
* Save the session as a document in mongodb.
*
* @param obj4Mongo object to store in the remote mongodb istance
*/
saveHumanAttentionLogToMongo: function (obj4Mongo) {
console.log("Send To MongoDB");
var d = new Date();
var currentTimeEvent = d.getTime();
obj4Mongo["time"] = parseInt(currentTimeEvent);
database.mongoConnect(collection => {
console.log("run mongo connection");
collection.insertOne(obj4Mongo, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
//console.log(obj4Mongo);
});
//console.log(collection);
});
},
/**
* Save the rating as a document in mongodb.
*
* @param rating4Mongo object to store in the remote mongodb instance
*/
saveRatingToMongo: function (rating4Mongo) {
console.log("Send rating to MongoDB");
var d = new Date();
var currentTimeEvent = d.getTime();
rating4Mongo["time"] = parseInt(currentTimeEvent);
database.mongoConnectRating(collection => {
//console.log("run mongo connection");
collection.insertOne(rating4Mongo, function(err, res) {
if (err) throw err;
console.log("1 rating inserted");
});
});
},
camelize: function (str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
};