模块或 API 以获取特定语言的随机短语

Module or API to get random phrases in specific languages

我想构建一个 Python 脚本,我需要随机获取整个短语以保存在列表中 是否有任何 API 或模块可以随机给我所选语言的完整短语? ,比方说,来自网站、短语、引语等的在线国际数据库...... 比方说:

phrase = get_random_spanish_phrase('es')
print(phrase)

它会做类似

的事情
esta es una frase en español

我使用模块 wonderwords 来安装:

pip install wonderwords 

Wonderwords 允许 Python 与数据库交互并生成随机的英语单词或短语。为了回答这个问题,我找到了使用 googletrans API 获取任何语言的短语或单词的解决方案,然后我执行了以下操作:

首先,我开始导入这两个模块,然后从模块 wonderwords 导入了函数 RandomSentence ,它会请求一个随机句子,然后 return 它。我从模块 googletrans 导入了 Translator 函数,它可以帮助我翻译从 wonderwords 生成的句子,将句子传递给另一个变量并翻译变量的内容。 代码看起来像这样

from googletrans import Translator
from wonderwords import RandomSentence

translator = Translator() #defining the translator object
sentence = RandomSentence() #defining the random sentence

'''now we create another variable that would store the translated random sentence in the language that we want'''

translated_sentence = translator.translate(sentence, src='en', dest='es') 
'''here we are passing as parameters 1) the sentence that we want to translate 2) the language of the source sentence (that in this case is on English) 3)the language that we want to translate to, (in this case is Spanish)'''

#finally, we convert that translated sentence into a string

translated_wanted_sentence = translated_sentence.text 
'''we get the translated_sentence converted into a string with the .text method'''