Exemple #1
0
def sentence(request):
    de, en = py_.at(request.GET, 'de', 'en')
    data = dict(languages=['de', 'en', 'es', 'fr', 'ko'],
                translations=dict(de=de, en=en),
                query=dict(de=de, en=en))
    translator = Translator()

    if de:
        text = translator.translate(de, src='de', dest='en').text
        data['translations']['en'] = text
    elif en:
        text = translator.translate(en, src='en', dest='de').text
        data['translations']['de'] = text

    if de or en:
        text = data['translations']['en']
        results = run_async(
            lambda l: translator.translate(text, src='en', dest=l),
            data['languages'][2:])

        for i, result in enumerate(results, 2):
            lang = data['languages'][i]
            data['translations'][lang] = result.text

    return render(request, 'sentence.html', data)
Exemple #2
0
def translate(input_phrase):
    translator = Translator()

    # detect language type of input phrase
    detect_result = translator.detect(input_phrase)
    language_type = detect_result.lang

    # define data frame
    trans_phrase_array = [''] * 5
    colmuns_list = ['English', 'Japanese', 'Spanish', 'Chinese', 'Ch_Prns']

    print('Language type: %s' % language_type)

    # switch translation process depend on language type
    if 'ja' in language_type:  # Japanese
        print('Input phrase is Japanese')
        phrase_in_zh = to_chinese_simplified(translator, input_phrase)
        trans_phrase_array[0] = to_english(translator, input_phrase).text
        trans_phrase_array[1] = input_phrase
        trans_phrase_array[2] = to_spanish(translator, input_phrase).text
        trans_phrase_array[3] = phrase_in_zh.text
        trans_phrase_array[4] = phrase_in_zh.pronunciation
    elif 'zh-CN' in language_type:  # Chinese(Simplified)
        print('Input phrase is Simplified Chinese')
        phrase_in_zh = to_chinese_simplified(translator, input_phrase)
        trans_phrase_array[0] = to_english(translator, input_phrase).text
        trans_phrase_array[1] = to_japanese(translator, input_phrase).text
        trans_phrase_array[2] = to_spanish(translator, input_phrase).text
        trans_phrase_array[3] = phrase_in_zh.text
        trans_phrase_array[4] = phrase_in_zh.pronunciation
    elif 'en' in language_type:  # English
        print('Input phrase is English')
        phrase_in_zh = to_chinese_simplified(translator, input_phrase)
        trans_phrase_array[0] = input_phrase
        trans_phrase_array[1] = to_japanese(translator, input_phrase).text
        trans_phrase_array[2] = to_spanish(translator, input_phrase).text
        trans_phrase_array[3] = phrase_in_zh.text
        trans_phrase_array[4] = phrase_in_zh.pronunciation
    elif 'es' in language_type:  # Spanish
        print('Input phrase is Spanish')
        phrase_in_zh = to_chinese_simplified(translator, input_phrase)
        trans_phrase_array[0] = to_english(translator, input_phrase).text
        trans_phrase_array[1] = to_japanese(translator, input_phrase).text
        trans_phrase_array[2] = input_phrase
        trans_phrase_array[3] = phrase_in_zh.text
        trans_phrase_array[4] = phrase_in_zh.pronunciation
    else:
        print('Input phrase should be Japanese, English, Chinese or Spanish')

    # write in csv
    # df_trans_phrase = pd.DataFrame(index=[], columns=colmuns_list)
    df_trans_phrase = pd.read_csv('phrase_translation_database.csv',
                                  encoding='utf-8-sig',
                                  index_col=0)
    sr_trans_phrase = pd.Series(trans_phrase_array,
                                index=colmuns_list,
                                name=input_phrase)
    df_trans_phrase = df_trans_phrase.append(sr_trans_phrase)
    df_trans_phrase.to_csv('phrase_translation_database.csv',
                           encoding='utf-8-sig')
