[Python] Auto-detect the language for single words or short phrases

Discussão em 'Python' iniciado por Stack, Julho 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently doing a project requiring me to use the {googletrans} library. I tried to run some simple code to see if I had done everything correctly by trying to translate bonjour into English.

    from googletrans import Translator

    translator = Translator()
    print(translator.translate("bonjour"))


    The code is able to execute without any errors, however it doesn't provide a translation and simply prints

    Translated(src=en, dest=en, text=bonjour, pronunciation=None, extra_data="{'confiden...")


    When I specify the source language for the code, it translates with no issues. It is able to auto-detect the language when using longer phrases such as sentences or more explicit cases such as Chinese characters.

    I have tried using another library called {deep-translate} to see if it was just an issue with the {googletrans} library but I encountered the same issues as above.

    I installed a library called {langdetect} to see if the input language could actually be detected in order to determine if the problem was simply on my end. When I printed the results of the detection code using multiple languages it did provide a language output but they were all incorrect.

    After this testing and the testing mentioned in the problem details I established that the library is fully able to detect a language and translate it but not for individual words.

    For the purpose of my project, I need to be able to use the auto-detection functionality for individual words. I know it is possible as the instructor of the course I am following ran the exact same code without any issues.

    Continue reading...

Compartilhe esta Página