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 toTranslateBox(self): if self.ui.toTranslateBox.currentIndex() == 0: self.destLanguage = 'en' #default destination language is English self.ui.toTranslateBox.setCurrentText("English") return else: lang = self.ui.toTranslateBox.currentText().lower() # print('to', lang) self.destLanguage = LANGCODES.get(lang, None)
def _changSettings(): lang = event.message.text[4:] dest = LANGCODES.get(lang) user_lang_modified = _changeUserTranslationDest(user_profile, dest=dest) print( "Translated destination language is successfully changed to: %s" % user_lang_modified.get("dest")) return user_lang_modified
def list_questions(language): if language in LANGCODES.values(): translator = Translator() return_data = [ Question(id=int(question[1].get('id')), created=question[1].get('created'), topic=int(question[1].get('topic')), question=translator.translate(question[1].get('question'), dest=language).text, answer=translator.translate(question[1].get('answer'), dest=language).text) for question in data.items() ] return JSONResponse(return_data, status_code=200) return JSONResponse({}, status_code=400)
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())
'Floods': 'other', 'Shootings': 'crimeviolence', 'Derailment': 'other', 'Wildfire': 'other', 'Haze': 'other' } docroot = '/afs/crc.nd.edu/user/n/nsmith9/NLP_final_project/googleTrain' trans = Translator() scorrect = True with open(sys.argv[1]) as json_data: d = json.load(json_data) lkeys = list(d.keys()) tmp = trans.detect(lkeys[0]) print(tmp.lang) if tmp.lang not in LANGCODES.values(): scorrect = False def words(text): return re.findall(r'\w+', text.lower()) WORDS = Counter(words(docroot + '/' + tmp.lang + '.txt')) def P(word, N=sum(WORDS.values())): "Probability of `word`." return WORDS[word] / N
from googletrans import Translator, LANGCODES import random lc = list(LANGCODES.values()) translator = Translator() def add_hooks(client, table): table["mangle"] = mangle table["translate"] = translate async def mangle(args, client, message): try: times = int(args[1]) text = " ".join(args[2:-1]) + "\n" + args[-1] except: times = 10 text = " ".join(args[1:-1] + "\n" + args[-1]) tmp_msg = "Translating..." tmp = await client.send_message(message.channel, tmp_msg) for i in range(times): text = translator.translate(text, dest=random.choice(lc)).text tmp_msg += str(i + 1) + "..." await client.edit_message(tmp, tmp_msg) text = translator.translate(text, dest="en").text await client.edit_message(tmp, text) async def translate(args, client, message):
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 fromTranslateBox(self): if self.ui.fromTranslateBox.currentIndex() == 0: self.srcLanguage = 'auto' #let google(translation service) determine the source language else: lang = self.ui.fromTranslateBox.currentText().lower() self.srcLanguage = LANGCODES.get(lang, None)
async def translate(ctx): message = ctx.message # do not want the bot to reply to itself so if message.author == client.user: return request = '{}'.format(message.content) try: request = request.split("!translate ")[1] except IndexError: print( "E - No message provided to translate, please refer to the help command (-h) for more info: {}" .format(request)) return await ctx.send( "Error - No message provided to translate, please refer to the help command (-h) for more info: {}" .format(request)) print("Request: {}".format(request)) print("=" * 50) print("S - New request received") args = request.split(" ") l_ind = find_in_list(args, "-l") n_ind = find_in_list(args, "-n") t_ind = find_in_list(args, "-t") h_ind = find_in_list(args, "-h") if h_ind != -1: print("S - Help information requested") return await ctx.send(HELP_TEXT) await ctx.send("Translation request received (⌐■_■)") if max(t_ind, l_ind, n_ind) == -1: text = request n = 10 langs = ['de', 'ko', 'la', 'ja', 'eo'] # default else: if t_ind == -1 or len(args[t_ind + 1:]) == 0: print("E - No text provided (-t): {}".format(request)) return await ctx.send( "Error - No text provided (-t): {}".format(request)) text = " ".join(args[t_ind + 1:]) if max(l_ind, n_ind, t_ind) != t_ind: print( "E - Optional arguments (-l,-n) should be positioned before the text argument (-t), please refer to the help command (-h) for more info: {}" .format(request)) return await ctx.send( "Error - Optional arguments (-l,-n) should be positioned before the text argument (-t), please refer to the help command (-h) for more info: {}" .format(request)) if n_ind == -1: n = 10 else: try: n = int(args[n_ind + 1]) except ValueError: print( "E - Invalid no. of translations (-n), please refer to the help command (-h) for more info: {}" .format(request)) return await ctx.send( "Error - Invalid no. of translations (-n), please refer to the help command (-h) for more info: {}" .format(request)) if l_ind == -1: langs = ['de', 'ko', 'la', 'ja', 'eo'] # default else: ind = l_ind while not ind in (n_ind, t_ind, h_ind): ind += 1 langs = args[l_ind + 1:ind] if len(langs) == 0: print( "E - No language codes provided (-l), please refer to the help command (-h) for valid codes : {}" .format(request)) return await ctx.send( "Error - No language codes provided (-l), please refer to the help command (-h) for valid codes : {}" .format(request)) invalid = [ lang for lang in langs if not lang in list(LANGCODES.values()) ] if len(invalid) != 0: print( "E - Invalid language code(s) provided (-l): {}. Please refer to the help command (-h) for valid codes: {}" .format(" ".join(invalid), request)) return await ctx.send( "Error - Invalid language code(s) provided (-l): {}. Please refer to the help command (-h) for valid codes: {}" .format(" ".join(invalid), request)) print("S - Languages: {}".format(langs)) print("S - No. of iterations: {}".format(n)) print("S - Text to be translated: {}".format(text)) await background_translate(ctx, text, langs, n)
def test_language_translation(language, string): if language in LANGCODES.values(): translator = Translator() return translator.translate(string, dest=language).text return None
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")
channel_access_token = conf_json.get("channel_access_token") # 回傳訊息給 Line 時使用 secret_key = conf_json.get("secret_key") # WebhookHandler驗證事件消息是否來自Line使用 linebot_developer_id = conf_json.get("self_user_id") server_url = "https://" + conf_json.get("server_url") rich_menu_id = conf_json.get("rich_menu_id") # load mongodb settings with open("../mongo_client_settings", "r", encoding="utf-8") as infile: mongo_client_settings = json.loads(infile.read()) line_bot_api = LineBotApi(channel_access_token) # 回傳訊息給 Line 時使用 handler = WebhookHandler(secret_key) # WebhookHandler驗證事件消息是否來自Line使用 # Make the arranged lang_list LANGCODES["chinese_tw"] = LANGCODES.pop("chinese (traditional)") LANGCODES["chinese_cn"] = LANGCODES.pop("chinese (simplified)") LANGCODES["kurdish"] = LANGCODES.pop("kurdish (kurmanji)") LANGCODES["myanmar"] = LANGCODES.pop("myanmar (burmese)") LANGCODES.pop("Filipino") LANGCODES.pop("Hebrew") lang_list = sorted([key for key in LANGCODES]) # New a tranlator to deal with translation requests translator = Translator(service_urls=[ 'translate.google.com', 'translate.google.com.tw', 'translate.google.co.kr', 'translate.google.co.jp' ]) app = Flask(__name__, static_url_path="/res", static_folder="../res")
async def languages(self, ctx): text = [(lang, code) for lang, code in LANGCODES.items()] await ctx.send("\n".join([f"{code}: {lang}" for code, lang in text]))
def specify(s): return LANGCODES.get(redirs.get(s, s), redirs.get(s, s))
def languageList(): return LANGCODES.values()
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。
from googletrans import Translator as BaseTranslator from googletrans.client import Translated from utils.formatters import BetterEmbed if TYPE_CHECKING: from asyncio import AbstractEventLoop from aiohttp import ClientSession # outside of the class to use it as a converter def to_language(arg: str) -> Union[str, None]: """Converts a string into a valid google trans language""" if (low := arg.lower()) in LANGUAGES: return low elif res := LANGCODES.get(low): return res raise BadArgument(message=f"Couldn't find the language {arg}") def check_length(arg: str) -> str: """Checks the initial text and returns it""" if len(arg) > 200: raise BadArgument(message="Cannot translate texts longer than 200 characters") return arg class AioTranslator(BaseTranslator): """Translates stuff using google translate's api""" def __init__(self, *args, **kwargs):