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
def init(self): debug("GoogleTranslatorApplet: init().") BasePlasmoid.init(self) self._widget = None self.setHasConfigurationInterface(False) self.setAspectRatioMode(Plasma.IgnoreAspectRatio) self.notifier = Notifier(self) self.nwmon = NetworkMonitor() if self.nwmon.connected(): self.setBusy(False) else: self.notifier.notify("waiting-for-network", i18n("Waiting for network connection.")) self.nwmon.status_changed.connect(self.netstate_changed) # Main widget debug("GoogleTranslatorApplet: Creating main widget.") self._widget = GoogleTranslator(self) self._widget.init() self.setGraphicsWidget(self._widget) self.setPopupIcon(self.metadata.pluginName()) self.setGraphicsWidget(self._widget) # Tool tip for panel self.metadata = self.package().metadata() self.tooltipdata = Plasma.ToolTipContent() self.tooltipdata.setMainText(self.metadata.name()) self.tooltipdata.setSubText(self.metadata.description()) self.tooltipdata.setImage(KIcon(self.metadata.pluginName())) Plasma.ToolTipManager.self().setContent(self.applet, self.tooltipdata) # Only register the tooltip in panels #if (self.formFactor() != Plasma.Planar): if ((self.formFactor() == Plasma.Horizontal) or (self.formFactor() == Plasma.Vertical)): print("GoogleTranslatorApplet: In Panel") Plasma.ToolTipManager.self().registerWidget(self.applet) else: Plasma.ToolTipManager.self().unregisterWidget(self.applet) print("GoogleTranslatorApplet: Not in Panel") self.configChanged()
class GoogleTranslatorApplet(BasePlasmoid): def __init__(self,parent,args=None): self.parent = parent BasePlasmoid.__init__(self, parent) def init(self): debug("GoogleTranslatorApplet: init().") BasePlasmoid.init(self) self._widget = None self.setHasConfigurationInterface(False) self.setAspectRatioMode(Plasma.IgnoreAspectRatio) self.notifier = Notifier(self) self.nwmon = NetworkMonitor() if self.nwmon.connected(): self.setBusy(False) else: self.notifier.notify("waiting-for-network", i18n("Waiting for network connection.")) self.nwmon.status_changed.connect(self.netstate_changed) # Main widget debug("GoogleTranslatorApplet: Creating main widget.") self._widget = GoogleTranslator(self) self._widget.init() self.setGraphicsWidget(self._widget) self.setPopupIcon(self.metadata.pluginName()) self.setGraphicsWidget(self._widget) # Tool tip for panel self.metadata = self.package().metadata() self.tooltipdata = Plasma.ToolTipContent() self.tooltipdata.setMainText(self.metadata.name()) self.tooltipdata.setSubText(self.metadata.description()) self.tooltipdata.setImage(KIcon(self.metadata.pluginName())) Plasma.ToolTipManager.self().setContent(self.applet, self.tooltipdata) # Only register the tooltip in panels #if (self.formFactor() != Plasma.Planar): if ((self.formFactor() == Plasma.Horizontal) or (self.formFactor() == Plasma.Vertical)): print("GoogleTranslatorApplet: In Panel") Plasma.ToolTipManager.self().registerWidget(self.applet) else: Plasma.ToolTipManager.self().unregisterWidget(self.applet) print("GoogleTranslatorApplet: Not in Panel") self.configChanged() def configChanged(self): debug("GoogleTranslator: configChanged") # TODO: # Clear ComboBoxes # Add languages # Select languages # Translate defaultSize = QVariant(QSize (0,0)) size = self.cfg.readEntry("size", defaultSize ).toSize() if (size != defaultSize) : self._widget.setPreferredSize(size.width(), size.height()) else: self._widget.setPreferredSize(400 ,200) self.connect(self._widget, SIGNAL("resized()"), self.dialog_resized) @QtCore.pyqtSlot(name="dialog_resized") def dialog_resized(self): if self.isPopupShowing(): self.config().writeEntry("size", QVariant(self.widget.size())) @QtCore.pyqtSlot(bool, name="netstate_changed") def netstate_changed(self, connected): print "GoogleTranslatorApplet: Network status changed! %i" % connected if connected: debug("GoogleTranslatorApplet: Connected!") self._widget.setEnabled(True) self.setBusy(False) else: self.notifier.notify("waiting-for-network", i18n("Waiting for network connection.")) self._widget.setEnabled(False) debug("GoogleTranslatorApplet: Not connected!") self.setBusy(True)
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)