-
Notifications
You must be signed in to change notification settings - Fork 26
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 #19 from yobix-ai/18-ocr-examples-and-docs
18 ocr examples and docs
- Loading branch information
Showing
24 changed files
with
464 additions
and
55 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
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,46 @@ | ||
from extractous import Extractor, PdfOcrStrategy, PdfParserConfig, TesseractOcrConfig | ||
from utils import cosine_similarity | ||
|
||
def test_ara_ocr_png(): | ||
ocr_config = TesseractOcrConfig().set_language("ara") | ||
extractor = Extractor().set_ocr_config(ocr_config) | ||
result = extractor.extract_file_to_string("../../test_files/documents/ara-ocr.png") | ||
|
||
with open("../../test_files/expected_result/ara-ocr.png.txt", "r", encoding="utf8") as file: | ||
expected = file.read() | ||
|
||
assert cosine_similarity(result, expected) | ||
|
||
|
||
def test_ocr_only_strategy_extract_deu_ocr_pdf_to_string(): | ||
test_file = "../../test_files/documents/eng-ocr.pdf" | ||
expected_result_file = "../../test_files/expected_result/deu-ocr.pdf.txt" | ||
|
||
pdf_config = PdfParserConfig().set_ocr_strategy(PdfOcrStrategy.OCR_ONLY) | ||
ocr_config = TesseractOcrConfig().set_language("deu") | ||
|
||
# Note builder patter is used | ||
extractor = Extractor() | ||
extractor = extractor.set_ocr_config(ocr_config) | ||
extractor = extractor.set_pdf_config(pdf_config) | ||
|
||
result = extractor.extract_file_to_string(test_file) | ||
|
||
with open(expected_result_file, "r", encoding="utf8") as file: | ||
expected = file.read() | ||
|
||
assert cosine_similarity(result, expected) | ||
|
||
def test_no_ocr_strategy_extract_deu_ocr_pdf_to_string(): | ||
test_file = "../../test_files/documents/deu-ocr.pdf" | ||
|
||
pdf_config = PdfParserConfig() | ||
pdf_config = pdf_config.set_ocr_strategy(PdfOcrStrategy.NO_OCR) | ||
ocr_config = TesseractOcrConfig() | ||
ocr_config = ocr_config.set_language("deu") | ||
|
||
extractor = Extractor().set_ocr_config(ocr_config).set_pdf_config(PdfParserConfig().set_ocr_strategy(PdfOcrStrategy.NO_OCR)) | ||
|
||
result = extractor.extract_file_to_string(test_file) | ||
|
||
assert result.strip() == "" |
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,13 @@ | ||
from sklearn.feature_extraction.text import CountVectorizer | ||
from sklearn.metrics.pairwise import cosine_similarity as cosine_sim | ||
|
||
def cosine_similarity(text1, text2): | ||
"""Calculate the cosine similarity between two texts.""" | ||
|
||
# Create the CountVectorizer and transform the texts into vectors | ||
vectorizer = CountVectorizer().fit_transform([text1, text2]) | ||
vectors = vectorizer.toarray() | ||
|
||
# Calculate cosine similarity between the two vectors | ||
cos_sim = cosine_sim(vectors) | ||
return cos_sim[0][1] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "extractous" | ||
version = "0.1.6" | ||
version = "0.1.7" | ||
edition = "2021" | ||
|
||
description = """ | ||
|
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
Oops, something went wrong.