async def translate(self, ctx): msg = ctx.message words = msg.content.split(" ") to_language = words[-1].lower() text = str() arr = msg.content.split('"') if len(arr) == 3: text = arr[1] else: text = " ".join(words[2:-1]) try: # LANGCODES has keys as languages, values as codes # LANGUAGES has keys as codes, values as languages code = None if to_language in LANGCODES.keys(): # if language name was passed # print("lang name was passed!!") code = LANGCODES[to_language] elif to_language in LANGCODES.values(): # if code was passed # print("lang code was passed!!") code = to_language else: await ctx.send( "Please enter a valid language!" + "\nUsage: ```pepe translate <text> <language-name>```" + '\nFor a list of supported languages, use "pepe languages"' ) trans = Translator() translated_text = trans.translate(text, dest=code) await ctx.send(translated_text.text) # , tts=True) except: traceback.print_exc()
def __init__(self, setting): langList = list(LANGCODES.keys()) wx.Frame.__init__(self, None, -1, 'CopyTranslator ' + version, size=(465, 345)) self.SetIcon(wx.Icon(logopath, wx.BITMAP_TYPE_ICO)) self.SetWindowStyle(MainFrame.mainStyle) self.setting = setting TextPanel = wx.Panel(self, -1) buttonPanel = wx.Panel(self, -1) # 始终置顶按钮 self.topCheck = wx.CheckBox(buttonPanel, -1, 'Stay on top') self.Bind(wx.EVT_CHECKBOX, self.setting.ReverseStayTop, self.topCheck) # 自动检测语言按钮 self.detectCheck = wx.CheckBox(buttonPanel, -1, 'Auto detect language') self.Bind(wx.EVT_CHECKBOX, self.setting.ReverseDete, self.detectCheck) # 监听剪贴板选框 self.listenCheck = wx.CheckBox(buttonPanel, -1, 'Listen on Clipboard') self.Bind(wx.EVT_CHECKBOX, self.setting.ReverseListen, self.listenCheck) # 自动复制选框 self.copyCheck = wx.CheckBox(buttonPanel, -1, 'Auto copy') self.Bind(wx.EVT_CHECKBOX, self.setting.ReverseCopy, self.copyCheck) # 切换模式 self.switchBtn = wx.Button(buttonPanel, -1, "Switch Mode") self.Bind(wx.EVT_BUTTON, self.setting.SwitchMode, self.switchBtn) self.transBtn = wx.Button(buttonPanel, -1, "Translate") self.Bind(wx.EVT_BUTTON, self.setting.translateCtrl, self.transBtn) self.transBtn.SetDefault() # 原文本 self.srcLabel = wx.StaticText(TextPanel, -1, "Source:") self.srcText = wx.TextCtrl(TextPanel, -1, "", size=(300, 125), style=wx.TE_MULTILINE) # 创建一个文本控件 self.copyBtn = wx.Button(buttonPanel, -1, "Copy result") self.Bind(wx.EVT_BUTTON, self.setting.Copy, self.copyBtn) # 目标文本 self.dstLabel = wx.StaticText(TextPanel, -1, "Result:") self.destText = wx.TextCtrl(TextPanel, -1, "", size=(300, 125), style=wx.TE_MULTILINE) # 创建一个文本控件 self.fromlabel = wx.StaticText(buttonPanel, -1, 'Source language') self.fromchoice = wx.Choice(buttonPanel, -1, choices=langList) self.fromchoice.SetSelection(self.fromchoice.FindString('english')) tolabel = wx.StaticText(buttonPanel, -1, 'Target language :') self.tochoice = wx.Choice(buttonPanel, -1, choices=langList) self.tochoice.SetSelection( self.tochoice.FindString('chinese (simplified)')) panel1sizer = wx.FlexGridSizer(4, 1, 6, 6) panel1sizer.AddMany( [self.srcLabel, self.srcText, self.dstLabel, self.destText]) TextPanel.SetSizer(panel1sizer) panel2sizer = wx.FlexGridSizer(11, 1, 6, 0) panel2sizer.AddMany([ self.topCheck, self.listenCheck, self.detectCheck, self.copyCheck, self.fromlabel, self.fromchoice, tolabel, self.tochoice, self.switchBtn, self.transBtn, self.copyBtn ]) buttonPanel.SetSizer(panel2sizer) sizer = wx.FlexGridSizer(1, 2, 0, 0) sizer.AddMany([TextPanel, buttonPanel]) self.SetSizer(sizer) self.Fit() # 创建定时器 self.timer = wx.Timer(self) # 创建定时器 self.Bind(wx.EVT_TIMER, self.setting.OnTimer, self.timer) # 绑定一个定时器事件 # 绑定事件 self.Bind(wx.EVT_CLOSE, self.OnClose) self.Bind( wx.EVT_ICONIZE, self.OnIconfiy ) # 窗口最小化时,调用OnIconfiy,注意Wx窗体上的最小化按钮,触发的事件是 wx.EVT_ICONIZE,而根本就没有定义什么wx.EVT_MINIMIZE,但是最大化,有个wx.EVT_MAXIMIZE。
def __init__(self): self.translation_client: Translator = Translator() self.translation_cache: Dict[str, Dict[str, str]] = dict() self.hyphenated_linebreak_regex = re.compile(r"\s*-\n\s*") self.linebreak_in_the_midst_of_text_regex = re.compile(r"\n[ ]*(\S+)") self.valid_languages = list(LANGCODES.keys()) + list(LANGUAGES.keys())
def get_fdiedb(): translator = Translator() language = choice(list(LANGCODES.keys())) print(language) result = translator.translate("fick die deutsche Bahn", dest=language) return language + ": " + result.text
def main(): st.sidebar.write('StanzaGraphs ') mes = ''' a Multilingual STANZA-based Summary and Keyword Extractor and Question-Answering System using TextGraphs and Neural Networks ''' st.sidebar.write(mes) langList = ['', 'Auto detect'] langList += list(LANGCODES.keys()) langFull = st.sidebar.selectbox('Translate to?', langList) if langFull != "Auto detect": lang = LANGCODES.get(langFull) else: lang = langFull st.write('Selected language: ', langFull) uploaded_file = st.sidebar.file_uploader('Select a File', type=['txt', 'pdf']) title = None action = None if uploaded_file is not None: fpath = save_uploaded_file(uploaded_file) if fpath[-4:] == '.pdf': pdf2txt(fpath[:-4]) text = file2text(fpath[:-4] + '.txt') data_lang = langid.classify(text)[0] if data_lang == 'zh': data_lang = 'zh-cn' elif data_lang == 'jv': data_lang = 'jw' data_fulllang = LANGUAGES.get(data_lang) st.sidebar.write('Language:', data_fulllang) fname = fpath[:-4] print("fname: ") print(fname) if not title or title != uploaded_file.name: title = uploaded_file.name action = st.sidebar.selectbox("Choose an action", ["Summarize", "Ask a question"]) if action == "Summarize": proceed = st.sidebar.button('Run with selected option!') if proceed: if langFull == "Auto detect": st.write('Translate summary from ', fname, 'to ', data_fulllang) else: st.write('Translate summary from ', fname, 'to ', langFull) summary(fname=fname, lang=lang) pass elif action == "Ask a question": st.write('Query Answering') question = st.text_input('Input your question here:') if question: st.write('Answers:') answer(fname, question, lang) else: st.info("Please select a text file to upload")