-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from SamEdwardes/feature/page-range
Add support for page_range in pdf_reader
- Loading branch information
Showing
5 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import pytest | ||
import spacy | ||
|
||
from spacypdfreader.parsers import pdfminer, pytesseract | ||
from spacypdfreader.spacypdfreader import pdf_reader | ||
|
||
|
||
def pdf_assertions(doc: spacy.tokens.Doc): | ||
# Page numbers. | ||
assert doc[0]._.page_number == 2 | ||
assert doc[-1]._.page_number == 3 | ||
# Doc attributes. | ||
assert doc._.page_range == (2, 3) | ||
assert doc._.first_page == 2 | ||
assert doc._.last_page == 3 | ||
assert doc._.pdf_file_name == "tests/data/test_pdf_01.pdf" | ||
|
||
|
||
def test_page_range_pdfminer_single(): | ||
nlp = spacy.load("en_core_web_sm") | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", nlp, pdfminer.parser, page_range=(2, 3) | ||
) | ||
pdf_assertions(doc) | ||
|
||
|
||
def test_page_range_pdfminer_multi(): | ||
nlp = spacy.load("en_core_web_sm") | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", | ||
nlp, | ||
pdfminer.parser, | ||
page_range=(2, 3), | ||
n_processes=2, | ||
) | ||
pdf_assertions(doc) | ||
|
||
|
||
def test_page_range_pytesseract_single(): | ||
nlp = spacy.load("en_core_web_sm") | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", nlp, pytesseract.parser, page_range=(2, 3) | ||
) | ||
pdf_assertions(doc) | ||
|
||
|
||
def test_page_range_pytesseract_multi(): | ||
nlp = spacy.load("en_core_web_sm") | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", | ||
nlp, | ||
pytesseract.parser, | ||
page_range=(2, 3), | ||
n_processes=2, | ||
) | ||
pdf_assertions(doc) | ||
|
||
|
||
def test_page_range_logic(): | ||
nlp = spacy.load("en_core_web_sm") | ||
with pytest.raises(ValueError): | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", | ||
nlp, | ||
pytesseract.parser, | ||
page_range=(10, 20), | ||
n_processes=2, | ||
) | ||
with pytest.raises(ValueError): | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", | ||
nlp, | ||
pytesseract.parser, | ||
page_range=(-1, 2), | ||
n_processes=2, | ||
) | ||
with pytest.raises(ValueError): | ||
doc = pdf_reader( | ||
"tests/data/test_pdf_01.pdf", | ||
nlp, | ||
pytesseract.parser, | ||
page_range=(3, 1), | ||
n_processes=2, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters