Python - 英语翻译器

发布于 2024-09-26 09:37:22 字数 46 浏览 3 评论 0原文

用 Python 编写程序将英语单词和/或短语翻译成其他语言的最佳方法是什么?

What is the best way to approach writing a program in Python to translate English words and/or phrases into other languages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

在你怀里撒娇 2024-10-03 09:37:22

AJAX Language API

这是一个极其困难的问题 --语言非常非常非常复杂。想想你必须做的所有事情——解析这个短语,弄清楚这些词的意思,然后翻译它们。这可能不惯用,因此您需要针对不同语法的特殊情况。很多很多特殊情况。如果外语与英语不同,你就需要弄清楚它的语法——例如,西班牙语中的“the big green ball”变成了“the ball big green”。

不要重新发明轮子。谷歌为他们的翻译服务提供了一个API,这无疑让许多聪明的人认真思考了这一点。

AJAX Language API

This is an incredibly difficult problem -- language is very very very complicated. Think about all the things you'd have to do -- parse the phrase, work out what the words mean, translate them. That's probably not idiomatic so you'll need special cases for different syntaxes. Many, many special cases. You'll need to work out the syntax of the foreign language if it differs from English -- "the big green ball" goes to "the ball big green" in Spanish, for instance.

Don't reinvent the wheel. Google provide an API to their translation service, which has undoubtedly had many many clever people thinking really quite hard about it.

不弃不离 2024-10-03 09:37:22

我认为你应该研究一下 Google Translate API。 这里是一个专门用 python 实现此目的的库。

I think you should look into the Google Translate API. Here is a library implemented specifically for this purpose in python.

北凤男飞 2024-10-03 09:37:22

最简单的方法是制作一本字典,将一种语言的单词与另一种语言的单词相匹配。然而,这是非常愚蠢的,根本不考虑语法,并且创建翻译器实际上需要很长时间,特别是如果您计划将其用于多种语言。如果语法对您来说并不重要(例如,如果您正在为游戏或故事创建自己的语言,而该语言的语法与英语没有不同),那么您可以使用字典并简单地让函数查找所请求的内容。字典中匹配

the simplest way to do this is to make a dictionary that matches one language's words to another language's words. However, this is extremely silly and would not take into account grammar at all and it would literally take a very long time to create a translator, especially if you plan to use it for multiple languages. If grammar is not important to you (for example, if you were creating your own language for a game or story that doesn't have grammar different from english) than you could get away with using dictionaries and simply having a function look for a requested match in the dictionary

烟火散人牵绊 2024-10-03 09:37:22
# command : pip install mtranslate
from mtranslate import translate


>>> from mtranslate import translate
>>> translate("Tranalating to kannada language (my mother tongue) ", to_language = "kn")
'ಕನ್ನಡ ಭಾಷೆಗೆ ಅನುವಾದ (ನನ್ನ ಮಾತೃಭಾಷೆ)'
# command : pip install mtranslate
from mtranslate import translate


>>> from mtranslate import translate
>>> translate("Tranalating to kannada language (my mother tongue) ", to_language = "kn")
'ಕನ್ನಡ ಭಾಷೆಗೆ ಅನುವಾದ (ನನ್ನ ಮಾತೃಭಾಷೆ)'
谷夏 2024-10-03 09:37:22

您可以使用 Goslate 包
它非常容易使用
现在的示例

import goslate
print(goslate.Goslate().translate('hello world', 'ar'))

第一个参数是您要翻译的文本,第二个参数是您要翻译的语言。

我希望你会发现这个答案有用

You can use Goslate Package for that
its very easy to use
example

import goslate
print(goslate.Goslate().translate('hello world', 'ar'))

now first argument is text you want to translate and second argument is in which language you want to translate.

i hope you will find the answer usefull

微凉 2024-10-03 09:37:22
 # Please install Microsoft translate using >> pip install translate

 from translate import Translator

 class clsTranslate():

     def translateText(self, strString, strTolang):
         self.strString = strString
         self.strTolang = strTolang
         translator = Translator(to_lang=self.strTolang)
         translation = translator.translate(self.strString)
         return (str(translation))

 # Create a Class object and call the Translate function
 # Pass the language as a parameter to the function, de: German zh: Chinese etc

objTrans=clsTranslate() 

strTranslatedText= objTrans.translateText('Howare you', 'de')

print(strTranslatedText)
 # Please install Microsoft translate using >> pip install translate

 from translate import Translator

 class clsTranslate():

     def translateText(self, strString, strTolang):
         self.strString = strString
         self.strTolang = strTolang
         translator = Translator(to_lang=self.strTolang)
         translation = translator.translate(self.strString)
         return (str(translation))

 # Create a Class object and call the Translate function
 # Pass the language as a parameter to the function, de: German zh: Chinese etc

objTrans=clsTranslate() 

strTranslatedText= objTrans.translateText('Howare you', 'de')

print(strTranslatedText)
温暖的光 2024-10-03 09:37:22

如果您使用深度翻译器,这非常非常容易!这是源代码(确保安装深度翻译模块):

from deep_translator import GoogleTranslator
import time
def start():
      while True:
                 def translate():
        line_to_translate = input('Which line/phrase/word you want to translate?\n')
        to_lang = input('In which language you want to translate it?\n')
        to_lang = to_lang.lower()
        translation = GoogleTranslator(source='auto', target=to_lang).translate(text=line_to_translate)
        return translation

        time.sleep(1 sec)
        esc = (input("Enter 'q' to exit and 'r' to restart.\n"))
    while True:
        if esc.lower() in {'q', 'r'}:
            break
        else:
            print('Please enter a valid Option!!')
            time.sleep(1)
            esc = (input("Enter 'q' to exit and 'r' to restart.\n"))
    if esc.lower() == 'q':
        return
    elif esc.lower() == 'r':
        pass


start()

It's very very easy if you use deep-translator! Here's the source code(make sure to install deep-translator module):

from deep_translator import GoogleTranslator
import time
def start():
      while True:
                 def translate():
        line_to_translate = input('Which line/phrase/word you want to translate?\n')
        to_lang = input('In which language you want to translate it?\n')
        to_lang = to_lang.lower()
        translation = GoogleTranslator(source='auto', target=to_lang).translate(text=line_to_translate)
        return translation

        time.sleep(1 sec)
        esc = (input("Enter 'q' to exit and 'r' to restart.\n"))
    while True:
        if esc.lower() in {'q', 'r'}:
            break
        else:
            print('Please enter a valid Option!!')
            time.sleep(1)
            esc = (input("Enter 'q' to exit and 'r' to restart.\n"))
    if esc.lower() == 'q':
        return
    elif esc.lower() == 'r':
        pass


start()
心碎无痕… 2024-10-03 09:37:22
# command : pip install mtranslate
from mtranslate import translate


>>> from mtranslate import translate
>>> translate("Tranalating to kannada language (my mother tongue) ", to_language = "kn")
'ಕನ್ನಡ ಭಾಷೆಗೆ ಅನುವಾದ (ನನ್ನ ಮಾತೃಭಾಷೆ)'
# command : pip install mtranslate
from mtranslate import translate


>>> from mtranslate import translate
>>> translate("Tranalating to kannada language (my mother tongue) ", to_language = "kn")
'ಕನ್ನಡ ಭಾಷೆಗೆ ಅನುವಾದ (ನನ್ನ ಮಾತೃಭಾಷೆ)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文