def handle_translation(command): for elem in lang: if (command.startswith(elem)): to_translate = command[len(elem):len(command):].strip() origin = "auto" for elem2 in lang: if (to_translate.startswith(elem2)): if (elem2 != "zh-cn:" and elem2 != "zh-tw:"): origin = to_translate[0:2] else: origin = to_translate[0:5] to_translate = to_translate[len(elem2):len(to_translate):] else: if (to_translate.startswith("auto:")): to_translate = to_translate[len("auto:"):len(to_translate):] if (to_translate != "" and elem != "zh-cn:" and elem != "zh-tw:"): response = translate(to_translate.encode('utf-8'), elem[0:2:], origin) elif(to_translate != ""): if (elem == "zh-cn:"): elem = "zh-CN:" else: elem = "zh-TW:" response = translate(to_translate.encode('utf-8'), elem[0:5:], origin) else: response = "Nothing to translate" return response else: return ""
def Translate(self): while True: phrase = self.GetInput() translation = (translate(phrase, 'es', 'auto')) # add the phrase and the translation to the dictionary self.allPhrases[phrase] = translation print(translation)
def translate(self): """Translate the skills which are written in other languages into English. (The result has already stored in the "translation_fr.json") Output: ---------- translation: `dict`. The dictionary. It can translate skill names which are appared in the profile database into English. """ translation = {} catalog = [] for i in tqdm(range(len(self.data_profile))): if 'skills' in (self.data_profile[i].keys()): for skills in self.data_profile[i]['skills']: if self.data_profile[i]['skills'][0][ 'title'] == 'Top Skills': for skill in self.data_profile[i]['skills'][0][ 'skills']: title = skill['title'].lower() if title not in catalog: catalog.append(title) result = translate(title, to_language='en').lower() if result != title: translation[title] = result return translation
def predict(): # initialize the data dictionary that will be returned from the # view data = {"success": False} # ensure an image was properly uploaded to our endpoint if request.method == "POST" and request.files['image']: imagefile = request.files["image"].read() image = Image.open(io.BytesIO(imagefile)) # preprocess the image and prepare it for classification image = prepare_image(image, target=(224, 224)) # classify the input image and then initialize the list # of predictions to return to the client preds = model.predict(image) results = imagenet_utils.decode_predictions(preds) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": translate(label, 'tr'), "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True print(data) # return the data dictionary as a JSON response return jsonify(data)
def get_question_string(message): language, question, author = message.content.split("\x11") translations = [] for lang in ['en', 'es', 'fr', 'de', 'ja', 'ko', 'cs', 'ru', 'sv']: if lang == language: translations.append(question) else: translations.append( mt.translate(question, to_language=lang, from_language=language)) translations.append(author) string = '''-------------------- :flag_gb: {} :flag_es: {} :flag_fr: {} :flag_de: {} :flag_jp: {} :flag_kr: {} :flag_cz: {} :flag_ru: {} :flag_se: {} @here - The question of the day was submitted by - <@{}>. If any translations are wrong, feel free to tell a member of the staff team :smiley: --------------------'''.format(*translations) return string
def get_review_text(page): try: reviews = page.find(attrs={ 'class': element_xpaths.review_whole_class }).stripped_strings except AttributeError: print('Going alternative') reviews = page.find( attrs={'class': element_xpaths.review_whole_alternate_class}) reviews.find(attrs={ 'class': element_xpaths.review_summary_class }).decompose() reviews = reviews.stripped_strings reviews = list(reviews) print(reviews) original = ' '.join(reviews) chunks = make_500_chunks(reviews) translated = [] iteration = 0 for chunk in chunks: iteration += 1 print('translation iter ', iteration) trans_chunk = mtranslate.translate(to_translate=chunk, to_language='en', from_language='zh-CN') translated.append(trans_chunk) translated = ' '.join(translated) return original, translated
async def caption(self, ctx, user=None): """Caption an image""" img = await self.__get_image(ctx, user) if not isinstance(img, str): return img headers = {"Content-Type": "application/json; charset=utf-8"} payload = {"Content": img, "Type": "CaptionRequest"} url = "https://captionbot.azurewebsites.net/api/messages" try: async with aiohttp.ClientSession() as cs: async with cs.post(url, headers=headers, json=payload) as r: data = await r.text() with open(f'db/guilds/{str(ctx.guild.id)}.json', "r") as f: jdata = json.load(f) lang_code = str(jdata["Guild_Info"]["Language"]).lower() result = mtranslate.translate(str(data), lang_code, "en") em = discord.Embed(color=self.bot.embed_color, title=result) em.set_image(url=img) await ctx.send(embed=em) except: await ctx.send(get_text(ctx.guild, "images", "images.data_fail"))
def main(): infile = open('data/reviews_to_translate.txt', 'r') outfile = open('data/result/reviews_translated.txt', 'a+') outfile2 = open('data/result/translation_results.txt', 'a+') count = 0 for line in infile.readlines(): count += 1 line = line.strip('\n') time.sleep(random.random() * 1) star = line[:2] sentence = line[2:] translation = translate(sentence, 'uz') sml = similarity(sentence, translation) print str(count) + ": " + str(sml) print ">>> " + sentence print "<<< " + translation #newline="" if sml > 0.7: newline = star + sentence else: newline = star + translation outfile2.write(">>> " + sentence + "\n") outfile2.write("<<< " + translation + "\n\n") outfile.write(newline + "\n") infile.close() outfile.close() outfile2.close()
async def translate(self, ctx, language, *, text): """translates the string into a given language Parameters • language - the language to translate to • text - the text to be translated """ await ctx.send(translate(text, language))
def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) print(content_type, chat_type, chat_id) if content_type == 'text': if msg['text'].find('@nct') > -1: linknct = msg['text'].split(" ", 1)[1] data = [ ('url', linknct), ] r = requests.post('http://dekcp.design/flat/getnct/index.php', data=data) bot.sendMessage(chat_id=chat_id, text='Status: %s' % r.json()[0]['msg']) bot.sendMessage(chat_id=chat_id, text='Singer: %s' % r.json()[0]['casy']) bot.sendMessage(chat_id=chat_id, text='Song: %s' % r.json()[0]['baihat']) bot.sendMessage(chat_id=chat_id, text='128: %s' % r.json()[0]['128']) bot.sendMessage(chat_id=chat_id, text='320: %s' % r.json()[0]['320']) bot.sendMessage(chat_id=chat_id, text='Lossless: %s' % r.json()[0]['lossless']) else: bot.sendMessage(chat_id=chat_id, text=translate(msg['text'], 'vi'))
def translate(self, sourceLang, targetLang, replaceOriginal=False, pre_translations=[]): """ Translate sentences :param sourceLang: two-letter code for source language :param targetLang: two-letter code for target language :param replaceOriginal: Replace source text with translation if ``True``. Used for early-translation """ if pre_translations: translations = pre_translations else: text = "\n".join(map(Sentence.getText, self._sentences)) # translation, _ = translate(text, sourceLang, targetLang) translation = m.translate(text.encode("utf-8"), targetLang, sourceLang).encode("utf-8") translations = translation.split("\n") if replaceOriginal: map(Sentence.setText, self._sentences, translations) map(Sentence.setTranslation, self._sentences, translations)
async def _(event): if event.fwd_from: return input_str = event.pattern_match.group(1) if event.reply_to_msg_id: previous_message = await event.get_reply_message() text = previous_message.message lan = input_str elif "|" in input_str: lan, text = input_str.split("|") else: await event.edit("Invalid Syntax. Module stopping.") return text = text.strip() lan = lan.strip() try: translated_text = translate(text, lan) output_str = """**SOURCE** {} **TRANSLATED** to {} {}""".format(text, lan, translated_text) await event.edit(output_str) except Exception as exc: await event.edit(str(exc))
def get_paper_info_from_html(PaperURL): """Fill the data about paper""" logger.debug("Load html from '%s'." % _FULLURL.format(_HOST, PaperURL)) soup = utils.get_soup(PaperURL, _PROXY_OBJ.get_cur_proxy()) res = dict() logger.debug("Parse paper string and get information.") details_soup = soup.find('div', class_='public-publication-details-top') res["type"] = details_soup.find( 'strong', class_='publication-meta-type').text.strip().lower().rstrip(':') res["title"] = details_soup.find('h1', class_='publication-title').text.strip() res["abstract"] = details_soup.find( 'div', class_='publication-abstract').find_all('div')[1].text.strip() meta_sec = details_soup.find( 'div', class_='publication-meta-secondary').text.split() if meta_sec[0] == "DOI:": res["doi"] = meta_sec[1] logger.debug("Translate abstract.") try: res["abstract_ru"] = translate(res["abstract"], 'ru') except Exception as error: logger.warn(traceback.format_exc()) logger.debug("Get references count.") rg_paper_id = soup.find('meta', property="rg:id")['content'][3:] res["rg_id"] = rg_paper_id ref_dict = get_referring_papers(rg_paper_id) if ref_dict is not None and len(ref_dict) > 0: #res["references"] = ref_dict res["references_count"] = len(ref_dict) logger.debug("References count: %i." % res["references_count"]) return res
async def _(event): approved_userss = approved_users.find({}) for ch in approved_userss: iid = ch["id"] userss = ch["user"] if event.is_group: if (await is_register_admin(event.input_chat, event.message.sender_id)): pass elif event.chat_id == iid and event.sender_id == userss: pass else: return input_str = event.pattern_match.group(1) if event.reply_to_msg_id: previous_message = await event.get_reply_message() text = previous_message.message lan = input_str try: translated = translate(text, lan, "auto") await event.reply(translated) except Exception as exc: print(exc) await event.reply("**Server Error !**\nTry Again.")
def writefile(tweet): f = open(label + '_tweets.txt', 'a', encoding='utf-8') tweet_id = tweet.id_str if tweet_id not in tweet_list: tweet_list.add(tweet_id) tweet_user_id = tweet.user.id_str tweet_user = tweet.user.screen_name tweet_text = str(tweet.text).encode("utf_8", "ignore").decode("utf_8") tweet_text = tweet_text.replace('\n', ' ') tweet_text_trans = translate(tweet_text, 'en') tweet_time = str(tweet.created_at).encode("utf_8", "ignore").decode("utf_8") tweet_lang = str(tweet.lang).encode("utf_8", "ignore").decode("utf_8") tweet_reply_id = tweet.in_reply_to_status_id_str if tweet.in_reply_to_status_id_str != None else '-1' tweet_reply_user_id = tweet.in_reply_to_user_id_str if tweet.in_reply_to_user_id_str != None else '-1' tweet_reply_username = tweet.in_reply_to_screen_name if tweet.in_reply_to_screen_name != None else '-1' tweet_coord = tweet.coordinates geo = str(tweet_coord).encode("utf_8", "ignore").decode("utf_8") location = "none" locat_sw = '-1' locat_ne = '-1' latitude = '-1' longitude = '-1' if len(geo) > 10: ind = geo.find("[") ind2 = geo.find("]") latlong = geo[ind + 1:ind2].split(",") longitude = latlong[0] latitude = latlong[1] if tweet.place != None: location = str(tweet.place.name).encode("utf_8", "ignore").decode("utf_8") location_trans = translate(location, 'en') locat_sw = str( tweet.place.bounding_box.coordinates[0][0])[1:-1].encode( "utf_8", "ignore").decode("utf_8") locat_ne = str( tweet.place.bounding_box.coordinates[0][2])[1:-1].encode( "utf_8", "ignore").decode("utf_8") #tweet_with_time_and_geo = tweet_user_id + "\t" + tweet_user + "\t" + tweet_id + "\t" + tweet_text + "\t" + tweet_time + "\t" + latitude + "\t" + longitude + "\t" + location + "\t" + locat_sw + "\t" + locat_ne + "\t" + tweet_lang + "\n" tweet_with_time_and_geo = tweet_user_id + "\t" + tweet_user + "\t" + tweet_id + "\t" + tweet_text + "\t" + tweet_text_trans + "\t" + tweet_time + "\t" + latitude + "\t" + longitude + "\t" + location + "\t" + location_trans + "\t" + locat_sw + "\t" + locat_ne + "\t" + tweet_reply_id + "\t" + tweet_reply_user_id + "\t" + tweet_reply_username + "\t" + tweet_lang + "\n" f.write( tweet_with_time_and_geo.encode("utf_8", "ignore").decode("utf_8")) f.close()
def migrateTxtFile(txt_filename, do_translate=True, migrate_replaces=MIGRATE_REPLACES, prev_translate_replaces=PREV_TRANSLATE_REPLACES, post_translate_replaces=POST_TRANSLATE_REPLACES): """ Make python module migration replacements. :param txt_filename: Text file path. :param do_translate: Automatically translate into English? :return: True/False. """ if not os.path.exists(txt_filename): log_func.warning(u'File <%s> not found' % txt_filename) return False # Read lines lines = list() file_obj = None try: file_obj = open(txt_filename, 'rt') lines = file_obj.readlines() file_obj.close() except: log_func.fatal(u'Error read text file <%s> for migration' % txt_filename) if file_obj: file_obj.close() return False # Replaces for i, line in enumerate(lines): new_line = line if do_translate and isNotEnglishText(new_line): if prev_translate_replaces: new_line = _replacesMigrateLine(new_line, prev_translate_replaces) new_line = mtranslate.translate(new_line, DEFAULT_DST_LANG, DEFAULT_SRC_LANG) if post_translate_replaces: new_line = _replacesMigrateLine(new_line, post_translate_replaces) new_line = _replacesMigrateLine(new_line, migrate_replaces) lines[i] = new_line # Write lines file_obj = None try: file_obj = open(txt_filename, 'wt') file_obj.writelines(lines) file_obj.close() return True except: log_func.fatal(u'Error write text file <%s> for migration' % txt_filename) if file_obj: file_obj.close() return False
def translate(self, text, lang=None): lang = lang or self.lang sentence = translate(unicode(text), lang) translated = unicodedata.normalize('NFKD', unicode(sentence)).encode( 'ascii', 'ignore') LOG.info("translated " + text + " to " + translated) return translated
def _translate_text(self, text: str): current_translation = text for current_destination in self.language_list: current_translation = translate(to_translate=current_translation, to_language=current_destination) return current_translation
def handle_random_fact(self, message): lang = self.lang.split("-")[0] fact = py8fact.random_fact(lang) if lang not in py8fact.SUPPORTED_LANGS: fact = translate(fact, self.lang) self.gui.show_text(fact) self.speak(fact, wait=True) self.gui.clear()
def handle_translate(self, message): word = message.data.get("TranslateKeyword") lang = message.data.get("LanguageKeyword") sentence = message.data.get("phrase") translated = translate(sentence, lang) self.say(translated, lang)
def handle_translate_to(self, message): lang = message.data.get("LanguageKeyword") sentence = message.data.get("translate") to = message.data.get("ToKeyword") translated = translate(sentence, lang) self.say(translated, lang)
def Translate(): df = pd.read_csv("datasettotranslate.csv") for i in range(len(df)): df["Text"][i] = mt.translate( df["Text"][i], "en", "tr") # translate(text to string,desired language,base language) print(i) df.to_csv("translated.csv")
def dat(): uniq_filename = str(datetime.datetime.now().date()) + '_' + str( datetime.datetime.now().time()).replace(':', '.') + '.jpg' data = request.form # for k, v in data.items(): # print (k, len(v)) image1_data = re.sub('^data:image/.+;base64,', '', data['image']) image1 = Image.open(BytesIO(base64.b64decode(image1_data))) image1.save(os.path.join(app.config['UPLOAD_FOLDER'], uniq_filename)) # filename='ad.jpg' # data = request.form # for k, v in data.iteritems(): # print k, len(v) # #for i in data: # # print data.keys(i),len(data.values(i)) # #print data.keys # print type(data) # #f.write(data) # image_data = re.sub('^data:image/.+;base64,', '', data['file']).decode('base64') # image = Image.open(cStringIO.StringIO(image_data)) # image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) cmd = './ba.sh ' + uniq_filename #testcv.image_make(filename) os.system(cmd) # time.sleep(1) file = open('cap.txt', 'r') line = file.read() # print line file.close() a = translate(line, "ml") b = a.encode('utf-8').decode('utf-8') # print b r = requests.post("http://210.212.237.167/tts/festival_cs.php", data={ 'op': b, 'Languages': 'malayalam', 'Voice': 'voice1', 'ex': 'execute', 'ip': '', 'rate': 'normal' }) print(r.status_code) m = re.search('(\d_\d+).wav', r.text) url = "http://210.212.237.167/tts/wav_output/fest_out" + m.group( 1) + ".wav" print(url) #return jsonify({"result":url}) return url
def create_json_for_db(): result = [] for info_dict in information_about_apartments: apartment_info = {} for key, value in info_dict.items(): if key == 'cost': apartment_info[key] = value elif key == 'rooms_info': number_of_rooms = re.match(r'[\d]', value) if number_of_rooms: apartment_info['rooms'] = int(number_of_rooms.group()) elif key == 'area_info': areas_info = [float(area) for area in value.split('/')] areas_info_length = len(areas_info) if areas_info_length == 1: apartment_info['area'] = areas_info[0] elif areas_info_length == 2: apartment_info['area'], apartment_info['living_area'] = areas_info elif areas_info_length == 3: apartment_info['area'], apartment_info['living_area'], apartment_info['kitchen_area'] = areas_info elif key == 'floors_info': if value: try: floors_info = [int(floor) for floor in value.split('/')] except Exception: continue floors_info_length = len(floors_info) if floors_info_length == 1: apartment_info['floor'] = floors_info[0] elif floors_info_length == 2: apartment_info['floor'], apartment_info['floors'] = floors_info elif key in ['conditions', 'walls_material']: if value and value not in CASHED: apartment_info[key] = mtranslate.translate(value, 'en').lower() CASHED[value] = apartment_info[key] elif value in CASHED: apartment_info[key] = CASHED[value] elif key == 'address': apartment_info['distance_to_center'] = get_distance(value, 'Майдан Незалежності, Київ') apartment_info['building_type'] = 'New building' apartment_info['city'] = 'Kyiv' result.append(apartment_info) print(apartment_info) load_apartments_info_to_db(data_to_db=result) os.remove('../json_files/kyiv_info.json') os.remove('../json_files/kyiv_apartment_page_links.json')
def get_tweets(username): auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth) tweets = api.user_timeline(screen_name=username, count=5, tweet_mode="extended") tweets_for_csv = [tweet for tweet in tweets] for j in tweets_for_csv: temp_var = 0 trans = translate(j.full_text) if (len(exclude_keywords) > 0): for words in exclude_keywords: if (trans.find(words) != -1): temp_var = 1 if (temp_var == 1): continue #pos,neg,neu,analysis = sentiment_scores(trans) prediction_sentence = ["", trans] prediction = getPrediction(tuple(prediction_sentence)) analysis = prediction[1][2] hasht = j.entities['hashtags'] hashTags = '' for c in hasht: hashTags = hashTags + c['text'] + ',' hashTags = translate(hashTags) ENGAGEMENT_PARAMETER = "FALSE" if (j.favorite_count > 1 and j.retweet_count > 1): ENGAGEMENT_PARAMETER = "TRUE" #final_parameter='None' # credibility,parameter=credibility_score(trans) # if(parameter!=None): # final_parameter=' '.join([w for w in parameter]) sql = "INSERT INTO tweet_entries (TIME_STAMP,ID,USER_NAME,TWEET,FOLLOWER_COUNT,LIKES,RETWEET_COUNT,ANALYSIS,DESCRIPTION,STATUSES_COUNT,FRIENDS_COUNT,FAVOURITES_COUNT,HASHTAGS,LOCATION,PROFILE_PICTURE,ENGAGEMENT_PARAMETER) VALUES (%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" val = (j.created_at, j.id, username, j.full_text, j.user.followers_count, j.favorite_count, j.retweet_count, analysis, j.user.description, j.user.statuses_count, j.user.friends_count, j.user.favourites_count, hashTags, j.user.location, j.user.profile_image_url, ENGAGEMENT_PARAMETER) mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.")
def handle_rail_travel_intent(self, message): source = message.data.get("Source") source = source.split(" ")[0] self.speak(translate("source is ", "hi")) self.speak(source) destination = message.data.get("Destination") destination = destination.split(" ")[0] self.speak(translate("destination is ", "hi")) self.speak(destination) date = message.data.get("Date") if "ko" in date: date = date.replace("ko", "") self.speak(translate("Date is ", "hi")) self.speak(date) url = "https://api.railwayapi.com/v2/between/source/" + source + "/dest/" + destination + "/date/" + date + "/apikey/ppipz4ugxv/" response = requests.get(url).json() for i in response['trains']: self.speak(translate("train number is ", "hi")) self.speak(str(i['number'])) self.speak(translate("train name is ", "hi")) self.speak(str(i['name'])) self.speak(translate("train departure time from source is ", "hi")) self.speak(str(i['src_departure_time'])) self.speak( translate("train arrival time for destination is ", "hi")) self.speak(str(i['dest_arrival_time']))
def telegram_webhook(): update = request.get_json() text = update["message"]["text"] content_type, chat_type, chat_id = telepot.glance(update["message"]) translation = translate(text, 'fa', 'auto') # translate to farsi (persian) language bot.sendMessage(chat_id, translation) return "OK"
def get_lat_long(location): try: translation = translate(location, "am", "auto") location_place = geolocator.geocode(translation, exactly_one=True, timeout=60) return location_place except: time.sleep(2)
def translator(text): try: text = translate(text, 'en', 'es') except NameError: # unicode is a default on python 3 pass text = unicodedata.normalize('NFD', text) text = text.encode('ascii', 'ignore') text = text.decode("utf-8") return str(text)
async def translate(self, ctx, language, *, text): """translates the string into a given language __**Parameters**__ • language - the language to translate to • text - the text to be translated """ await ctx.send(translate(text, language)) await asyncio.sleep(2) await ctx.message.delete()
def gethex(self, inputmessage): if apistatic.SMSTYPE == 'USERINFOUNIOCDE': localmessage = '' if apistatic.LANGUAGE == 'TELUGU': localmessage = translate(inputmessage, 'te') if apistatic.LANGUAGE == 'BENGAL': localmessage = translate(inputmessage, 'bn') if apistatic.LANGUAGE == 'HINDI': localmessage = translate(inputmessage, 'hi') localmessage = localmessage.encode('utf-16-le').encode( 'hex').upper() resulthex = "" for x in xrange(0, len(localmessage), 4): resulthex = resulthex + localmessage[x + 2:x + 4] + localmessage[x:x + 2] return resulthex else: return inputmessage
def get_keywords(): response = requests.get('http://www.generalecommerce.com/clients/broadcastnews_tv/category_list_js.html', timeout=20) assert response.status_code == 200 soup = BeautifulSoup(response.content, 'html.parser') keywords = [l.replace('news', '') for l in set([v.text for v in soup.find_all('td', {'class': 'devtableitem'}) if 'http' not in v.text])] assert len(keywords) > 0 random.shuffle(keywords) for keyword in keywords: japanese_keyword = translate(keyword, 'ja') logging.debug('[Google Translate] {} -> {}'.format(keyword, japanese_keyword)) if re.search('[a-zA-Z]', japanese_keyword): # we don't want that: Fed watch -> Fed時計 continue yield japanese_keyword
def translate_text(text,to): return translate(text,to)
def test_google_translate_api(): to_translate = 'Procter & Gamble' print(translate(to_translate)) print(translate(to_translate, 'ja')) print(translate(to_translate, 'ru'))
def main(): to_translate = 'Bonjour comment allez vous?' print(translate(to_translate)) print(translate(to_translate, 'ar')) print(translate(to_translate, 'ru'))