Exemple #3
0
def do_translate(bot: Bot, update: Update, args: List[str]):

    short_name = "Created By @MidukkiBot 😬"

    msg = update.effective_message # type: Optional[Message]

    lan = " ".join(args)

    to_translate_text = msg.reply_to_message.text

    translator = Translator()

    try:

        translated = translator.translate(to_translate_text, dest=lan)

        src_lang = translated.src

        translated_text = translated.text

        msg.reply_text("Translated from {} to {}.\n {}".format(src_lang, lan, translated_text))

    except :

        msg.reply_text("Error")
async def _(event):
    if event.fwd_from:
        return
    input_str = event.pattern_match.group(1)
    start = datetime.now()
    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
    translator = Translator()
    try:
        translated = translator.translate(text, dest=lan)
        src_lang = translated.src
        translated_text = translated.text
        end = datetime.now()
        ms = (end - start).seconds
        output_str = "Translated from {} to {} in {} seconds. \n {}".format(src_lang, lan, str(ms), translated_text)
        await event.edit(output_str)
    except exc:
        await event.edit(str(exc))
Exemple #5
0
def translate(request):
    print("translate!!!")
    # 로그인 했을 경우
    if 'userId' in request.session:

        userId = request.session['userId']
        print(userId)
        user = User.objects.get(username=userId)

        # 로그인 확인 후 원래 작업
        spell = request.GET.get('spell')
        print(spell)

        #google translate 사용
        '''cmd = ['python']
        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).stdout
        output = result.read().strip()
        result.close()
        print(output)'''
        translator = Translator()
        translated = translator.translate(spell, dest='ko')
        print(spell + " : " + translated.text)
        context = {'mean': translated.text}
        return render(request, 'Vocabulary/translate.html', context)

    # 로그인 안했을 경우
    else:
        redirect_to = reverse('Main')
        return HttpResponseRedirect(redirect_to)
    def translate_dest_to_source(self):
        self.ensure_one()

        trl = Translator().translate(text=self.value, src=self.lang, dest='en_US').text

        if self.value == self.value.capitalize():
            trl = trl.capitalize()

        self.source = trl
        self.state = 'translated'
Exemple #7
0
def message():
    data = json.loads(request.data)
    content = data["content"]
    translator = Translator()
    translated = translator.translate(content, dest="en")

    response = {"message": {"text": translated.text}}

    response = json.dumps(response, ensure_ascii=False)
    return response
Exemple #8
0
    def detect_language_google(self, sample):
        '''
		EN: It uses Google Translate to detect the language of a given sample.
		SP: Utiliza el Traductor de Google para detectar el idioma de una muestra dada.
		
		:param sample: sample of text from which the language is detected / muestra de texto a partir de la cual detectar el idioma
		:return: the detected language / el idioma detectado
		'''
        translator = Translator()
        det = translator.detect(sample)
        language_google = self.LANGUAGES_GOOGLE[det.lang]
        return language_google
Exemple #9
0
def google_translate(text, target='en'):
    if not target:
        target = 'en'
    # Instantiates a client
    translator = Translator()
    try:
        new = translator.translate(text, dest=target)
    except AttributeError as e:
        print(e)
        return None
    if new.extra_data['confidence'] and float(
            new.extra_data['confidence']) > float(0.8) and new.text != text:
        return new
    return None
