-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
409 lines (369 loc) · 16.1 KB
/
index.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
const path = require('path');
const Sentiment = require('sentiment');
const sentiment = new Sentiment();
const natural = require('natural');
const analyzer = new natural.SentimentAnalyzer('English', natural.PorterStemmer, 'afinn');
const {
parseDate,
formatDate,
getWeekString,
writeFile,
parsePdf
} = require('./utils');
/**
* Inner function to parse individual messages and extract metadata.
* @param {string} messageBlock - The text block representing a single message.
* @return {Object} - An object containing message data and calculated read times.
*/
function parseMessage(messageBlock) {
// Split the message block on "Page X of Y" to isolate the first page
const pages = messageBlock.split(/Page \d+ of \d+/);
const firstPage = pages[0]; // The content before the first "Page X of Y"
// Extract the metadata block starting from the last occurrence of "Sent:"
const metadataIndex = firstPage.lastIndexOf("Sent:");
const metadataBlock = firstPage.substring(metadataIndex).trim();
let body = firstPage.substring(0, metadataIndex).trim(); // The body is everything before the metadata
if (pages.length > 1) {
const remainingPages = pages.slice(1).join(''); // Join the remaining pages back into a single string
// Clean up the body by trimming trailing newlines
body = body.concat(remainingPages).trim();
}
// console.log(body);
// Initialize message object with the body
const message = {
body: body.trim(),
wordCount: body.split(/\s+/).filter(Boolean).length // Count words in the body
};
// Process the metadata block for details
const metadataLines = metadataBlock.split('\n');
metadataLines.forEach(line => {
if (line.startsWith('Sent:')) {
message.sentDate = parseDate(line.substring(5).trim());
} else if (line.startsWith('From:')) {
message.sender = line.substring(5).trim();
} else if (line.startsWith('Subject:')) {
message.subject = line.substring(8).trim();
}
});
// Calculate word count
message.wordCount = message.body ? message.body.split(/\s+/).length : 0;
// Perform sentiment analysis on the message body
if (message.body) {
const sentimentResult = analyzer.getSentiment(message.body.split(/\s+/));
message.sentiment_natural = sentimentResult;
} else {
message.sentiment_natural = 0;
}
// Perform sentiment analysis on the message body
if (message.body) {
const sentimentResult = sentiment.analyze(message.body);
message.sentiment = sentimentResult.score; // Add the sentiment score to the message object
} else {
message.sentiment = 0; // Default to 0 if there's no message body
}
// Extracting recipient view times and calculating read times
message.recipientReadTimes = {};
// Identify the section of text containing recipients
const recipientSectionMatch = messageBlock.match(/To:(.+?)\nSubject:/s);
if (recipientSectionMatch) {
const recipientSection = recipientSectionMatch[1];
const recipientLines = recipientSection.split('\n');
recipientLines.forEach(line => {
const recipientMatch = line.match(/(.+?)\(First Viewed: (.+?)\)/);
if (recipientMatch) {
const recipient = recipientMatch[1].trim();
const firstViewed = recipientMatch[2].trim();
message.recipientReadTimes[recipient] = firstViewed !== 'Never' ? parseDate(firstViewed) : 'Never';
}
});
}
return message;
}
/**
* Outer function to iterate through all messages in the PDF text.
* @param {string} text - The text extracted from the PDF.
* @return {Array} - An array of message objects with extracted and calculated data.
*/
function processMessages(text) {
const messages = [];
const messageBlocks = text.split(/Message \d+ of \d+/).slice(1); // Assumes each message is separated by 'Message N of M'
messageBlocks.forEach((messageBlock) => {
const message = parseMessage(messageBlock);
messages.push(message);
});
return messages;
}
/**
* Parses the provided PDF file and processes the messages contained within it.
*
* @param {string} inputFilePath - The path to the input PDF file.
* @returns {Promise<Object>} - A promise that resolves to an object containing
* the processed messages, the directory of the input file, and the base file name
* without extension.
*/
async function parsePdfFile(inputFilePath) {
try {
const pdfText = await parsePdf(inputFilePath);
console.log('PDF text parsed');
const messages = processMessages(pdfText);
console.log(`Processed ${messages.length} messages`);
const directory = path.dirname(inputFilePath);
const fileNameWithoutExt = path.basename(inputFilePath, path.extname(inputFilePath));
return { messages, directory, fileNameWithoutExt };
} catch (error) {
console.error(`Failed to process PDF at ${inputFilePath}:`, error);
throw error;
}
}
/**
* Writes the provided messages data to a JSON file in the specified directory, using the base file name.
*
* @param {Object} data - The data object containing messages, directory, and base file name.
* @returns {Promise<Object>} - A promise that resolves to the data object for further processing.
*/
function writeJsonFile(data) {
return new Promise((resolve, reject) => {
try {
const { messages, directory, fileNameWithoutExt } = data;
const jsonData = JSON.stringify(messages, null, 2);
const jsonFilePath = path.join(directory, `${fileNameWithoutExt}.json`);
console.log(`Writing JSON to ${jsonFilePath}`);
writeFile(jsonFilePath, jsonData);
resolve(data); // Pass the data object along for further processing
} catch (error) {
reject(`Failed to write JSON file: ${error}`);
}
});
}
const messageTemplate = (message, index, total) => {
const {
sentDate,
sender,
recipientReadTimes,
wordCount,
sentiment,
sentiment_natural,
subject,
body,
} = message;
return `
-----------------------------------------------------
## Message ${index + 1} of ${total}
- Sent: ${formatDate(sentDate)}
- From: ${sender}
- To:
${Object.entries(recipientReadTimes).map(([recipient, firstViewed]) => ` - ${recipient}: ${formatDate(firstViewed)}`).join('\n')}
- Word Count: ${wordCount}, Sentiment: ${sentiment}, ${sentiment_natural}
- Subject: ${subject}
${body}
`};
function writeMarkDownFile(data) {
return new Promise((resolve, reject) => {
try {
const { messages, directory, fileNameWithoutExt } = data;
let markdownContent = '';
messages.forEach((message, index) => {
const messageContent = messageTemplate(message, index, messages.length);
markdownContent += messageContent;
});
const markdownFilePath = path.join(directory, `${fileNameWithoutExt}.md`);
console.log(`Writing all messages to ${markdownFilePath}`);
writeFile(markdownFilePath, markdownContent);
resolve(data); // Pass the data object along for further processing
} catch (error) {
reject(`Failed to write Markdown file: ${error}`);
}
});
}
/**
* Outputs the provided message statistics to the console in a single Markdown table.
*
* @param {Object} stats - The message statistics object.
*/
function outputMarkdownSummary(totals, stats) {
console.log('\n');
// Output the totals to Markdown
let header = '| Name | Sent | Words | View Time | Avg View Time | Avg. Sentiment | Sentiment ntrl |';
// Output the statistics to Markdown
console.log(header);
let separator = '|------------------|------|-------|-----------|---------------|----------------|----------------|';
console.log(separator);
for (const [person, personTotals] of Object.entries(totals)) {
if (person.includes('Marie') || person.includes('Nora') || person.includes('Henry')) {
continue;
}
const paddedName = person.padEnd(16);
const paddedSent = personTotals.messagesSent.toString().padStart(5);
const paddedTotalTime = (personTotals.totalReadTime).toFixed(1).toString().padStart(10);
const paddedAvgTime = (personTotals.averageReadTime).toFixed(1).toString().padStart(14);
const wordCountDisplay = personTotals.messagesSent > 0 ? personTotals.totalWords.toString().padStart(6) : ' '.padStart(6);
const paddedSentiment = personTotals.avgSentiment.toFixed(2).toString().padStart(14);
const paddedSentiment_natural = personTotals.sentiment_natural.toFixed(2).toString().padStart(14);
const row = `| ${paddedName} |${paddedSent} |${wordCountDisplay} |${paddedTotalTime} |${paddedAvgTime} | ${paddedSentiment} | ${paddedSentiment_natural} |`;
console.log(row);
}
console.log('\n')
header = '| Week | Name | Sent | Words | Avg View Time | Avg. Sentiment | Sentiment ntrl |';
separator = '|-----------------------|------------------|------|-------|---------------|----------------|----------------|';
console.log(separator);
console.log(header);
let previousWeek = null;
for (const [week, weekStats] of Object.entries(stats)) {
console.log(separator);
for (const [person, personStats] of Object.entries(weekStats)) {
const paddedWeek = (previousWeek !== week ? week : '').padEnd(21);
const paddedName = person.padEnd(16);
const paddedSent = personStats.messagesSent.toString().padStart(5);
const paddedAvgTime = (personStats.averageReadTime).toFixed(1).toString().padStart(14);
const wordCountDisplay = personStats.messagesSent > 0 ? personStats.totalWords.toString().padStart(6) : ' '.padStart(6);
const paddedSentiment = personStats.avgSentiment.toFixed(2).toString().padStart(14);
const paddedSentiment_natural = personStats.sentiment_natural.toFixed(2).toString().padStart(14);
const row = `| ${paddedWeek} | ${paddedName} |${paddedSent} |${wordCountDisplay} |${paddedAvgTime} | ${paddedSentiment} | ${paddedSentiment_natural} |`;
console.log(row);
previousWeek = week;
}
}
console.log(separator);
}
/**
* Generates a CSV string from the provided message statistics object,
* and writes this string to a CSV file at the provided file path.
*
* @param {Object} stats - The message statistics object.
* @param {string} filePath - The path to the output CSV file.
*/
function outputCSV(stats, filePath) {
let csvOutput = 'Week,Name,Messages Sent,Messages Read,Average Read Time (minutes),Total Words, Sentiment, Sentiment_natural\n';
for (const [week, weekStats] of Object.entries(stats)) {
for (const [person, personStats] of Object.entries(weekStats)) {
const wordCount = (personStats.totalWords !== undefined) ? personStats.totalWords : '';
csvOutput += `"${week}","${person}",${personStats.messagesSent},${personStats.messagesRead},${personStats.averageReadTime.toFixed(2)},${wordCount},${personStats.avgSentiment.toFixed(2)},${personStats.sentiment_natural.toFixed(2)}\n`;
}
}
console.log(`Writing CSV to ${filePath}`);
writeFile(filePath, csvOutput);
}
/**
* Compiles and outputs message statistics based on the array of messages.
* @param {Array} messages - The array of message objects.
*/
function compileAndOutputStats({ messages, directory, fileNameWithoutExt }) {
/**
* The message statistics object.
* example:
* {
* '2021-01-01': {
* 'Alice': {
* messagesSent: 3,
* messagesRead: 2,
* totalReadTime: 10,
* totalWords: 100
* },
* 'Bob': {
* ...
* }
*/
const stats = {};
const totals = {};
messages.forEach(message => {
const weekString = getWeekString(message.sentDate);
const sender = message.sender;
if (!totals[sender]) {
totals[sender] = {
messagesSent: 0,
messagesRead: 0,
totalReadTime: 0,
totalWords: 0,
sentiment: 0,
sentiment_natural: 0,
averageReadTime: 0,
};
}
if (!stats[weekString]) {
stats[weekString] = {};
}
if (!stats[weekString][sender]) {
stats[weekString][sender] = {
messagesSent: 0,
messagesRead: 0,
totalReadTime: 0,
totalWords: 0,
sentiment: 0,
sentiment_natural: 0,
averageReadTime: 0,
};
}
totals[sender].messagesSent++;
totals[sender].totalWords += message.wordCount;
totals[sender].sentiment += message.sentiment;
totals[sender].sentiment_natural += message.sentiment_natural;
stats[weekString][sender].messagesSent++;
stats[weekString][sender].totalWords += message.wordCount;
stats[weekString][sender].sentiment += message.sentiment;
stats[weekString][sender].sentiment_natural += message.sentiment_natural;
// Increment read message count and total read time for each recipient
for (const [recipient, firstViewed] of Object.entries(message.recipientReadTimes)) {
if (firstViewed !== 'Never') {
const firstViewedDate = new Date(firstViewed);
const readTime = (firstViewedDate - message.sentDate) / 60000;
if (!totals[recipient]) {
totals[recipient] = {
messagesSent: 0,
messagesRead: 0,
totalReadTime: 0,
totalWords: 0,
sentiment: 0,
sentiment_natural: 0,
};
}
if (!stats[weekString][recipient]) {
stats[weekString][recipient] = {
messagesSent: 0,
messagesRead: 0,
totalReadTime: 0,
totalWords: 0,
sentiment: 0,
sentiment_natural: 0,
};
}
stats[weekString][recipient].messagesRead++;
totals[recipient].messagesRead++;
stats[weekString][recipient].totalReadTime += readTime;
totals[recipient].totalReadTime += readTime;
}
}
});
// Calculate total average read time and sentiment for each person
Object.entries(totals).forEach(([sender, total]) => {
totals[sender].averageReadTime = totals[sender].totalReadTime / totals[sender].messagesRead;
totals[sender].avgSentiment = totals[sender].sentiment / totals[sender].messagesSent;
});
// Calculate weekly average read time ans sentiment for each person in each week
for (const week in stats) {
// Sort stats[week] by person's name
stats[week] = Object.fromEntries(Object.entries(stats[week]).sort());
for (const person in stats[week]) {
const personStats = stats[week][person];
personStats.averageReadTime = personStats.messagesRead === 0 ? 0 : personStats.totalReadTime / personStats.messagesRead;
// calculate average of sentiment values
personStats.avgSentiment = personStats.sentiment / personStats.messagesSent;
}
}
// Output the statistics to Markdown (console) and CSV (file)
const csvFilePath = path.join(directory, `${fileNameWithoutExt}.csv`);
outputCSV(stats, csvFilePath);
outputMarkdownSummary(totals, stats);
}
const INPUT_FILE_PATH = process.argv[2];
if (!INPUT_FILE_PATH) {
console.error('No input file path provided');
process.exit(1);
}
// Entry Point
parsePdfFile(INPUT_FILE_PATH)
.then(writeJsonFile)
.then(writeMarkDownFile)
.then(compileAndOutputStats)
.catch(error => {
console.error('Error:', error);
});