我已经构建了一个 Python 脚本来使用普林斯顿英语 Wordnet 中的数据随机创建句子,下面是Gödel、Escher、Bach提供的图表。Callingpython GEB.py会生成一系列无意义的英语句子,例如:
复苏的不美观成本。苔藓指甲。厌恶四十桃。星光隐藏。翻译长袍的面粉,敢于在苹果木上打一拳,重新提出要求。金枪鱼旁边的半叶状货轮。
并将它们保存到 gibberish.txt。这个脚本工作正常。
另一个脚本 ( translator.py) 接受 gibberish.txt 并通过 py-googletrans Python 模块尝试将这些随机句子翻译成葡萄牙语:
from googletrans import Translator
import json
tradutor = Translator()
with open('data.json') as dataFile:
data = json.load(dataFile)
def buscaLocal(keyword):
if keyword in data:
print(keyword + data[keyword])
else:
buscaAPI(keyword)
def buscaAPI(keyword):
result = tradutor.translate(keyword, dest="pt")
data.update({keyword: result.text})
with open('data.json', 'w') as fp:
json.dump(data, fp)
print(keyword + result.text)
keyword = open('/home/user/gibberish.txt', 'r').readline()
buscaLocal(keyword)
Run Code Online (Sandbox Code Playgroud)
目前,第二个脚本仅输出 gibberish.txt 中第一句话的翻译。就像是:
复苏的不美观成本。财产保险。
我曾尝试使用readlines() …