-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-1.js
74 lines (64 loc) · 2.05 KB
/
index-1.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
const PDFJS = require("pdfjs-dist");
const nlp = require("compromise");
function getPageText(pageNum, PDFDocumentInstance) {
// Return a Promise that is solved once the text of the page is retrieven
return new Promise(function(resolve, reject) {
PDFDocumentInstance.getPage(pageNum).then(function(pdfPage) {
// The main trick to obtain the text of the PDF page, use the getTextContent method
pdfPage.getTextContent().then(function(textContent) {
var textItems = textContent.items;
var finalString = "";
// Concatenate the string of the item to the final string
for (var i = 0; i < textItems.length; i++) {
var item = textItems[i];
finalString += item.str + " ";
}
// Solve promise with the text retrieven from the page
resolve(finalString);
});
});
});
}
/**
* Extract the test from the PDF
*/
var PDF_URL = "./pdf/su.pdf";
PDFJS.getDocument(PDF_URL).then(
function(PDFDocumentInstance) {
//var totalPages = PDFDocumentInstance.numPages;
var pageNumber = 6;
// Extract the text
getPageText(pageNumber, PDFDocumentInstance).then(function(textPage) {
// Show the text of the page in the console
//console.log(textPage);
handleText(textPage);
});
},
function(reason) {
// PDF loading error
console.error(reason);
}
);
const handleText = str => {
const questionList = str.split("Mark the letter A, B, C or D ");
// console.log(questionList);
questionList.map((text, index) => {
const group = text.split(/(?:Question[ ]|Câu[ ])/);
console.log(
"Type: Mark the letter A, B, C or D ",
group[0].normalize("NFKD"),
"\n"
);
group.slice(1).map((cont, index) => {
const content = cont.split(/[A-Z][.][ ]/);
console.log(`Question: ${content[0].normalize("NFKD")}`);
content.slice(1).map((ans, index) => {
console.log(
`Answer ${String.fromCharCode(index + 65)}:`,
ans.normalize("NFKD")
);
});
});
console.log("\n");
});
};