Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep original text length #247

Merged
merged 2 commits into from
Feb 19, 2025
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
12 changes: 11 additions & 1 deletion ammico/test/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ def test_truncate_text(accepted):
test_obj.subdict["text"] = "I like cats and dogs."
test_obj._truncate_text()
assert test_obj.subdict["text"] == "I like cats and dogs."
assert "text_truncated" not in test_obj.subdict
test_obj.subdict["text"] = 20000 * "m"
test_obj._truncate_text()
assert test_obj.subdict["text"] == 5000 * "m"
assert test_obj.subdict["text_truncated"] == 5000 * "m"
assert test_obj.subdict["text"] == 20000 * "m"


@pytest.mark.gcv
Expand All @@ -173,6 +175,14 @@ def test_analyse_image(set_testdict, set_environ, accepted):
set_testdict[item], analyse_text=True, accept_privacy=accepted
)
test_obj.analyse_image()
testdict = {}
testdict["text"] = 20000 * "m"
test_obj = tt.TextDetector(
testdict, skip_extraction=True, analyse_text=True, accept_privacy=accepted
)
test_obj.analyse_image()
assert test_obj.subdict["text_truncated"] == 5000 * "m"
assert test_obj.subdict["text"] == 20000 * "m"


@pytest.mark.gcv
Expand Down
11 changes: 8 additions & 3 deletions ammico/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def _truncate_text(self, max_length: int = 5000) -> str:
"""Truncate the text if it is too long for googletrans."""
if self.subdict["text"] and len(self.subdict["text"]) > max_length:
print("Text is too long - truncating to {} characters.".format(max_length))
self.subdict["text"] = self.subdict["text"][:max_length]
self.subdict["text_truncated"] = self.subdict["text"][:max_length]

def analyse_image(self) -> dict:
"""Perform text extraction and analysis of the text.
Expand All @@ -283,7 +283,7 @@ def analyse_image(self) -> dict:
self._truncate_text()
self.translate_text()
self.remove_linebreaks()
if self.analyse_text:
if self.analyse_text and self.subdict["text_english"]:
self._run_spacy()
self.clean_text()
self.text_summary()
Expand Down Expand Up @@ -336,8 +336,13 @@ def translate_text(self):
raise ValueError(
"Privacy disclosure not accepted - skipping text translation."
)
text_to_translate = (
self.subdict["text_truncated"]
if "text_truncated" in self.subdict
else self.subdict["text"]
)
try:
translated = self.translator.translate(self.subdict["text"])
translated = self.translator.translate(text_to_translate)
except Exception:
print("Could not translate the text with error {}.".format(Exception))
translated = None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "ammico"
version = "0.2.5"
version = "0.2.6"
description = "AI Media and Misinformation Content Analysis Tool"
readme = "README.md"
maintainers = [
Expand Down