Skip to content

Commit

Permalink
Merge pull request #365 from awwaawwa/bug_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Byaidu authored Dec 28, 2024
2 parents cd3a5a4 + 7850d32 commit 246e3a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pdf2zh/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def _sort_dict_recursively(obj):
return obj

def __init__(self, translate_engine: str, translate_engine_params: dict = None):
assert (
len(translate_engine) < 20
), "current cache require translate engine name less than 20 characters"
self.translate_engine = translate_engine
self.replace_params(translate_engine_params)

Expand Down
3 changes: 1 addition & 2 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def translate(self, text, ignore_cache=False):
return cache

translation = self.do_translate(text)
if not (self.ignore_cache or ignore_cache):
self.cache.set(text, translation)
self.cache.set(text, translation)
return translation

def do_translate(self, text):
Expand Down
10 changes: 5 additions & 5 deletions test/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ def test_add_cache_impact_parameters(self):
self.assertNotEqual(first_result, second_result)

# Test cache with ignore_cache=True
no_cache_result = translator.translate(text, ignore_cache=True)
self.assertNotEqual(first_result, no_cache_result)
no_cache_result1 = translator.translate(text, ignore_cache=True)
self.assertNotEqual(first_result, no_cache_result1)

translator.ignore_cache = True
no_cache_result = translator.translate(text)
self.assertNotEqual(first_result, no_cache_result)
no_cache_result2 = translator.translate(text)
self.assertNotEqual(no_cache_result1, no_cache_result2)

# Test cache with ignore_cache=False
translator.ignore_cache = False
cache_result = translator.translate(text)
self.assertEqual(second_result, cache_result)
self.assertEqual(no_cache_result2, cache_result)

# Test cache with another parameter
translator.add_cache_impact_parameters("test2", "value2")
Expand Down

0 comments on commit 246e3a1

Please sign in to comment.