Skip to content
New issue

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

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

Already on GitHub? # to your account

Exception when translate a string with digit #216

Closed
vgdh opened this issue Jun 5, 2023 · 4 comments · Fixed by #220
Closed

Exception when translate a string with digit #216

vgdh opened this issue Jun 5, 2023 · 4 comments · Fixed by #220

Comments

@vgdh
Copy link

vgdh commented Jun 5, 2023

  • deep_translator version: 1.11.1
  • Python version: 3.10
  • Operating System: ubuntu 22

Description

I pass a string that contain only one number and got this exception.

What I Did

from deep_translator import GoogleTranslator
tr = GoogleTranslator(source="auto", target="german").translate("1")
Exception has occurred: NotValidPayload       (note: full exception trace is shown but execution is paused at: <module>)
1 --> text must be a valid text with maximum 5000 character,otherwise it cannot be translated
  File "/root/pyproj/src/translate_Google.py", line 6, in <module> (Current frame)
    tr = GoogleTranslator(source="auto", target="german").translate("1")
deep_translator.exceptions.NotValidPayload: 1 --> text must be a valid text with maximum 5000 character,otherwise it cannot be translated
@Vincent-Stragier
Copy link
Contributor

Hi @vgdh,

A priori, the issue comes from this module. It raises an exception if the text is not a string or the string is a digit. See:

def is_input_valid(
text: str, min_chars: int = 0, max_chars: Optional[int] = None
) -> bool:
"""
validate the target text to translate
@param min_chars: min characters
@param max_chars: max characters
@param text: text to translate
@return: bool
"""
if not isinstance(text, str) or text.isdigit():
raise NotValidPayload(text)
if max_chars and (not min_chars <= len(text) < max_chars):
raise NotValidLength(text, min_chars, max_chars)
return True

I don't know if it is on purpose or a bug… To fix this specific problem, you could change the condition on line 23 from if not isinstance(text, str) or text.isdigit(): to if not (isinstance(text, str) or text.isdigit()): or if not isinstance(text, str) or not text.isdigit(): or simply if not isinstance(text, str):.

Or catch the exception before it occurs and automatically return the digit… well, it is probably not the best solution for languages that do not use Arabic figures.

@dinhanhx
Copy link

dinhanhx commented Jun 10, 2023

I think deep-translator should take digits as inputs. Let have a look at google,
image
image

@vgdh And yes, I also encounter the exact exception.

@vgdh vgdh changed the title Exception when translate a string with one character Exception when translate a string with digit Jun 10, 2023
@dinhanhx
Copy link

num2words can be used to address the problem.

@nidhaloff
Copy link
Owner

@vgdh I think you have a good point there. When I created the module, I thought that it wouldn't make sense to pass a digit as input, but yes I will update this one. At least for the google translator. Thanks for the input

@nidhaloff nidhaloff linked a pull request Jun 25, 2023 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants