pdfxtract 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +30 -2
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -7,14 +7,42 @@ import { extractText, getDocumentProxy } from "unpdf";
|
|
|
7
7
|
|
|
8
8
|
//#region src/converter.ts
|
|
9
9
|
const VERBOSITY_ERRORS = 0;
|
|
10
|
+
const LINE_BREAK_HYPHEN = /([A-Za-z]{2,})-\n([A-Za-z]{2,})/g;
|
|
11
|
+
const WORD_SEPARATOR = /[^A-Za-z'-]+/;
|
|
12
|
+
/**
|
|
13
|
+
* Words the document spells out without crossing a line break. Fragments left
|
|
14
|
+
* by hyphenation are dropped rather than flattened, so they cannot vouch for
|
|
15
|
+
* themselves when `dehyphenate` consults this set.
|
|
16
|
+
*/
|
|
17
|
+
function buildVocabulary(text) {
|
|
18
|
+
return new Set(text.replace(LINE_BREAK_HYPHEN, "\n").split(WORD_SEPARATOR).filter(Boolean).map((word) => word.toLowerCase()));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* PDF.js reports every hyphen as U+002D, so one sitting before a line break is
|
|
22
|
+
* ambiguous: it may be typesetting hyphenation ("prob-\nlem") or part of a real
|
|
23
|
+
* compound ("machine-\nlearning"). Resolve each occurrence against the rest of
|
|
24
|
+
* the document, preferring whichever spelling is attested elsewhere. When
|
|
25
|
+
* neither is, keep the hyphen only if both halves stand alone as words.
|
|
26
|
+
*/
|
|
27
|
+
function dehyphenate(text) {
|
|
28
|
+
const vocabulary = buildVocabulary(text);
|
|
29
|
+
return text.replace(LINE_BREAK_HYPHEN, (_match, head, tail) => {
|
|
30
|
+
const joined = `${head}${tail}`;
|
|
31
|
+
const hyphenated = `${head}-${tail}`;
|
|
32
|
+
if (vocabulary.has(joined.toLowerCase())) return joined;
|
|
33
|
+
if (vocabulary.has(hyphenated.toLowerCase())) return hyphenated;
|
|
34
|
+
const halvesAreWords = vocabulary.has(head.toLowerCase()) && vocabulary.has(tail.toLowerCase());
|
|
35
|
+
return halvesAreWords ? hyphenated : joined;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
10
38
|
async function convertPdf(pdfPath, txtPath) {
|
|
11
39
|
const buffer = await readFile(pdfPath);
|
|
12
40
|
const pdf = await getDocumentProxy(new Uint8Array(buffer), { verbosity: VERBOSITY_ERRORS });
|
|
13
41
|
try {
|
|
14
42
|
const { text } = await extractText(pdf, { mergePages: true });
|
|
15
|
-
await writeFile(txtPath, text, "utf-8");
|
|
43
|
+
await writeFile(txtPath, dehyphenate(text), "utf-8");
|
|
16
44
|
} finally {
|
|
17
|
-
pdf.destroy();
|
|
45
|
+
await pdf.loadingTask.destroy();
|
|
18
46
|
}
|
|
19
47
|
}
|
|
20
48
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdfxtract",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Recursively convert PDF files to TXT in a directory",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"url": "https://github.com/NaokiYazawa/pdfxtract/issues"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"unpdf": "^
|
|
41
|
+
"unpdf": "^1.8.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@biomejs/biome": "2.4.6",
|