Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dist
.mypy_cache
.venv
.pytest_cache
.idea
**/__pycache__
21 changes: 19 additions & 2 deletions airbyte_cdk/sources/file_based/file_types/unstructured_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@
FileType,
detect_filetype,
)
import nltk

unstructured_partition_pdf = None
unstructured_partition_docx = None
unstructured_partition_pptx = None

try:
nltk.data.find("tokenizers/punkt.zip")
nltk.data.find("tokenizers/punkt_tab.zip")
except LookupError:
nltk.download("punkt")
nltk.download("punkt_tab")
Comment on lines +44 to +49
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.



def optional_decode(contents: Union[str, bytes]) -> str:
if isinstance(contents, bytes):
Expand Down Expand Up @@ -162,6 +170,10 @@ def parse_records(
logger.warn(f"File {file.uri} cannot be parsed. Skipping it.")
else:
raise e
except Exception as e:
exception_str = str(e)
logger.error(f"File {file.uri} caused an error during parsing: {exception_str}.")
raise e

def _read_file(
self,
Expand All @@ -186,7 +198,7 @@ def _read_file(
remote_file,
self._get_file_type_error_message(filetype),
)
if filetype in {FileType.MD, filetype is FileType.TXT}:
if filetype in {FileType.MD, FileType.TXT}:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

file_content: bytes = file_handle.read()
decoded_content: str = optional_decode(file_content)
return decoded_content
Expand Down Expand Up @@ -418,7 +430,12 @@ def _render_markdown(self, elements: List[Any]) -> str:

def _convert_to_markdown(self, el: Dict[str, Any]) -> str:
if dpath.get(el, "type") == "Title":
heading_str = "#" * (dpath.get(el, "metadata/category_depth", default=1) or 1)
category_depth = dpath.get(el, "metadata/category_depth", default=1) or 1
if not isinstance(category_depth, int):
category_depth = (
int(category_depth) if isinstance(category_depth, (str, float)) else 1
)
heading_str = "#" * category_depth
Comment on lines +433 to +438
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

return f"{heading_str} {dpath.get(el, 'text')}"
elif dpath.get(el, "type") == "ListItem":
return f"- {dpath.get(el, 'text')}"
Expand Down