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
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ def run(self, documents: list[Document]) -> dict[str, list[Document]]:
return {"documents": new_documents}

def _detect_language(self, document: Document) -> str | None:
language = None
if document.content is None:
logger.warning(
"Langdetect cannot detect the language of Document with id: {document_id}", document_id=document.id
)
return None
try:
language = langdetect.detect(document.content)
return langdetect.detect(document.content)
except langdetect.LangDetectException:
Comment on lines +113 to 120
logger.warning(
"Langdetect cannot detect the language of Document with id: {document_id}", document_id=document.id
)
return language
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Prevent DocumentLanguageClassifier from crashing when ``Document.content=None`` by
marking them as unmatched and logging a warning.
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ def test_warning_if_no_language_detected(self, caplog):
classifier = DocumentLanguageClassifier()
classifier.run(documents=[Document(content=".")])
assert "Langdetect cannot detect the language of Document with id" in caplog.text

def test_none_content_is_unmatched(self, caplog):
with caplog.at_level(logging.WARNING):
classifier = DocumentLanguageClassifier()
result = classifier.run(documents=[Document(content=None)])
assert result["documents"][0].meta["language"] == "unmatched"
assert "Langdetect cannot detect the language of Document with id" in caplog.text
Loading