-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (60 loc) · 2.07 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
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/hoa.pdf";
PDFJS.getDocument(PDF_URL).then(
function(PDFDocumentInstance) {
//var totalPages = PDFDocumentInstance.numPages;
var pageNumber = 1;
// 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 => {
console.log(str);
const questionList = str.split(/(?:Question[ ]?|Câu[ ]?)(?:[0-9]+)[.|:][ ]/);
let s = "";
console.log(questionList)
questionList.map((text, index) => {
const mark = text.match(/(Mark the letter A, B, C or D(.*))/)
const content = text.split(/[A-Z][.][ ]|[ ][A-Z][ ][ ][.]/);
console.log("Question", index, content[0], "\n");
s += "Question" + ' ' + index + ':' + content[0] + "\n";
content.slice(1).map((cont, index) => {
console.log(`Answer ${String.fromCharCode(index + 65)}:`, cont);
s += `Answer ${String.fromCharCode(index + 65)}:` + cont;
});
console.log("\n");
s += "\n";
});
// fs.writeFileSync("./data.json", s);
};