Exemple #10
0
def get_text(image, preprocess):
    ### TESSERACT ###
    try:
        # load the example image and convert it to grayscale
        image = cv2.imread(image)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # check to see if we should apply thresholding to preprocess the
        # image
        if preprocess == "thresh":
            gray = cv2.threshold(gray, 0, 255,
                                 cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        # make a check to see if median blurring should be done to remove
        # noise
        elif preprocess == "blur":
            gray = cv2.medianBlur(gray, 3)

        # write the grayscale image to disk as a temporary file so we can
        # apply OCR to it
        filename = "{}.png".format(os.getpid())
        cv2.imwrite(filename, gray)

        # load the image as a PIL/Pillow image, apply OCR, and then delete
        # the temporary file
        if os.name == 'nt':  #if on windows
            pytesseract.pytesseract.tesseract_cmd = r'.\Tesseract-OCR\tesseract.exe'
        text = pytesseract.image_to_string(Image.open(filename))
        os.remove(filename)
        logging.debug(text)

        s = Translator().translate(text=text, dest='fr').text
        src = Translator().translate(text=text, dest='fr').src
        logging.debug("Traduction du français : " + s)

        if text:
            text = text + "\nTraduction du " + src + " vers le fr : " + s

        # show the output images
        #cv2.imshow("Image", image)
        #cv2.imshow("Output", gray)
        cv2.waitKey(0)

        ### End TESSERACT ###
        return text
    except Exception as e:
        logging.error("ERROR : ", e)
        logging.debug("File corrupted")
        return "File corrupted"
    def __find(self, timeout):
        while not self.finder_pool.empty():
            self.__loading()
            proxy = self.finder_pool.get()

            try:
                translator = Translator(proxies=proxy, timeout=timeout)
                translator.translate('Hello', dest='de')
            except Exception:
                pass
            else:
                if self.finder_pool.qsize():
                    self.done_proxies.put(proxy)
            finally:
                self.finder_pool.task_done()
                self.__clear()
Exemple #12
0
def many_resorts():
    """
    (None) -> dict

    Return a huge list of different cities of Europe.
    Data are taken from site:
    https://andy-travelua.livejournal.com/389517.html
    """
    dictionary = dict()
    cite = requests.get("https://andy-travelua.livejournal.com/389517.html")
    resorts = re.findall('\d?\d\.\s([А-Я]+\s?[А-Я]+)\s', cite.text)
    resorts.remove('ЗАМКИ СЛОВАЧЧИНИ')
    resorts.remove("САКСОНСЬКА")
    resorts.remove("СРЕМСКИ КАРЛОВЦИ")
    for word in range(len(resorts)):
        lst = list(resorts[word])
        for letter in range(1, len(lst)):
            lst[letter] = lst[letter].lower()
        resorts[word] = ''.join(lst)
    resorts.remove("Кшьонж")
    resorts.remove("Куфштайн")
    for item in range(len(resorts)):
        value = Translator().translate(text=resorts[item], dest='en').text
        dictionary[value] = value
    return dictionary
Exemple #13
0
def translate_chinese(subs, dst="en"):
    # try to translate to english
    from py_translator import Translator
    lst = [row[2] for row in subs]
    trans = {}
    for sublist in slice_iterator(lst):
        translator = Translator()
        try:
            translations = translator.translate(sublist, dest=dst)    
            for translation in translations:
                trans[translation.origin] = translation.text
        except Exception as ex:
            print(ex)
    for row in subs:
        if row[2] in trans:
            row[2] = trans[row[2]]
    dump_sbv(dst_file=f"subtitle_{dst}.sbv", subs=subs)
Exemple #14
0
def traductor(word):
    from py_translator import Translator
    try:
        text_new = Translator().translate(src='es', dest='en', text=word).text
        return text_new

    except ConnectionError as e:
        print("Received error:", e.data)
Exemple #15
0
async def translateme(e):
  if not e.text[0].isalpha() and e.text[0]!="!" and e.text[0]!="/" and e.text[0]!="#" and e.text[0]!="@":
    global langi
    translator=Translator()
    textx=await e.get_reply_message()
    message = e.text
    if textx:
         message = textx
         text = str(message.message)
    else:
        text = str(message[4:])
    reply_text=translator.translate(text, dest=langi).text
    reply_text="`Source: `\n"+text+"`\n\nTranslation: `\n"+reply_text
    await bot.send_message(e.chat_id,reply_text)
    await e.delete()
    if LOGGER:
        await bot.send_message(LOGGER_GROUP,"Translate query "+message+" was executed successfully")
def newTranslator(proxies={
    'http': '127.0.0.1:1080',
    'https': '127.0.0.1:1080'
}):
    """

    :param use_proxies: 代理
    :return:
    """
    return Translator(proxies=proxies)
Exemple #17
0
def translate_to_en(text):

    try:
        # Google cloud translation service
        translated_text = Translator().translate(text=text, dest='en').text
        return translated_text

    except:
        print("------------- *** ERROR : Unable to translate text ***")
        return '[TRANSLATION_ERROR]'
def main():
    try:
        filename = sys.argv[1]
    except IndexError:
        print('Use "python3 auto_translate.py %filename%"')
        sys.exit()

    translator = Translator()

    document = Document(filename)
    table = document.tables[0]
    for row in tqdm(table.rows):
        eng_cell = row.cells[0]
        ru_cell = row.cells[1]
        if eng_cell.text:
            if not ru_cell.text:
                text = translator.translate(eng_cell.text, src='en',
                                            dest='ru').text
                ru_cell.text = text

    document.save('auto_translated__' + filename)
    print('Done')
Exemple #19
0
def traduz(palavra,linguagem,ficheiro):
    abrevLinguagem = linguas.get(removeAccents(linguagem).lower())
    if abrevLinguagem is not None:
        dict = verifica_dicionario(palavra,abrevLinguagem,ficheiro)
        cache = verifica_cache(palavra,abrevLinguagem)
        if dict:
            traducao = dict
        elif cache:
            traducao = cache
        else:
            traducao = Translator().translate(palavra,abrevLinguagem).text
            guardar_cache(palavra,abrevLinguagem,traducao) # guarda nova traduçao em cache
        resposta = "A tradução de " + palavra + " é " + traducao + "."
        return resposta,1
    else: # nao encontrou a lingua
        return None,0
Exemple #20
0
def translate_redirector(text=' ', language='en', show_previous=False):
	if len(text) == 0:
		return render_template('translator_wrong.html')

	translation = Translator().translate(text=text, dest=language).text
	messages = []

	if (show_previous):
		add_to_database(text, translation, language, len(text))
		rows = get_rows_from_database()
		for i in range(len(rows) - 1, -1, -1):
			if (rows[i][LANGUAGE] == language):
				messages.append(rows[i])
				if (len(messages) == MAX_LAST_REQUEST_COUNTER):
					break

	return render_template('translator.html', messages=messages, content=translation)
Exemple #21
0
def few_resorts():
    """
    (None) -> dict

    Return the dictionary of famous and beautiful cities of Europe.
    Data are taken from site:
    https://www.obozrevatel.com/ukr/travel/news/top-10-nejmovirnih-mist-evropi-yaki-potribno-vidvidati.htm
    """
    dictionary = dict()
    site = requests.get(
        "https://www.obozrevatel.com/ukr/travel/news/top-10-nejmovirnih-mist-evropi-yaki-potribno-vidvidati.htm"
    )
    resorts = re.findall('<h2>(\w+),\s\w+</h2>', site.text)
    resorts.remove("Оломоуц")
    for item in range(len(resorts)):
        value = Translator().translate(text=resorts[item], dest='en').text
        dictionary[value] = value
    return dictionary
    def AnalisisSentimiento(self):
        for i in self.listaDeFicheros:
            patron = re.compile('(?<=_)[^\]]+(?=.txt)')
            s = patron.findall(str(i))
            f = open(i, 'r', encoding='ISO-8859-2')
            texto = f.read()
            textotraducido = Translator().translate(str(texto), dest='en').text
            analysis = TextBlob(textotraducido)
            self.prueba = analysis.sentiment.polarity
            self.imprimir = (
                f"TEXTO:{texto}\n ANALISIS DE SENTIMIENTO: {self.prueba}")
            self.listaAnalisis.append(self.imprimir)
            f.close()

            val = (str(self.prueba), str(s[0]))
            BBDD.updateAnalisisSentimiento(val)

        self.flag = True
        self.btn_guardar.setEnabled(True)
        self.flagHilo = True
Exemple #23
0
    def command_respond(self, bot, update):
        user_id = update.message.chat_id
        text = update.message.text
        ##get the room that user uses
        self.logger.info(f"= Got on chat #{user_id}: {text!r}")
        curr_room_id = self.storage.users.find_one({"_id": user_id})['room_id']
        for i in self.storage.users.find():
            userId = int(i['_id'])
            if not (userId == user_id):
                if (i['room_id'] == curr_room_id):
                    ## translate to user lang

                    response = Translator().translate(text,
                                                      dest=i['language']).text
                    # response = goslate.Goslate().translate(text, i['language'])
                    # response = Translator().translate('text', src='en', dest=i['language'])
                    ## send to users
                    msg = messageBold(
                        "*" + update.message['from_user']['first_name'] + "*" +
                        " \n" + response, bot)
                    msg.send_to(userId)
Exemple #24
0
    def translate(self):
        proxy = {
            'socks': 'socks://localhost:9050',
            'socks': 'socks://localhost:9050',
        }

        ROTATING_PROXY_LIST = {'socks': 'socks://localhost:9050'}

        text = ""

        if self.type_of_input == "using_file":
            text = str(
                base64.b64decode(self.name_of_file).decode("utf-8")).replace(
                    '\\ n', '\n').replace('\\n', '\n')
        else:
            text = self.text

        s = Translator(proxies=proxy).translate(text=text,
                                                dest=self.language.code).text

        self.term_translated = s
Exemple #25
0
def get_paraphrases(
	sentence,
	rightward_model,
	leftward_model,
	):

	unfolded_rightward_model = Anamorphism(underlying_model=rightward_model,beam_width=1)
	unfolded_leftward_model = Anamorphism(underlying_model=leftward_model,beam_width=2)

	# sentence = byte_pair_encoding(sentence=sentence.lower(),code_path=unfolded_rightward_model.bpe_code_path)

	# _ , target_sents = unfolded_rightward_model.forward(sequence=[],source_sentence=sentence)
	# target_sent = target_sents[0].lower()

	target_sent = Translator().translate(text=sentence,src='en', dest='fr').text

	# target_sent = "Der mann lacht."

	# input_target_sent = byte_pair_encoding(sentence=target_sent,code_path=leftward_code_path)
	print("target sentence:",target_sent)
	# print("INPUT TARGET SENTENCE",input_target_sent)
	probs , paraphrases = unfolded_leftward_model.forward(sequence=[],source_sentence=target_sent)
	unfolded_leftward_model.empty_cache()
	print("ALL PARAPHRASES",display(probs=probs,support=paraphrases))
	# print("paraphrases",paraphrases)
	# raise Exception

	paraphrase1=paraphrases[0]
	paraphrase2=paraphrases[1]

	_ , backtrans1=unfolded_rightward_model.forward(sequence=[],source_sentence=paraphrase1)
	_ , backtrans2=unfolded_rightward_model.forward(sequence=[],source_sentence=paraphrase2)
	backtrans1=backtrans1[0]
	backtrans2=backtrans2[0]

	print("tok",nltk.word_tokenize(byte_pair_decoding(backtrans1)),nltk.word_tokenize(byte_pair_decoding(backtrans2)))
	condition1 = nltk.word_tokenize(byte_pair_decoding(backtrans1))==nltk.word_tokenize(byte_pair_decoding(backtrans2))
	if condition1:

		return [paraphrase1,paraphrase2] 
Exemple #26
0
    def __translate(self, line):
        self.__progress()

        curr_proxy = self.proxies.done_proxies.get()
        tr = Translator(proxies=curr_proxy, timeout=10)

        res = None

        try:
            res = tr.translate(line.msgid, dest=self.dest, src=self.src)
        except Exception:
            curr_proxy = self.proxies.done_proxies.get()
            tr = Translator(proxies=curr_proxy, timeout=10)
            res = tr.translate(line.msgid, dest=self.dest, src=self.src)
        finally:
            self.proxies.done_proxies.put(curr_proxy)
            line.msgstr = res.text if res else ''
 def translate(self, txt):
     if self.fromLanguage == self.toLanguage:
         return txt
     else:
         if not self.WarningDone:
             self.WarningDone = True
             print(
                 "  !! Traduction en cours, soyez patient ... / translation ongoing, please be patient ..."
             )
         #translationChunks = txt.split('.')
         translationChunks = [txt]
         translatedTxt = ''
         for chunk in translationChunks:
             translatedChunk = Translator.translate(
                 self, chunk, dest=self.toLanguage,
                 src=self.fromLanguage).text
             translatedTxt += translatedChunk
             if DEBUG:
                 print("#DEBUG: text to translate : {}".format(
                     chunk.strip()))  # .encode('utf-8')
                 print("#DEBUG: translated text : {}\n".format(
                     translatedTxt))  # .encode('utf-8')
         return translatedTxt
class BCTask:
	account_jp_id = os.environ.get('BCJP_ACCOUNT_ID')
	account_en_id = os.environ.get('BCEN_ACCOUNT_ID')
	web_hook = os.environ.get('BATTLECAT_WEBHOOK')
	names = os.environ.get('BC_BOT_NAMES')
	avatars = os.environ.get('BC_BOT_AVATARS')
	translator = Translator()

	def __init__(self, consumer_key, consumer_secret, access_token, access_secret):
		self.twitter = TwitterApi(consumer_key, consumer_secret, access_token, access_secret)
		self.discord = DiscordApi(self.web_hook, self.names, self.avatars)
		self.handle = {
			self.account_jp_id: self.handle_tweet_jp,
			self.account_en_id: self.handle_tweet_en
		}

	def handle_tweet_jp(self, tweet):
		try:
			text = self.translator.translate(tweet.text, dest='en').text
		except:
			print("Error translating tweet: {}".format(tweet.text))
			text = tweet.text
			
		message = '\n==============================\n【Twitter】【BCJP】- Battle Cat JP Twitter\n==============================\n\n'+text
		self.discord.send_discord_message(message)

	def handle_tweet_en(self, tweet):
		message = '\n==============================\n【Twitter】【BCEN】- Battle Cat EN Twitter\n==============================\n\n'+tweet.text
		self.discord.send_discord_message(message)

	def handle_tweet(self, tweet):
		self.handle[tweet.userId](tweet)

	def execute(self):
		print("Starting up BCTask....")
		account_ids = [self.account_en_id, self.account_jp_id]
		self.twitter.on_status_change(account_ids, self.handle_tweet)
Exemple #29
0
import nltk
from nltk.corpus import gutenberg, brown
import collections
import pandas as pd
import re
from py_translator import Translator
translator = Translator()
#nltk.download() #if u need to download smth for nltk

#Opening + tokenization
file = open('got_s01e01.srt', 'r') 
text = file.read()
file.close()
text_clean = (re.sub('[^a-zA-Z]+', ' ', text.replace('\n', ' '))).lower()

tokens = nltk.word_tokenize(text_clean)
long_tokens = []
for token in tokens:
    if len(token) > 2: #drop words less than 2 letters
        long_tokens.append(token)
ctr = collections.Counter(long_tokens) ##how often word appears in the text
ctr_df = pd.DataFrame.from_dict(dict(ctr), orient='index')
ctr_df.reset_index(inplace=True)
ctr_df.columns = ['word', 'n']

# ctr(frequency) from corpus
news_text = gutenberg.words()
ctr_df['frequency'] = 0
fdist = nltk.FreqDist(w.lower() for w in news_text)
words = list(ctr_df['word'].get_values())
for word in words:
from py_translator import Translator

while True:
    s = Translator().translate(text='Bugün hava gözümü ağrıdır.',
                               dest='en').text
print(s)