-
Notifications
You must be signed in to change notification settings - Fork 27
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 #22 from yobix-ai/7-implement-extracting-from-an-a…
…rray-of-bytes 7 implement extracting from an array of bytes
- Loading branch information
Showing
21 changed files
with
653 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import sys | ||
|
||
from extractous import Extractor, PdfOcrStrategy, PdfParserConfig | ||
|
||
|
||
def extract_to_stream(file_path: str): | ||
|
||
# Extract the file | ||
extractor = Extractor() | ||
reader = extractor.extract_file(in_file) | ||
|
||
buffer = bytearray(4096 * 4096) | ||
while True: | ||
bytes_read = reader.readinto(buffer) | ||
# If no more data, exit the loop | ||
if bytes_read == 0: | ||
break | ||
# Decode the valid portion of the buffer and append it to the result | ||
chunk = buffer[:bytes_read].decode('utf-8') | ||
print(chunk) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Pare input args | ||
if len(sys.argv) != 2: | ||
print(f"Usage: '{sys.argv[0]}' <filename>") | ||
sys.exit(1) | ||
in_file = sys.argv[1] | ||
if not os.path.isfile(in_file): | ||
raise FileNotFoundError(f"No such file: '{in_file}'") | ||
|
||
extract_to_stream(in_file) |
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
40 changes: 40 additions & 0 deletions
40
bindings/extractous-python/tests/test_extract_bytes_to_stream.py
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,40 @@ | ||
import pytest | ||
|
||
from extractous import Extractor | ||
from utils import cosine_similarity, read_to_string, read_file_to_bytearray | ||
|
||
TEST_CASES = [ | ||
("2022_Q3_AAPL.pdf", 0.9), | ||
("science-exploration-1p.pptx", 0.9), | ||
("simple.odt", 0.9), | ||
("table-multi-row-column-cells-actual.csv", 0.9), | ||
("vodafone.xlsx", 0.4), | ||
("category-level.docx", 0.9), | ||
("simple.doc", 0.9), | ||
("simple.pptx", 0.9), | ||
("table-multi-row-column-cells.png", -1.0), | ||
("winter-sports.epub", 0.9), | ||
("bug_16.docx", 0.9), | ||
#("eng-ocr.pdf", 0.9), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("file_name, target_dist", TEST_CASES) | ||
def test_extract_bytes_to_stream(file_name, target_dist): | ||
"""Test the extraction from bytes of various file types.""" | ||
original_filepath = f"../../test_files/documents/{file_name}" | ||
expected_result_filepath = f"../../test_files/expected_result/{file_name}.txt" | ||
|
||
file_bytes = read_file_to_bytearray(original_filepath) | ||
|
||
extractor = Extractor() | ||
reader = extractor.extract_bytes(file_bytes) | ||
result = read_to_string(reader) | ||
|
||
# Expected | ||
with open(expected_result_filepath, "r", encoding="utf8") as file: | ||
expected = file.read() | ||
|
||
assert cosine_similarity(result, expected) > target_dist, \ | ||
f"Cosine similarity is less than {target_dist} for file: {file_name}" | ||
|
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,10 @@ | ||
from extractous import Extractor | ||
from utils import read_to_string | ||
|
||
def test_extract_url(): | ||
extractor = Extractor() | ||
|
||
reader = extractor.extract_url("https://www.google.com") | ||
result = read_to_string(reader) | ||
|
||
assert "Google" in result |
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.