def onetrans(string,dest = "zh-CHS"): if dest == "zh-CN": dest = "zh-CHS" #data = bing(string,dst=dest).encode('UTF8') #for python2.7 data = bing(string,dst=dest) return(data)
def _translate_non_english(row): lang = row['lang'] comment = row['comment_text'] if lang != 'en': try: english_comment = bing(comment, dst='en') except Exception: english_comment = comment else: english_comment = comment return english_comment
def load_initial(): f = "roles.xlsx" wb = xlrd.open_workbook(f) fout = open("output2.txt", "w") table = wb.sheets()[0] for i in range(table.nrows): row = table.row(i) for col in row: if len(col.value) < 2: continue print col.value col.value = col.value.replace("/", ";") result = bing(col.value, dst="zh-CHS") print result fout.write((col.value + ":" + result + "\n").encode("utf-8")) fout.close()
# Muhan Zhang # Letras a Flashcards # Hecho Con Amor en Miami, FL from quizpy import * from translation import bing source_lines = list( filter(bool, (open( "letras/" + input("Please enter the name of the file, with the suffix: "), "r").readlines()))) scrubbed_lines = [line for line in source_lines if line != "\n"] dest_lines = [] source_lang = input("And the language?: ") name = input("Give this set a name: ") for line in scrubbed_lines: try: dest_lines.append(bing(line, dst='en')) except: print("Failing quietly") # print (line) print(create_set(name, scrubbed_lines, source_lang, dest_lines, "en").text)
from translation import baidu, google, youdao, iciba,bing print(bing('hello world!', dst = 'ru'))
line = line.lower() candidate = [] word = line.split() candidate.append(word) cand_length = len(candidate) # Reading the Spanish file with open("5_es.txt", "r", encoding="utf8") as file1: for line1 in file1: if line1: count = count + 1 print("Line number = " + str(count)) line1 = line1.rstrip() line1 = line1.lower() reference = [] target = 'en' line1 = line1.split() print(line1) for sub1 in line1: # word to word translation from spanish to english try: english = (bing(sub1, dst='en')) except: pass reference.append(english) print(reference) score = [] for (cand, ref) in zip(candidate, reference): score.append(BLEU.compute(cand, ref, [0.25, 0.25, 0.25, 0.25])) bleu = sum(score) / float(len(score)) print(bleu)
def translate(self, text, to='fr'): return bing(text, dst=to)
from translation import bing, baidu, iciba, google print(bing('Hello World', dst = 'de')) print(baid('Hello World', dst = 'de')) print(iciba('Hello World' dst = 'de')) print(google('Hello World', dst = 'de'))
from translation import Translation, baidu, google, youdao, iciba, bing print(baidu('hello world!', dst = 'zh')) print(youdao('hello world!', dst = 'zh-CN')) print(iciba('hello world!', dst = 'zh')) print(google('hello world!', dst = 'zh-CN')) print(bing('hello world!', dst = 'zh-CHS'))
print("Link # %s is %s" % (len(a), each)) a.append(each) a = (list(set(a))) elif requests.get(each).status_code == 200 and len(a) == num_articulos: break print("Links are ready\n") for some in a: try: print("Loading...") request = urllib.request.Request(some, None, headers) response = urllib.request.urlopen(request) data = response.read() resumen = Document(data).summary() texto = str(resumen) traduccion = (bing(texto, dst='en')) analisis = TextBlob(traduccion) factor = ((analisis.polarity * 5) + 5) * (1 - analisis.subjectivity) lista.append(factor) except: pass print( "\nWhat Google News think about %s being 10 the best and 1 the worst is %s" % (z, sum(lista[:]) / len(lista[:]))) google_username = "" google_password = "" path = "" pytrend = TrendReq(google_username, google_password, custom_useragent='') pytrend.build_payload(kw_list=[z])
def tranlate_pls(self, txt): return translation.bing(txt, dst=self.language_to)
''' Author: Pema Gurung Date: 21-8-17 ''' #using baidu, Google, bing translating the language #install translation : pip install translation from translation import baidu, google, bing #<translator_API> ('text',dst='language_code you wanna convert it to') print(baidu('hello,nice to meet you.', dst='fr')) print(bing('hello,nice to meet you.', dst='hi')) print(bing('हैलो!', dst='en'))
for d in day2topic: klist = json.load(open(keyword_path + d + ".kw")) dayinfo = topic_info[d] if d not in subset_topic_info: subset_topic_info[d] = {} for t in day2topic[d]: tinfo = dayinfo[t] subset_topic_info[d][t] = tinfo kw = sorted(klist[t], key=lambda k: klist[t][k], reverse=True) enkw = [] for k in kw: if k not in translated_dict: en = bing(k, src='ja', dst='en') translated_dict[k] = en json.dump(translated_dict, open(translated_dict_path, 'w')) enkw.append(translated_dict[k]) if limit: topic_subset[d + '-Topic' + t].extend([kw[0]]) if len(kw) > 1: topic_subset[d + '-Topic' + t].extend([kw[1]]) else: topic_subset[d + '-Topic' + t].extend(enkw) json.dump(topic_subset, open(output_file, 'w'), indent=True) topicjson_file = "/Users/brenoust/Google Drive/StageRecherche_REN/SeaCove/Web/client/data/subtopics-all.json" json.dump(subset_topic_info, open(topicjson_file, 'w'), indent=True)
def to_english(tokens, src_lang): sentence = " ".join(tokens) english_sentence = bing(sentence, src=src_lang, dst="en") print(english_sentence) return english_sentence.split()
from translation import Translation, baidu, google, youdao, iciba, bing print(baidu('hello world!', dst='zh')) print(youdao('hello world!', dst='zh-CN')) print(iciba('hello world!', dst='zh')) print(google('hello world!', dst='zh-CN')) print(bing('hello world!', dst='zh-CHS'))
def get_translation_result(query): dst_lang = 'zh-CHS' if is_english(query) else 'en' rtn = translation.bing(query, dst=dst_lang) return rtn
async def on_message(message): msg = (message.content).lower() user = message.author.name ch = message.channel ############################ #START NEW COMMANDS BELOW HERE #_____________________________________________________________________________________________________________________________________ if msg.startswith('/help'): helplist = "/urb 'word'; Defines a word using Urban Dictionary\n/map'number' 'coordinates or a location'. -Displays a satellite image at the specified \tzoom level at the specified location. ex: /map15 New York City\n/sp 'word(s)'. -splits a word up by each letter\n\ /img 'word(s)'. -Displays a random image based on your specified word(s)\n/define 'word'. -Defines a word using Dictionary.com\n/verse 'quran' or 'bible'. -Displays a random verse from the specified religious text\n\ /apod 'YYMMDD'. -Displays the Astronomy Picture of the Day and the associated text of the specified date." em = discord.Embed(title="A list of the available commands", description=helplist, colour=0x48437) await client.send_message(ch, embed=em) #___WEATHER FORECAST !IN PROGRESS!________________________________________________________________________________________________________________________ if msg.startswith('/fore'): inp = msg[6:] days = [] forecast = [] combine = [] url = 'http://forecast.weather.gov/MapClick.php?CityName=', inp, '#.WYdo0elOmUm' print(url) soup3 = requests.get(''.join(url)) soup = BeautifulSoup(soup3.content, "html.parser") for a in soup.find_all('div', {'class': 'col-sm-2 forecast-label'}): days.append(a.text) for a in soup.find_all('div', {'class': 'col-sm-10 forecast-text'}): forecast.append(a.text) for a in range(0, len(days)): text = ''.join((days[a], ': ', forecast[a], '\n\n')) combine.append(text) await client.delete_message(message) em = discord.Embed(title=' '.join(('Detailed Forecast for', inp)), description=''.join(combine), colour=0x000DA) await client.send_message(ch, embed=em) #____FORTUNE TELLER______________________________________________________________________________________________________________________________________ if msg.startswith('/tell'): fortunes = [] url = 'http://www.fortunecookiemessage.com/archive.php?start=', str( random.randrange(0, 800, 50)) print(url) soup3 = requests.get(''.join(url)) soup = BeautifulSoup(soup3.content, "html.parser") for a in soup.find_all('a', href=True): if a['href'].startswith('/cookie'): fortunes.append(a.text) await client.delete_message(message) em = discord.Embed(title=' '.join(('Fortune for', user)), description=fortunes[random.randint( 0, len(fortunes))], colour=0x000DA) await client.send_message(ch, embed=em) #_____LANGUAGE PROCESSING !IN PROGRESS!_____________________________________________________________________________________________________________________ if msg.startswith('/phil'): url = 'https://history.hanover.edu/texts/voltaire/volindex.html' soup3 = requests.get(url) soup = BeautifulSoup(soup3.content, "html.parser") d = 0 phils = [] sent = [] pos = [] words = [] posWords = [] final = [] sentStr = ['DT', 'NN', 'VBZ', 'JJ', '.'] noPos = ['CD', ',', '.', ';', '``', "''", ':', '(', ')'] for a in soup.find_all('a', href=True): d += 1 if d > 2 and d < 106: phils.append(''.join( ('https://history.hanover.edu', a['href']))) url = phils[random.randint(0, 104)] soup3 = requests.get(url) soup = BeautifulSoup(soup3.content, "html.parser") a = soup.text.split('2001.')[1].split('Hanover')[0].replace( '\n', '\n').replace("\'", '').replace(' "', '"').replace('" ', '"') topic = a.split()[0] tokens = nltk.word_tokenize(a) #print(tokens) tagged = nltk.pos_tag(tokens) #print(tagged) for a in range(len(tagged)): sent.append(tagged[a]) for a in sent: if a[1] not in pos and a[1] not in noPos: pos.append(a[1]) words.append(a[0]) for b in range(len(sentStr)): for a in sent: if a[1] == sentStr[b] and a[0] not in posWords: posWords.append(a[0]) final.append(posWords[random.randint(0, len(posWords) - 1)]) posWords = [] await client.delete_message(message) print(topic) em = discord.Embed(description=' '.join(final), colour=0x000DA) await client.send_message(ch, embed=em) #____WIKI SEARCH___________________________________________________________________________________________________ if msg.startswith('/wiki'): inp = msg[6:] print(inp) link = ''.join(('https://simple.wikipedia.org/wiki/', inp)) try: soup3 = requests.get(link) rlink = soup3.url soup3 = requests.get(rlink) soup = BeautifulSoup(soup3.content, "html.parser") words = [] link = soup.find_all('p') for a in range(len(link)): words.append(link[a].get_text()) s = soup.find('h1', {'id': 'firstHeading'}).text d = '\n'.join(words) url = soup3.url em = discord.Embed(title=s, description=d, colour=0x000DA) fm = discord.Embed.set_footer(em, text=url) ms = await client.send_message(ch, embed=em) await client.delete_message(message) except Exception as o: print(o) #____BING VIDEO SEARCH_______________________________________________________________________________________________________________________ if msg.startswith('/vid'): inp = msg[5:] await client.delete_message(message) tmp = await client.send_message(ch, "Loading...".format(msg)) link = 'http://www.bing.com/videos/search?q=', inp, '&qs=n&form=QBVLPG&sp=-1&pq=animal&sc=8-5&sk=&cvid=16424F2262AA469C81860655F851BDB5' print(link) soup3 = requests.get(''.join(link)) soup = BeautifulSoup(soup3.content, "html.parser") words = [] for a in soup.find_all('a', href=True): if a['href'].startswith('https://www.you'): words.append(a['href']) print('\n'.join(words)) link = words[random.randint(0, len(words) - 1)] print(link) await client.delete_message(tmp) await client.send_message(ch, content=('\n'.join( (user + ": " + inp, link)))) #____BING TRANSLATION____________________________________________________________________________________________________________ if msg.startswith('/tr'): inp = msg[3:].split() text = [] for a in range(1, len(inp)): text.append(inp[a]) print(text) print((inp[0])[0].upper()) await client.delete_message(message) try: translated = bing(' '.join(text), dst=langs[inp[0]]) em = discord.Embed(title=' '.join(text), description=translated, colour=0x69420) await client.send_message(ch, embed=em) except Exception as e: print(e) #Urban Dictionary Search_____________________________________________________________________________________________________________________________________________ if msg.startswith('/urb'): inp = msg[5:] await client.delete_message(message) tmp = await client.send_message( ch, "Loading urban definition of {0}...".format(msg[5:])) try: link = 'http://www.urbandictionary.com/define.php?term=', '+'.join( inp.split()) soup3 = requests.get(''.join(link)) soup = BeautifulSoup(soup3.content, "html.parser") text = soup.find('div', { 'class': 'meaning' }).text.replace(''', ''), '\n\n', soup.find('div', { 'class': 'example' }).text.replace(''', '') em = discord.Embed(title=msg[5:], description=''.join(text), colour=0x48437) await client.delete_message(tmp) await client.send_message(ch, embed=em) except: await client.delete_message(tmp) await client.send_message( ch, '{0} is not a word in the Urban Dictionary.\nTry looking up "Ignoramous"...' .format(inp)) #Google Image Search___________________________________________________________________________________________________________________________________________________ if msg.startswith('/map'): inp = msg[4:] inpNum = inp[0:2] inp = inp[2:] inp = inp.replace(' ', '') link = 'https://maps.googleapis.com/maps/api/staticmap?center=', inp, '&zoom=', inpNum.replace( ' ', '' ), '&size=1000x1000&scale=2&maptype=satellite&key=AIzaSyAYf5mIyC5RJxY-u3xiRPsLfjn6niJ9O4o' linkst = 'https://maps.googleapis.com/maps/api/streetview?size=600x300&location=', inp, '&heading=151.78&pitch=-0.76&key=AIzaSyAYf5mIyC5RJxY-u3xiRPsLfjn6niJ9O4o' link = ''.join(link) linkst = ''.join(linkst) inp = inp.replace('_', ' ') em = discord.Embed(title=' '.join(('Zoom level', inpNum, 'of', inp)), colour=0x48437) emImg = discord.Embed.set_image(em, url=link) images = [] for a in range(0, 360, 36): linkst = 'https://maps.googleapis.com/maps/api/streetview?size=650x650&location=', inp, '&heading=', str( a), '&pitch=0&key=AIzaSyAYf5mIyC5RJxY-u3xiRPsLfjn6niJ9O4o' linkst = ''.join(linkst) print(linkst) with urllib.request.urlopen(linkst) as url: f = io.BytesIO(url.read()) im = Image.open(f) images.append(im) #imageio.mimsave('img.gif', images, duration=10) im.save('mapImg.gif', save_all=True, append_images=images, loop=0, subrectangles=True, duration=500) print("done") em2 = discord.Embed(title='', colour=0x000DA) await client.delete_message(message) await client.send_message(ch, embed=em) await client.send_file(ch, 'mapImg.gif') #S P A C E S_______________________________________________________________________________________________________________________________________________ if msg.startswith('/sp'): await client.delete_message(message) inp = ' '.join(message.content[4:]) await client.send_message(ch, inp) #Image Search_______________________________________________________________________________________________________________________________________ if msg.startswith('/img'): x = False imgs = [] msg1 = msg[5:] dif = msg[6:] await client.delete_message(message) tmp = await client.send_message(ch, "Loading...".format(msg)) if msg.startswith('/imgf'): link = 'https://www.bing.com/images/search?q=', dif, '&qft=+filterui:photo-animatedgif&FORM=RESTAB' soup3 = requests.get(''.join(link)) soup = BeautifulSoup(soup3.content, "html.parser") for a in soup.find_all('a', href=True): if a['href'].startswith("http://") and a['href'].endswith( '.gif'): imgs.append(a['href']) if len(imgs) < 1: await client.send_message(ch, 'No gifs were found...') else: link = 'https://www.bing.com/images/search?q=', msg1, '&FORM=RESTAB' soup3 = requests.get(''.join(link)) soup = BeautifulSoup(soup3.content, "html.parser") for a in soup.find_all('a', href=True): if a['href'].startswith("http://") and a['href'].endswith( '.jpg'): imgs.append(a['href']) if len(imgs) < 1: await client.send_message(ch, 'No images were found...') if msg.startswith('/imgx'): link = imgs[random.randint(0, len(imgs) - 1)] print(link) try: r = requests.get(link) with open('/mainImg.jpg', 'wb') as outfile: outfile.write(r.content) img = Image.open('/mainImg.jpg') w = img.size[0] h = img.size[1] img1 = img.crop((0, 0, w / 2, h / 2)) img2 = img1.transpose(Image.FLIP_LEFT_RIGHT) img3 = img1.transpose(Image.FLIP_LEFT_RIGHT).rotate(180) img4 = img1.rotate(-180) newim = Image.new('RGB', (w, h)) newim.paste(img1, (0, 0)) newim.paste(img2, (int(w / 2), 0)) newim.paste(img3, (0, int(h / 2))) newim.paste(img4, (int(w / 2), int(h / 2))) newim.save('/mainImg.jpg') await client.send_message( ch, ' '.join((user, ' creates: ', dif))) await client.send_file(ch, '/mainImg.jpg') print(newim.size) os.remove('/mainImg.jpg') x = True await client.delete_message(tmp) except Exception as e: print(e) await client.send_message( ch, "An error occured, please try again...") if not (x): try: link = imgs[random.randint(0, len(imgs) - 1)] print(link) em = discord.Embed(title=msg1, colour=0x48437) emImg = discord.Embed.set_image(em, url=link) await client.delete_message(tmp) await client.send_message(ch, embed=em) except Exception as errorMsg: print(errorMsg) await client.send_message( ch, "An error occured, please try again...") #DEFINITION______________________________________________________________________________________________________________________________________ if msg.startswith("/define"): try: inp = msg[8:] await client.delete_message(message) tmp = await client.send_message(ch, "Defining {0}...".format(inp)) soup3 = requests.get(''.join( ('http://www.dictionary.com/browse/', inp))) soup = BeautifulSoup(soup3.content, "html.parser") b = soup.find('span', {'class': 'dbox-pg'}) b = (' '.join(b.get_text().split())).replace('. ', '.\n') a = soup.find('div', {'class': 'def-content'}) a = (' '.join(a.get_text().split())).replace('. ', '.\n') em = discord.Embed(title='- '.join((inp, b)), description=a, colour=0xABCDE) await client.delete_message(tmp) await client.send_message(ch, embed=em) except: await client.delete_message(tmp) await client.send_message(ch, "No definition of {0}.".format(inp)) #ASTRONOMY PICTURE OF THE DAY______________________________________________________________________________________________________________________ if msg.startswith("/apod"): inp = msg[6:] if len(inp) < 6 or len(inp) > 6: await client.send_message(ch, "Incorrect format; /apod YYMMDD") await client.delete_message(message) else: if inp.isdigit(): try: date = [inp[i:i + 2] for i in range(0, len(inp), 2)] tmp = await client.send_message( ch, "Loading Astronomy Picture of the Day from {0}.". format('/'.join(date))) link = ''.join( ('https://apod.nasa.gov/apod/ap', inp, '.html')) soup3 = requests.get(link) soup = BeautifulSoup(soup3.content, "html.parser") imgLink = ''.join( ('https://apod.nasa.gov/', str((soup.find_all( "a", href=True))[1]).split('="')[1].split('">')[0])) text = " ".join( (re.sub('<[^>]+>', '', str( (soup.find_all('p'))[2]))).split()) text = text.split("Tomorrow's picture:")[0].split( "digg")[0] text = text.replace("Explanation: ", "").replace( ". ", ".\n\n").replace("? ", "?\n\n").replace("! ", "!\n\n") em = discord.Embed(title='/'.join(date), description=text, colour=0x48437) emImg = discord.Embed.set_image(em, url=imgLink) await client.delete_message(tmp) await client.send_message(ch, embed=em) except IndexError: client.send_message(ch, 'Incorrect format; /apod YYMMDD') await client.delete_message(message) elif not inp.isdigit(): await client.send_message(ch, "Incorrect format; /apod YYMMDD") await client.delete_message(message) #RELIGIOUS TEXT_____________________________________________________________________________________________________________________________________ if msg.startswith("/verse"): inp = msg[7:] #BIBLE______________ if inp.lower() == "bible": await client.delete_message(message) req = requests.get('https://dailyverses.net/random-bible-verse') soup = BeautifulSoup(req.content, "html.parser") verse = str(soup.select('.bibleVerse')) verse = verse.split('[<div class="bibleVerse">')[1].split( '<div class')[0] verse = verse.replace("<br/>", " ") verse2 = str(soup.select('.bibleChapter')) verse2 = re.sub('<[^>]+>', '', verse2) verse2 = verse2.split('[')[1].split('|')[0] em = discord.Embed(title=verse, description=verse2, colour=0x48437) await client.send_message(ch, embed=em) #QURAN____________ elif inp.lower() == 'quran': await client.delete_message(message) req = requests.get('http://ayatalquran.com/random') soup = BeautifulSoup(req.content, "html.parser") verse = str(soup.select('#aya_text')) verse = verse.split('[<h2 id="aya_text">')[1].split('</h2>]')[0] book1 = str(soup.select('#sura_id')) book1 = book1.split('[<span id="sura_id">')[1].split('</span>]')[0] book2 = str(soup.select('#verse_id')) book2 = book2.split('[<span id="verse_id">')[1].split( '</span>]')[0] book = "The Holy Quran ", book1, ":", book2 em = discord.Embed(title=verse, description="".join(book), colour=0x48437) await client.send_message(ch, embed=em)
def getMessage(from_number, body, img_url): language_data = {'portuguese': 'pt', 'irish': 'ga', 'chinese': 'zh-CHS', 'danish': 'da', 'czech': 'cs', 'japanese': 'ja', 'spanish': 'es', 'urdu': 'ur', 'polish': 'pl', 'arabic': 'ar', 'swahili': 'sw', 'vietnamese': 'vi', 'german': 'de', 'hindi': 'hi', 'dutch': 'nl', 'korean': 'ko', 'swedish': 'sv', 'bulgarian': 'bg', 'latin': 'la', 'ukrainian': 'uk', 'lithuanian': 'lt', 'french': 'fr', 'russian': 'ru', 'thai': 'th', 'finnish': 'fi', 'filipino': 'tl', 'turkish': 'tr', 'greek': 'el', 'latvian': 'lv', 'english': 'en', 'italian': 'it'} # Initialize Firebase Application and get user data fb = firebase.FirebaseApplication("https://medicai-4e398.firebaseio.com/", None) data = fb.get('/Users', None) message = "" # Check to see if the user has used our app before if from_number in data: # Resets User Data if body == "R": data[from_number]["current"] = "location" result = fb.put('', '/Users', data) message = "Hi there! Welcome to MedicAI. Before we can help you out, we're going to need a couple of things to achieve better results. Please enter your address." return message if body.split(" ")[0].lower() == "language": data[from_number]["language"] = language_data[body.split(" ")[2].lower()] print(data[from_number]["language"]) message = "Changed your language to " + body.split(" ")[2].lower() + "." result = fb.put('', '/Users', data) message = bing(message, dst=data[from_number]["language"]) return message # Takes data as location, goes to age if data[from_number]["current"] == "location": data[from_number]["current"] = "age" data[from_number]["location"] = body message = "The next thing we need to know is how old you are. Please enter your age." # Takes data as age, goes to gender elif data[from_number]["current"] == "age": try: val = int(body) data[from_number]["age"] = body data[from_number]["current"] = "gender" message = "We need one last thing. Please enter your gender (M/F)." except: message = "Please enter an actual number. (Ex: 12, 73)" # Takes data as gender, now user can use application elif data[from_number]["current"] == "gender": message = "Thanks for registering on MedicAI! How can we help you today?" if body == "M" or "m": data[from_number]["current"] = "completed" data[from_number]["gender"] = "male" elif body == "F" or "f": data[from_number]["current"] = "completed" data[from_number]["gender"] = "female" else: message = "Please enter your gender as M (male) or F (female)" # Start getting symptom analysis from text else: if img_url != None: finalDisease = getImage(img_url, from_number) else: finalDisease = getDiseaseFromSymptom(body, from_number) if finalDisease == "": message = "We were unable to find a disease with those conditions. Try being more specific or upload a picture!" else: message = finalDisease # If the user has never used our app, then we walk him through our intialization process else: data[from_number] = {"current": "location", "language": "es"} print(data) message = "Hi there! Welcome to MedicAI. Before we can help you out, we're going to need a couple of things to achieve better results. Please enter your address." # Save the new data back to firebase result = fb.put('', '/Users', data) print(message) message = bing(message, dst=data[from_number]["language"]) return message