def main(): translator = GoogleTranslator() count = 0 with open("input.txt","r", encoding="utf-8") as fo: lines = fo.readlines() with open('output.txt', 'w', encoding='utf-8') as df: for line in lines: if len(line) > 1: count += 1 print('\r' + str(count), end = '', flush = True) df.write(line.strip() + "\n") result = translator.translate(line) df.write(result.strip() + "\n\n")
def long_time_task(curPage): skip = (curPage - 1) * 10000 data = curTableObj.find({ "status": 1, "ctitle": "" }).sort('_id', pymongo.ASCENDING).limit(100).skip() j = 0 g = GoogleTranslator() for v in data: v['title'] = v['title'].replace(''', '').replace('&', '') s = ctitle = g.translate(v['title']) if s == '': curTableObj.update({"id": v['id']}, {"$set": {"status": 3}}) else: curTableObj.update({"id": v['id']}, {"$set": {"ctitle": ctitle}}) if j % 10: time.sleep(0.5) j += 1
import os, json from ImageAnalyzor import ImageAnalyzor from GoogleTranslator import GoogleTranslator if __name__ == "__main__": analyzor = ImageAnalyzor() url = input("Please input the url of the image: ") analysis = analyzor.analyzeByUrl(url) # path=input("Please input the path of the image: ") # analysis=analyzor.analyzeByLocal(path) print(json.dumps(analysis)) if "captions" in analysis["description"]: image_caption = analysis["description"]["captions"][0][ "text"].capitalize() print(image_caption) t2 = GoogleTranslator(src='en', dest='zh-CN') result = t2.translate(image_caption) print(result)