Exemplo n.º 1
0
class YandexTranslateTest(unittest.TestCase):

    def setUp(self):
        self.translate = YandexTranslate(YANDEX_API_KEY)
    
    def test_langs(self):
        languages = self.translate.langs
        self.assertEqual(languages, set(
          [
            u"el", u"en", u"ca", u"it",
            u"hy", u"cs", u"et", u"az",
            u"es", u"ru", u"nl", u"pt",
            u"no", u"tr", u"lv", u"lt",
            u"ro", u"pl", u"be", u"fr",
            u"bg", u"hr", u"de", u"da",
            u"fi", u"hu", u"sr", u"sq",
            u"sv", u"mk", u"sk", u"uk",
            u"sl"
          ]
    ))

    def test_lang_detection(self):
        language = self.translate.detect("Je m'appele Malika")
        self.assertEqual(language, 'fr')

    def test_translation(self):
        result = self.translate.translate(u"Hello", 'en-es')
        self.assertEqual(result["text"][0], u"Hola")
        self.assertEqual(result["code"], 200)

    def test_opposite_translation(self):
        result = self.translate.translate(u"Hola", 'es-en')
        self.assertEqual(result["text"][0], u"Hello")
        self.assertEqual(result["code"], 200)
Exemplo n.º 2
0
def translate(text,source,dest):
    # print("\n\n {0} {1} {2} \n\n".format(text,source,dest))
    translate = YandexTranslate('trnsl.1.1.20190515T114153Z.33301c8ad79f95bd.69a1158dfac270a48213caaf9b6102a3115b8fe1')
    if source =='Eng':
        lang_source='en'
    if source =='Ru':
        lang_source='ru'
    if dest =='Eng':
        lang_dest='en'
    if dest =='Ru':
        lang_dest='ru'
    if source == 'Fra':
        lang_source='fr'
    if dest =='Fra':
        lang_dest='fr'

    try:
        check = translate.detect(text)
    except:
        print("Check Exception on text: {}".format(text))
        return text

    # print("\n\n check: {0} \n\n".format(check))
    if not check.find(lang_dest):
        empty = lang_dest + '-' + lang_source
        #print("\n\n empty: {0} \n\n".format(empty))
        rez=translate.translate(text, empty)
        s = rez['text'][0].decode('utf8')
        # print(u"\n\n rez: {0} \n\n".format(s))
        return s
    return text
Exemplo n.º 3
0
 def test_blocked_key(self):
     translate = YandexTranslate(
         "trnsl.1.1.20130723T112255Z.cfcd2b1ebae9f"
         "ff1.86f3d1de3621e69b8c432fcdd6803bb87ef0e963")
     with self.assertRaises(YandexTranslateException,
                            msg="ERR_KEY_BLOCKED"):
         languages = translate.detect("Hello!")
Exemplo n.º 4
0
    async def ytranslate(self, ctx, language: str, *, text: str):
        """Translate text via yandex

        Language may be just "ru" (target language to translate)
        or "en-ru" (original text's language - target language)"""
        text = chat.escape(text, formatting=True)
        apikeys = await self.bot.db.api_tokens.get_raw("yandex", default={"translate": None})
        try:
            translate = YandexTranslate(apikeys["translate"])
            response = translate.translate(text, language)
        except YandexTranslateException as e:
            if str(e) == "ERR_LANG_NOT_SUPPORTED":
                await ctx.send(chat.error("An error has been occurred: Language {} is not supported"
                                          .format(chat.inline(language))))
            elif str(e) == "ERR_TEXT_TOO_LONG":
                # Discord will return BAD REQUEST (400) sooner than this happen, but whatever...
                await ctx.send(chat.error("An error has been occurred: Text that you provided is too big to "
                                          "translate"))
            elif str(e) == "ERR_KEY_INVALID":
                await ctx.send(chat.error("This command requires Yandex.Translate API key\n"
                                          "You can set it via {}ytapikey".format(ctx.prefix)))
            elif str(e) == "ERR_KEY_BLOCKED":
                await ctx.send(chat.error("API key is blocked. You need to get new api key or unlock current."))
            elif str(e) == "ERR_DAILY_REQ_LIMIT_EXCEEDED":
                await ctx.send(chat.error("Daily requests limit reached. Try again later."))
            elif str(e) == "ERR_DAILY_CHAR_LIMIT_EXCEEDED":
                await ctx.send(chat.error("Daily char limit is exceeded. Try again later."))
            elif str(e) == "ERR_UNPROCESSABLE_TEXT":
                await ctx.send(chat.error("An error has been occurred: Text is unprocessable by translation server"))
            elif str(e) == "ERR_SERVICE_NOT_AVAIBLE":
                await ctx.send(chat.error("An error has been occurred: Service Unavailable. Try again later"))
            else:
                await ctx.send(chat.error("An error has been occurred: {}".format(e)))
            return
        input_lang = None
        output_lang = None
        if len(language) == 2:
            try:
                input_lang = translate.detect(text=text)
            except YandexTranslateException as e:
                if str(e) == "ERR_LANG_NOT_SUPPORTED":
                    await ctx.send(chat.error("This language is not supported"))
                else:
                    await ctx.send(chat.error("Unable to detect language: {}".format(e)))
                return
            output_lang = language
        elif len(language) == 5:
            input_lang = language[:2]
            output_lang = language[3:]
        if response["code"] == 200:
            await ctx.send("**[{}] Input:** {}".format(input_lang.upper(), chat.box(text)))
            await ctx.send("**[{}] Translation:** {}".format(output_lang.upper(), chat.box(response["text"][0])))
        else:
            # According to yandex.translate source code this cannot happen too, but whatever...
            await ctx.send("An error has been occurred. Translation server returned code {}"
                           .format(chat.inline(response["code"])))
Exemplo n.º 5
0
def translate(text):
    '''Translates the message from one language to another'''
    ## Mr. Scott's secret key for Yandex API
    translate = YandexTranslate('') #Yandex key
    finalText, languageTo = getLanguage(text)
    ## detect the language of the text
    language = translate.detect(finalText)
    print('Translating from',language,'to',languageTo)
    ## translate to english
    translated = 'Translate:', translate.translate(finalText, language+'-'+languageTo)
    return (translated[1]['text'][0])
Exemplo n.º 6
0
def translate_language(sourcetext, translationfrom, translationTo):
    translate = YandexTranslate(
        'trnsl.1.1.20171108T161528Z.11fd5cf559085f7e.68f45fa90a6197a56fc8353410bec46f7386e419'
    )
    print('Languages:', translate.langs)
    print('Translate directions:', translate.directions)
    print('Detect language:', translate.detect(sourcetext))
    translated_text = translate.translate(
        sourcetext, translationfrom + '-' + translationTo)
    arr = translated_text['text']
    return arr
    print(arr)
Exemplo n.º 7
0
class ya_translate(object):

    def __init__(self):
        self.translate = YandexTranslate('trnsl.1.1.20170417T120755Z.ace07b3e54a7011b.23ef0f7b86f04dff506c4b86bdb4eb547901af1b')

    def get_english_words(self,eng_word):
        tmp_translate_word=self.translate.translate(eng_word,"ru")
        translate_word=tmp_translate_word['text'][0]
        if self.translate.detect(translate_word) in 'ru':
            return translate_word
        else:
            return
Exemplo n.º 8
0
class Yanslate:
    def __init__( self, key ):

            self.translate = YandexTranslate( key )
        

    def detect( self, text ):

        print('Detect language:', self.translate.detect( text ))
       
        
    def list( self ):
        print('Languages:', self.translate.langs)


    def trans( self,text, lang ):

    	return ('Translate:', self.translate.translate(text, lang))
Exemplo n.º 9
0
async def t(arg):
    'translate'
    translator = YandexTranslate(config.yandex_translate_key)
    #    lang = translator.detect(arg)

    if arg.startswith('-'):
        lang, text = arg.split(None, 1)
        lang = lang[1:]
    else:
        text = arg
        lang = translator.detect(text)

    if lang != 'en':
        translation = translator.translate(text, '{}-en'.format(lang))['text'][0]
        answer = 'translated from {}\n{}'.format(lang, translation)
        return answer
    else:
        return "That's already English"
Exemplo n.º 10
0
class Translator(object):
    """
		Wrapper around the Yandex translate API package.

		Provides methods for determining whether a text is english, and translating it english if not.

	"""

    translator = None

    def __init__(self, api_key):
        self.translator = YandexTranslate(api_key)

    def translator(self):
        """
			Returns:
				YandexTranslate: the initialised YandexTranslate instance
		"""
        return self.translator

    def is_english(self, input_text):
        """
			Use the translator to determine whether the input text is English.
			If it is then returns True, otherwise it returns the detected language 
			type.

			Returns:
				True if english, otherwise language string if not english.
		"""
        language = self.translator.detect(input_text)
        if language == "en":
            return True
        return language

    def translate(self, input_text):
        """
			Translates the input text into English.

			Returns:
				str: The translated text
		"""
        return self.translator.translate(input_text, "en")
Exemplo n.º 11
0
def yandex(text):
    from yandex_translate import YandexTranslate
    translate = YandexTranslate('<YANDEX-API-KEY>')
    lang = translate.detect(text)

    outtext.delete(1.0, END)

    # весь текст перевести в нижний регистр и разделить по пробелам в список
    # и удалить символы ! и ?
    words = text.lower().translate({ord('!'): '', ord('?'): ''}).split()

    # убрать повторяющиеся слова (преобразовав в кортеж) и отсортировать по алфавиту
    words = sorted(set(words))

    for word in words:

        if lang == "en":
            h = translate.translate(word, 'en-ru')
        elif lang == "ru":
            h = translate.translate(word, 'ru-en')

        outtext.insert(END, word + " - " + str(h.get("text")) + "\n")
Exemplo n.º 12
0
def translate_word(bot, update, args):
    print('Пришло слово для перевода: "{}"'.format(args))

    try:
        # Подключаемся к Яндекс переводчику
        translate = YandexTranslate(
            'trnsl.1.1.20161206T204246Z.d8c8ef6d545c7ee5.3b96b277a0d9bb5f0a0060714a5b75d6fa6604b3')

        # Получаем слово для перевода, телеграм нам дает список, собираем его в строку
        word = ' '.join(args).lower()
        # Определяем язык слова
        word_lang = translate.detect(word)

        # Если язык непонятный, пишем в чат что его не поддерживаем
        if word_lang not in ['ru', 'en']:
            bot.sendMessage(update.message.chat_id, 'Я знаю только русский и английский')
            return
        # Выбираем язык в какой требуется перевод
        elif word_lang == 'ru':
            lang = 'en'
        else:
            lang = 'ru'

        # Переводим и на выходе получаем словарь
        word_translated_dic = translate.translate(word, lang)
        # Проверяем что полученный словарь содержит перевод и это строка
        if word_translated_dic['text'][0] and isinstance(word_translated_dic['text'][0], str):
            # Пишем в чат перевод из словара выбираем список по ключу text
            bot.sendMessage(update.message.chat_id, 'Перевод: {}'.format(word_translated_dic['text'][0]))
        # Иначе поднимаем ошибку с кодом 422
        else:
            raise YandexTranslateException(422)

    # Если возникли проблемы с переводом, пишем про это
    except YandexTranslateException:
        bot.sendMessage(update.message.chat_id, 'Не смог перевести слово. Или слово такое, что Яндекс его перевести '
                        'не может, или просто недоступно API для перевода')
Exemplo n.º 13
0
async def tts(arg):
    'text to speech'
    # print 'arg: <'+arg+'>'
    # arg = arg.decode('utf-8').encode('utf-8')
    # print 'arg: ', arg
    if arg.strip() == 'languages':
        return '\n'.join('{}: {}'.format(*i) for i in sorted(gtts.lang.tts_langs().items()))

    if arg.startswith('-'):
        lang, text = arg.split(None, 1)
        lang = lang[1:]
    else:
        text = arg
        translator = YandexTranslate(config.yandex_translate_key)
        lang = translator.detect(text)

    if not lang in gtts.lang.tts_langs():
        lang = 'fi'

    tmp_ogg_file_path = 'tmp/tts.ogg'
    tts = gtts.gTTS(text=text, lang=lang, slow=False)
    tts.save(tmp_ogg_file_path)

    return gtts.lang.tts_langs().get(lang) + '\n' + text, tmp_ogg_file_path
Exemplo n.º 14
0
class NLPEngine:
    def __init__(self):
        self.session_client = dialogflow.SessionsClient()
        self.session_context = dialogflow.ContextsClient()
        self.neutral_words = [
            "bot",
            "chatbot",
            "iris",
            "chat bot",
            "hotel",
            "no",
        ]
        self.spanish_words = [
            "oy",
            "oye",
            "ey",
            "si"
        ]
        self.translate = YandexTranslate('trnsl.1.1.20200215T104617Z.e985952a7'
                                         'c20d3fc.45cea67a739d4bbe0d98177bb452'
                                         '7b84b0857455')

    def detect_language(self, text, last_lang='es', current_locale='es'):
        locale = self.get_current_locale(current_locale)
        if locale is None and last_lang is not None:
            locale = last_lang
        elif locale is None and last_lang is None:
            locale = "es"
        text = text.lower().strip()

        if len(text.split()) == 1:
            if text in self.neutral_words and last_lang is not None:
                return last_lang
            elif text in self.neutral_words and last_lang is None:
                return locale
            elif text in self.spanish_words:
                return "es"
            else:
                try:
                    language = self.translate.detect(text)
                    if language in ['en', 'es']:
                        return language
                    elif last_lang is not None:
                        return last_lang
                    else:
                        return 'es'
                except Exception:
                    return last_lang
        try:
            language = self.translate.detect(text)
            if language in ['en', 'es']:
                return language
            elif last_lang is not None:
                return last_lang
            else:
                return 'es'
        except Exception:
            return last_lang

    @staticmethod
    def get_current_locale(locale):
        if locale.startswith("en"):
            return "en"
        elif locale.startswith("es"):
            return "es"
        else:
            return None

    def predict(self, user_id, message, last_lang='es', current_locale='es'):
        language = self.detect_language(message, last_lang, current_locale)
        intent = self.detect_intent_texts(
            user_id=user_id,
            text=message,
            language_code= language
        )
        return intent

    def detect_intent_texts(self, user_id, text, language_code):
        """Returns the result of detect intent with texts as inputs.
    
        Using the same `user_id` between requests allows continuation
        of the conversation."""
        LOGGER.info(user_id)
        LOGGER.info(language_code)
        LOGGER.info(text)
        session = self.session_client.session_path(DIALOGFLOW_PROJECT_ID, user_id)
        context = self.session_context.session_path(DIALOGFLOW_PROJECT_ID, user_id)
        text_input = dialogflow.types.TextInput(
            text=text, language_code=language_code)
    
        query_input = dialogflow.types.QueryInput(text=text_input)
    
        response = self.session_client.detect_intent(
            session=session, query_input=query_input)
        intent = response.query_result.intent.display_name
        if intent.endswith("- no - no") or intent.endswith(".question - yes")\
                or intent.endswith(".question - no") or intent.endswith(
                ".hotel.cancel - yes") or\
                intent.endswith(".hotel.cancel - no"):
            LOGGER.info("Context Clear")
            self.session_context.delete_all_contexts(context)
    
        return intent, language_code
Exemplo n.º 15
0
 def test_invalid_key(self):
     with self.assertRaises(YandexTranslateException, msg="ERR_KEY_INVALID"):
         translate = YandexTranslate("my-invalid-key")
         language = translate.detect("Hello!")
Exemplo n.º 16
0
from yandex_translate import YandexTranslate
import language_codes

if __name__ == "__main__":
    translate = YandexTranslate(
        'trnsl.1.1.20160421T051442Z.3f43d13c5d30c594.bf3c396e87421e06cac7eab7b3771b3bf20af85c'
    )
    print ":speak: What do you want to translate?"
    print ":listen:"
    message = raw_input()
    print "Translating {}".format(message)
    language = translate.detect(message)
    # translate from detected language to english
    print ":speak:You are speaking {}".format(
        language_codes.languages[language])
    translation = translate.translate(message, '{}-en'.format(language))
    print translation
    print ":speak:The english translation is {}".format(translation["text"][0])
Exemplo n.º 17
0
class Yandex(BaseService):

    def __init__(self, api_key):
        self._api_key = api_key
        self._service = YandexTranslate(self._api_key)
        self._directions = self._directions()

    def translate_cascade(self, initial_language,
                          cascade_steps, text):
        """ 1. Check for the text if the service thinks it is the same language as the user has provided
            2. Check if the services thinks steps are legit and there is no step that cannot be done
            3. Translate cascadingly
        :param initial_language: two letter string of the language user needs to start with
        :param cascade_steps: user provided steps (usually excluding the initial language)
        :param text: the text user wants to translate cascadingly
        :return: a tuple of all translations and the final translation in the original language
        """
        logging.debug(initial_language + " - " + text)

        cascade_steps = self.steps_to_execute(initial_language, cascade_steps)
        if not self.check_language(initial_language, text):
            raise YandexTranslateException(501)

        if not self.check_cascade_steps(initial_language, cascade_steps):
            raise YandexTranslateException(501)
        results = {}
        for lang in cascade_steps[1:]:
            try:
                results[lang] = self._service.translate(text, lang).get('text', '')[0]
            except YandexTranslateException:
                return {}
            text = results[lang]
            logging.debug(lang + " - " + text)
        result = results[initial_language]
        return (results, result)

    def get_language(self, text=""):
        """get the language detected by the service
           :param text: the text user wants to translate cascadingly
           :return: language detected
        """
        return self._service.detect(text)

    def check_language(self, initial_language, text):
        """check whether the user provided text is in the same langauge as the
         initial langauge provided by the user
        :param initial_language: two letter string of the language user needs to start with
        :param text: the text user wants to translate cascadingly
        :return: boolean whether a language is correct
        """
        lang = self.get_language(text)
        if lang == initial_language:
            is_correct_language = True
        else:
            is_correct_language = False
        return is_correct_language

    def _directions(self):
        """Service's available translation directions
        :return: list of the available translation directions (from-to)
        """
        return self._service.directions

    def is_translation_step_valid(self, from_lang, to_lang):
        """
        If one translation step valid
        :param from_lang: two letter string for lang
        :param to_lang: two letter string for lang
        :return: boolean if translation valid from_lang to to_lang
        """
        lang_pair = from_lang + "-" + to_lang
        logging.debug(lang_pair)
        if lang_pair.strip() in self._directions:
            valid = True
        else:
            valid = False
        return valid

    def check_cascade_steps(self, initial_language, cascade_steps):
        """check if steps provided by the user are allowed by the service
        :param initial_language: two letter string of the language user needs to start with
        :param cascade_steps: user provided steps (usually excluding the initial language)
        :return: boolean of whether all the translation steps are doable
        """
        cascade_steps = self.steps_to_execute(initial_language,
                                              cascade_steps)
        is_cascade_achievable = False
        # checking a language with itself is not allowed
        # the first item is the initial_language
        for lang in cascade_steps[1:]:
            if self.is_translation_step_valid(initial_language, lang):
                is_cascade_achievable = True
                initial_language = lang
            else:
                is_cascade_achievable = False
                break
        return is_cascade_achievable
Exemplo n.º 18
0
                                          vk.messages.send( #Тоже самое, но для бесед
                                                      random_id = get_random_id(),
                                                      chat_id=event.chat_id,
                                                      message='Введите фразу, которую надо перевести '
    	                                      )
                                          flag = 0 #Шаманский танец для выхода из 2-х циклов
                                          for event in longpoll.listen():
                                            if event.type == VkBotEventType.MESSAGE_NEW  and event.from_chat:
                                                if text.lower() == 'выход':
                                                    break
                                                else:
                                                     message = event.obj['message']
                                                     text = message['text']
                                                     trNormal = 1 #Колхозный флаг для ошибки
                                                     try: #Исключение, о них поговорим ниже
                                                         trFrom = translate.detect(text.lower()) #Определяем язык

                                                         trResult = translate.translate(text.lower(), trFrom + '-' + trTo) #Переводим
                                                     except Exception as e: #Если что-то пошло не так
                                                         trNormal = 0 #Пинаем флаг ошибки
                                                         print("Exception:", e) #Пишем в консоль
                                                         pass
                                                     if trNormal == 1: 

                                                             vk.messages.send( #Тоже самое, но для бесед
                                                                  random_id = get_random_id(),
                                                                 chat_id=event.chat_id,
                                                                 message='Вот перевод текста \n' + str(trResult['text'])
    		                                         )
                                                             flag = 1
                                                             break
Exemplo n.º 19
0
import csv
from yandex_translate import YandexTranslate  #make sure this is imported
# this is your api key
translate = YandexTranslate('<YandexAPIKey>')
print('Languages:', translate.langs)

# read your csv, single col to be translated
with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    your_list = list(reader)

print('Detect language:', translate.detect(your_list))
# detects the current language
trans = translate.detect(your_list)
print(trans)
print(len(your_list))
print(your_list[1])

# loop over your list, translate from detected language to english.
counter = 0
while counter < len(your_list):
    lang = translate.detect(your_list[counter]),
    print('Translate:', translate.translate(your_list[counter],
                                            '%s-en' % lang))
    counter = counter + 1
Exemplo n.º 20
0
class TranslateService(BaseService):
    #
    # Public methods
    def __init__(self):
        self.engine = YandexTranslate(config.TRANSLATE_YANDEX_KEY)

        BaseService.__init__(self)

    def translate(self, text, direction):
        """
        Translate text from foreign language on native.

        This method tries to find translated word in own DB and if translate doesn't exists,
        it sends request to external service
        :param text: sentence to translate.
        :param direction: Direction for translate. For example en-ru.

        :return: translated text
        """

        translation = self.get(text, direction)
        if translation:
            return translation

        response = self.engine.translate(text, direction)
        if response and response[u'code'] == 200:
            return self.add(text, response)

        raise TranslateError(response)

    def get(self, text, direction):
        """
        Try to find translation in DB
        :param text: sentence to translate.
        :param direction: Direction for translate. For example en-ru.

        :return: translated text
        """

        return self.db.Translation.find_one({'text': text, 'direction': direction})

    def add(self, text, translation):
        """
        Add to collection new translation
        :param text: sentence to translate.
        :param translation: response from translate.yandex.ru

        :return: translated text
        """
        ss = ServiceLocator.resolve(ServiceLocator.SESSIONS)

        translation_entity = self.db.Translation()

        translation_entity.direction = translation[u'lang']
        translation_entity.text = text
        translation_entity.variations = translation[u'text']
        translation_entity.author = ss.get().login

        try:
            translation_entity.validate()
            translation_entity.save()

            return translation_entity
        except Exception as ex:
            error(u'Translate.add', ex)
            return None

    def detect(self, text):
        """
        Detect language
        :param text: text for which needs to detect language
        :return: language
        """

        return self.engine.detect(text)
Exemplo n.º 21
0
class YandexTranslateTest(unittest.TestCase):
    def setUp(self):
        self.translate = YandexTranslate(
            "trnsl.1.1.20130421T140201Z.323e508a" "33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e"
        )

    def test_directions(self):
        directions = self.translate.directions
        self.assertGreater(len(directions), 1)

    def test_langs(self):
        languages = self.translate.langs
        self.assertEqual(
            languages,
            set(
                [
                    u"el",
                    u"en",
                    u"ca",
                    u"it",
                    u"hy",
                    u"cs",
                    u"et",
                    u"az",
                    u"es",
                    u"ru",
                    u"nl",
                    u"pt",
                    u"no",
                    u"tr",
                    u"lv",
                    u"lt",
                    u"ro",
                    u"pl",
                    u"be",
                    u"fr",
                    u"bg",
                    u"hr",
                    u"de",
                    u"da",
                    u"fi",
                    u"hu",
                    u"sr",
                    u"sq",
                    u"sv",
                    u"mk",
                    u"sk",
                    u"uk",
                    u"sl",
                ]
            ),
        )

    def test_blocked_key(self):
        translate = YandexTranslate(
            "trnsl.1.1.20130723T112255Z.cfcd2b1ebae9f" "ff1.86f3d1de3621e69b8c432fcdd6803bb87ef0e963"
        )
        with self.assertRaises(YandexTranslateException, msg="ERR_KEY_BLOCKED"):
            languages = translate.detect("Hello!")

    def test_detect_language(self):
        language = self.translate.detect(text="Hello, world!")
        self.assertEqual(language, "en")

    def test_translate(self):
        result = self.translate.translate(u"Hello!", "ru")
        self.assertEqual(result["text"][0], u"Здравствуйте!")
        self.assertEqual(result["code"], 200)

    def test_translate_in_another_direction(self):
        result = self.translate.translate(u"Здравствуйте", "en")
        self.assertEqual(result["text"][0], u"Hello")
        self.assertEqual(result["code"], 200)

    def test_language_detection_error(self):
        with self.assertRaises(YandexTranslateException, msg="ERR_LANG_NOT_SUPPORTED"):
            self.translate.detect("なのです")

    # Yandex.Translate tries to translate this as english-to-russian
    # def test_translate_error(self):
    #   with self.assertRaises(YandexTranslateException,
    #              msg="ERR_LANG_NOT_SUPPORTED"):
    #     self.translate.translate("なのです", "ru")

    def test_without_key(self):
        with self.assertRaises(
            YandexTranslateException,
            msg="Please, provide key for " "Yandex.Translate API: " "https://translate.yandex.ru/apikeys",
        ):
            translate = YandexTranslate()

    def test_error_long_text(self):
        with self.assertRaises(YandexTranslateException, msg="ERR_TEXT_TOO_LONG"):
            self.translate.translate("hi! " * 4098, "ru")

    def test_invalid_key(self):
        with self.assertRaises(YandexTranslateException, msg="ERR_KEY_INVALID"):
            translate = YandexTranslate("my-invalid-key")
            language = translate.detect("Hello!")
Exemplo n.º 22
0
from yandex_translate import YandexTranslate
import language_codes

if __name__ == "__main__":
  translate = YandexTranslate('trnsl.1.1.20160421T051442Z.3f43d13c5d30c594.bf3c396e87421e06cac7eab7b3771b3bf20af85c')
  print ":speak: What do you want to translate?"
  print ":listen:"
  message = raw_input()
  print "Translating {}".format(message)
  language = translate.detect(message)
  # translate from detected language to english
  print ":speak:You are speaking {}".format(language_codes.languages[language])
  translation = translate.translate(message, '{}-en'.format(language))
  print translation
  print ":speak:The english translation is {}".format(translation["text"][0])
Exemplo n.º 23
0
 def test_invalid_key(self):
     with self.assertRaises(YandexTranslateException,
                            msg="ERR_KEY_INVALID"):
         translate = YandexTranslate("my-invalid-key")
         language = translate.detect("Hello!")
Exemplo n.º 24
0
def analyze_tweet(request):
    try:
        #Instanciando o tradutor e a classe
        Tweet.remover()
        #translator = Translator()	USAR COM GOOGLETRANS
        results = Tweet()
        results.remover()  #Limpando novamente caso haja sujeira

        #Coleta das informacoes fornecidas pelo HTML
        texto = tira_emoji(request.POST.get('tweet_text'))
        lang = request.POST.get('iso_language_code')

        #Pega os links no texto, caso houver
        links = get_links(texto)

        #Remove os links no texto, caso houver
        texto = del_links(texto)

        #Esse aqui eh o yandex, funciona mas fico duvidoso por precisar dar str() --> USAR DE BACKUP
        translate = YandexTranslate(
            'trnsl.1.1.20180809T195441Z.ee8cb1461b193a37.718666e4f14fa0a45cf6e490af90863be18ab677'
        )

        lang = translate.detect(texto)
        textoen = translate.translate(texto, 'en')
        textobr = translate.translate(texto, 'pt')

        textotraden = str(textoen.get('text'))
        textotradbr = str(textobr.get('text'))
        extras = str(textoen.get('code'))
        '''
		#Para a traducao EN
		translationsen = translator.translate(['Erro na traducao EN', texto], dest='en')
		for translationen in translationsen:
			textotraden = translationen.text
			lang = translationen.src
		#Para a traducao BR
		translationsbr = translator.translate(['Error at translating BR', texto], dest='pt')
		for translationbr in translationsbr:
			textotradbr = translationbr.text
			extras = translationbr.extra_data
		'''

        #Polarizacao do texto traduzido
        pol = unique_sentiment_score(textotraden)

        #Da o setAll na classe usando a informacao do HTML
        results.setAll(texto, textotraden, textotradbr, pol,
                       request.POST.get('profile_img_url'),
                       request.POST.get('name'), request.POST.get('url'),
                       request.POST.get('screen_name'), lang,
                       request.POST.get('followers_count'),
                       request.POST.get('friends_count'),
                       request.POST.get('favourites_count'),
                       request.POST.get('verified'), extras, links)

        #Contexto de retorno para o HTML
        context = {'results': results, 'text': request.POST.get('tweet_text')}

    except KeyError:
        return render(request, 'tcc/error.html')
    else:
        return render(request, 'tcc/tweet_analysis.html', context)
Exemplo n.º 25
0
def get_person():
    """
    Obtaining the average user according to the qualities of the different users in dataframe and making dictionaries of the 
    """
    df = pd.read_csv('users_connections.csv')
    d = df.drop_duplicates('Friend ID').reset_index(drop=True)                              # getting dataframe without repitition of the users in the friend column
    # You can get your own yandex key by passing through https://passport.yandex.com/auth/list?origin=translate&retpath=https%3A%2F%2Ftranslate.yandex.com%2Fdevelopers%2Fkeys&mode=edit
    translate = YandexTranslate('trnsl.1.1.20200101T142004Z.5ca473d122ac01d7.151c6a9995eb8e52c98d75ad3f266addd27137d9')         # initializing translater
    print("Starting analysis...")

    print("Analysis of the user's sex")
    sexes = {}
    for i in range(d.shape[0]):                                                             # getting the dictionary of the users' sexes and ther quantity
        if d['Friend\'s sex'][i] != 0:                                                      # checking if the sex is indicated
            if d['Friend\'s sex'][i] not in sexes:                                          # checking if founded sex is in the dictionary or not
                sexes[d['Friend\'s sex'][i]] = 1                                            # adding to dictionary if not
            else:
                sexes[d['Friend\'s sex'][i]] += 1                                           # incrementing quantity if it is in dictionary
    rand_sex = random.randrange(0, len(max_items(sexes)))                                   # randomizing the gender number as the quantity of the users' sexes can be equal
    if max_items(sexes)[rand_sex] == 1:                                                     # getting one of sexes with the highest quantity and translating it as string
        sex = "Female"
    else:
        sex = "Male"

    print("Analysis of the user's full name")
    first_names, last_names = {}, {}                                    
    for i in range(d.shape[0]):                                                             # getting the dictionary of the users' first and last names and ther quantity with respect to selected sex
        if d['Friend\'s sex'][i] == max_items(sexes)[rand_sex]:                             # checking if the name and surname are appropriate according to the sex (ex. if sex is male => name should be for men)
            name = unidecode.unidecode(d['Friend\'s first name'][i])                        # rewriting the name into english alphabet
            if name not in first_names:                                                     # adding usage quantity if the name is not in the dictionary or incrementing the quantity if it there
                first_names[name] = 1
            else:
                first_names[name] += 1

            surname = unidecode.unidecode(d['Friend\'s last name'][i])                      # the same procedure as for the name
            if surname not in last_names:
                last_names[surname] = 1
            else:
                last_names[surname] += 1 

    rand_name = random.randrange(0, len(max_items(first_names)))                            # randomizing the name number as the quantity of the users' names can be equal                                  
    name = max_items(first_names)[rand_name]                                                # getting one of names with the highest quantity and translating it as string
    rand_surname = random.randrange(0, len(max_items(last_names)))                          # randomizing the surname number as the quantity of the users' surnames can be equal
    surname = max_items(last_names)[rand_surname]                                           # getting one of surnames with the highest quantity and translating it as string

    print("Analysis of the user's birthday")
    birth_day, birth_month, birth_year = {}, {}, {}
    for i in range(d.shape[0]):                                                             # getting the dictionary of the users' day, month and year of birth and their quantities
        if d['Friend\'s birthday'][i] != '0':                                               # checking if the birthday is indicated
            birthday = d['Friend\'s birthday'][i].split('.')                                # spliting indicated birthday date by day, month and year
            for j in range(len(birthday)):
                if j == 0:                                                                  # if the day is indicated
                    if birthday[j] not in birth_day:                                        # checks whether it is in dictionary or not, increments if it is there  
                        birth_day[birthday[j]] = 1
                    else:
                        birth_day[birthday[j]] += 1 
                elif j == 1:                                                                # same as for day
                    if birthday[j] not in birth_month:
                        birth_month[birthday[j]] = 1
                    else:
                        birth_month[birthday[j]] += 1 
                elif j == 2:                                                                # same as for day
                    if birthday[j] not in birth_year:
                        birth_year[birthday[j]] = 1
                    else:
                        birth_year[birthday[j]] += 1 
    rand_day = random.randrange(0, len(max_items(birth_day)))                               # randomizing the number of the day of birth as the quantity of the users' day of birth can be equal                   
    rand_month = random.randrange(0, len(max_items(birth_month)))                           # randomizing the number of the month of birth as the quantity of the users' month of birth can be equal
    rand_year = random.randrange(0, len(max_items(birth_year)))                             # randomizing the number of the year of birth as the quantity of the users' year of birth can be equal

    birthday = max_items(birth_day)[rand_day]+'.'+max_items(birth_month)[rand_month]+'.'+max_items(birth_year)[rand_year]   # getting final date of birth

    print("Analysis of the user's living country and city")                              
    countries= {}
    for i in range(d.shape[0]):                                                             # getting the dictionary of the countries where users live and their quantities
        if d['Country'][i] != '0':                                                          # checking if the country is indicated
            res = ast.literal_eval(d['Country'][i])                                         # checking the string value and rewriting it to the python expression, if it is one of them, in our case it is dictionary
            if 'title' in res:                                                              # checking if the title of the country is indicated
                if res['title'] not in countries:                                           # checking if the title is in the dictionary or not and increments if it is there
                    countries[res['title']] = 1                                             
                else:
                    countries[res['title']] += 1

    rand_country = random.randrange(0, len(max_items(countries)))                           
    country = max_items(countries)[rand_country]                                            # getting random selected country from the list of the countries with the maximum users in it

    cities = {}
    for i in range(d.shape[0]):                                                             # getting the dictionary of the cities where users live with respect of the selected country and their quantities
        if d['Country'][i] != '0':                                                          # checking firstly if the country is indicated
            res = ast.literal_eval(d['Country'][i])                                         
            if 'title' in res:
                if res['title'] == max_items(countries)[rand_country]:                      # checking if the city belongs to the selected country in order to avoid  misleadings
                    if d['City'][i] != '0':                                                 # cheking if the city is indicated
                        res = ast.literal_eval(d['City'][i])                                # rewriting string into dictionary
                        if 'title' in res:                                                  # checking if the title of the city is indicated
                            if res['title'] not in cities:                                  # manipulating with dictionary
                                cities[res['title']] = 1
                            else:
                                cities[res['title']] += 1  

    rand_city = random.randrange(0, len(max_items(cities)))
    city = max_items(cities)[rand_city]                                                     # getting random selected city from the list of the cities with the maximum users in it

    print("Analysis of the user's personal info and translating in english")
    langs, political, religions, inspired_by, people_main, life_main, smoking, alcohol = {}, {}, {}, {}, {}, {}, {}, {}
    # Getting dictionaries of the diffent personal information and their number in users profile 
    for i in range(d.shape[0]):                                                           
        p = d['Friend\'s personal info'][i]
        if p != '0':                                                                        # checking if the personal info is indicated
            res = ast.literal_eval(p)                                                       # rewriting it into dictionary
            if 'langs' in res:                                                              # checking if the languages are indicated
                for j in range(len(res['langs'])):                                          # going throungh all the mentioned languages and manipulating with dictionary
                    if res['langs'][j] not in langs:
                        langs[res['langs'][j]] = 1
                    else:
                        langs[res['langs'][j]] += 1
            if 'political' in res:                                                          # checking if the political interests are indicated
                if res['political'] not in political:                                       # manipulating with dictionary
                    political[res['political']] = 1
                else:
                    political[res['political']] += 1
            if 'religion' in res and res['religion'] != '':                                 # checking if the religion is mentioned and it is not empty string
                try:                                                                        # trying to translate mentioned religion name while finding the original language
                    text = translate.translate(res['religion'], translate.detect(res['religion'])+'-en')['text'][0].title()
                except:                                                                     # if the original language of the writings is not found or any other exception, using default translation
                    text = translate.translate(res['religion'], 'ru-en')['text'][0].title() # default translation could be changed
                if text not in religions:                                                   # manipulating with the dictionary 
                    religions[text] = 1
                else:
                    religions[text] += 1
            if 'inspired_by' in res:                                                        # checking if the inspirations are indicated
                text = remove_emoji(res['inspired_by'])                                     # removing emojis and unneccessary symbols
                words = re.sub('[!@#$.«»()%^&*_=+"<>:;]', '', text).split(", ")             # spliting text by ', ' to get different inspirations
                for w in words:                                                             # checking and manipulating with each phrase 
                    try:                                                                    # trying to translate and capitalising each word
                        w = translate.translate(w,translate.detect(w)+'-en')['text'][0].title()
                    except:
                        w = translate.translate(w,'ru-en')['text'][0].title()
                    if " And " in w:                                                        # if there is And in the phrase then we are spliting again               
                        ph = w.split(" And ")
                        for j in ph:
                            if j not in inspired_by:
                                inspired_by[j] = 1
                            else:
                                inspired_by[j] += 1
                    else:
                        if w not in inspired_by:
                            inspired_by[w] = 1
                        else:
                            inspired_by[w] += 1
            if 'people_main' in res:                                                        # almost the same procedure as for the previous attributes 
                if res['people_main'] != 0:
                    if res['people_main'] not in people_main:
                        people_main[res['people_main']] = 1
                    else:
                        people_main[res['people_main']] += 1
            if 'life_main' in res:
                if res['life_main'] != 0:
                    if res['life_main'] not in life_main:
                        life_main[res['life_main']] = 1
                    else:
                        life_main[res['life_main']] += 1
            if 'smoking' in res:
                if res['smoking'] != 0:
                    if res['smoking'] not in smoking:
                        smoking[res['smoking']] = 1
                    else:
                        smoking[res['smoking']] += 1
            if 'alcohol' in res:
                if res['alcohol'] != 0:
                    if res['alcohol'] not in alcohol:
                        alcohol[res['alcohol']] = 1
                    else:
                        alcohol[res['alcohol']] += 1
    

    languages = max_four_lang(langs)    # getting the first four languages with the highest usage
    
    # Getting personal information with the highest number of appearance in the user's profile
    polit_poss = ['Communistic','Socialistic','Moderate','Liberal','Conservative','Monarchical','Ultraconservative','Indifferent','Libertarian']
    rand_polit = random.randrange(0, len(max_items(political)))
    political_interest = polit_poss[int(max_items(political)[rand_polit])-1]

    rand_rel = random.randrange(0, len(max_items(religions)))
    religion = max_items(religions)[rand_rel]
    
    rand_insp = random.randrange(0, len(max_items(inspired_by)))
    inspiration = max_items(inspired_by)[rand_insp]

    peop_qual = ['Mind and creativity','Kindness and honesty','Health and beauty','Power and wealth','Courage and perseverance','Humor and love of life']    
    rand_peop = random.randrange(0, len(max_items(people_main)))
    main_in_people = peop_qual[int(max_items(people_main)[rand_peop])-1]
    
    life_prior = ['Family and children','Career and money','Entertainment and relaxation','Science and research','Perfecting the world','Self development','Beauty and art','Fame and influence']
    rand_life = random.randrange(0, len(max_items(life_main)))
    main_in_life = life_prior[int(max_items(life_main)[rand_life])-1]
    
    smoke_rel = ['Sharply negative','Negative','Compromise','Neutral','Positive']
    rand_smoke = random.randrange(0, len(max_items(smoking)))
    relation_to_smoking = smoke_rel[int(max_items(smoking)[rand_smoke])-1]
    
    alc_rel = ['Sharply negative','Negative','Compromise','Neutral','Positive']
    rand_alc = random.randrange(0, len(max_items(alcohol)))
    relation_to_alcohol = alc_rel[int(max_items(alcohol)[rand_alc])-1]

    print("Analysis of the user's higher education")
    # Getting information about university
    university_names = {}
    for i in range(d.shape[0]): 
        if d['Universities'][i] != '0':                                                 # checking if the university is mentioned
            resList = ast.literal_eval(d['Universities'][i])                            # changing it into list firstly
            if res != []:                                                               # if the list of the universities is not empty
                for uni in resList:                                             
                    resDict = ast.literal_eval(str(uni))                                # change each mentioned university to dictionary
                    if 'name' in resDict:                                               # cheking if the name of the university is indicated and manipulating with dictionary
                        if resDict['name'] not in university_names:
                            university_names[resDict['name']] = 1
                        else:
                            university_names[resDict['name']] += 1

    rand_uni_name = random.randrange(0, len(max_items(university_names)))
    university_name = max_items(university_names)[rand_uni_name]

    # Finding faculty name according to the selected university
    faculty_names = {}
    for i in range(d.shape[0]):                                                         # almost the same procedure as for the names of the universities
        if d['Universities'][i] != '0':
            resList = ast.literal_eval(d['Universities'][i])
            if res != []: 
                for uni in resList:
                    resDict = ast.literal_eval(str(uni))
                    if 'name' in resDict:
                        if resDict['name'] == university_name:                          # checking for the faculties only in selected university
                            if 'faculty_name' in resDict and resDict['faculty_name'] != '':
                                fac_name = translate.translate(resDict['faculty_name'], translate.detect(resDict['faculty_name'])+'-en')['text'][0]
                                if fac_name not in faculty_names:
                                    faculty_names[fac_name] = 1
                                else:
                                    faculty_names[fac_name] += 1         

    rand_fac_name = random.randrange(0, len(max_items(faculty_names)))
    faculty_name=translate.translate(max_items(faculty_names)[rand_fac_name],translate.detect(max_items(faculty_names)[rand_fac_name])+'-en')['text'][0].title()

    personality = [name,surname,sex,birthday,country,city,languages,political_interest,religion,inspiration,main_in_people,main_in_life,relation_to_smoking,relation_to_alcohol,university_name,faculty_name]
    return personality, first_names,last_names,sexes,countries,cities,langs,political, religions, inspired_by,people_main, life_main, smoking, alcohol, university_names,faculty_names
Exemplo n.º 26
0
    keywords.append(interests[i].split(','))

print("{:<5} {:<25} {:<10} {:<25}".format('#', 'KEYWORD', 'LANGUAGE',
                                          'TRANSLATION'))

translated = []
step = 1
for i in range(0, len(keywords)):
    for k in range(0, len(keywords[i])):
        keywords[i][k] = re.sub("' | '", "", keywords[i][k])
        keywords[i][k] = keywords[i][k].replace("'", "")
        keywords[i][k] = keywords[i][k].replace('"', "")
        keywords[i][k] = keywords[i][k].replace("[", "")
        keywords[i][k] = keywords[i][k].replace("]", "")
        keywords[i][k] = keywords[i][k].lower().lstrip()
        language = translate.detect(keywords[i][k])
        if language != 'en':
            translation = translate.translate(keywords[i][k], 'en')
            print("{:<5} {:<25} {:<10} {:<25}".format(step, keywords[i][k],
                                                      language,
                                                      translation['text'][0]))
            translated.append(translation['text'][0])
            step += 1
        else:
            translated.append(keywords[i][k])

with open('translated_keywords.csv', 'w', encoding="utf-8", newline='') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_NONE)
    writer.writerow(['KEYWORD'])
    if translated != []:
        for i in translated:
Exemplo n.º 27
0
 def detect_lang(self, text):
     translator = YandexTranslate(self.yandex_api_key)
     return translator.detect(text)
Exemplo n.º 28
0
from yandex_translate import YandexTranslate
translate = YandexTranslate(
    'Y"trnsl.1.1.20130421T140201Z.323e508a""33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e.'
)
print('Languages:', translate.langs)
print('Translate directions:', translate.directions)
print('Detect language:', translate.detect('Привет, мир!'))
print('Translate:', translate.translate(
    'Привет, мир!',
    'ru-en'))  # or just 'en'from yandex_translate import YandexTranslate
translate = YandexTranslate('Your API key here.')
print('Languages:', translate.langs)
print('Translate directions:', translate.directions)
print('Detect language:', translate.detect('Привет, мир!'))
print('Translate:', translate.translate('Привет, мир!',
                                        'ru-en'))  # or just 'en'
Exemplo n.º 29
0
def detect_language_text(text):
    translate = YandexTranslate(settings.YANDEX_TRANSLATE_API_KEY)
    part_text = text[1:50]
    return translate.detect(part_text)
Exemplo n.º 30
0
 def test_blocked_key(self):
     translate = YandexTranslate(
         "trnsl.1.1.20130723T112255Z.cfcd2b1ebae9f" "ff1.86f3d1de3621e69b8c432fcdd6803bb87ef0e963"
     )
     with self.assertRaises(YandexTranslateException, msg="ERR_KEY_BLOCKED"):
         languages = translate.detect("Hello!")
Exemplo n.º 31
0
def collect(request):
    try:
        #Busca das informacoes de search.html
        op = request.POST.get('op')  #Opcao do usuario
        search = request.POST.get('search')  #Texto do usuario
        #translator = Translator()	USAR COM GOOGLETRANS

        #Setup do collect
        Tweet.remover()  #Limpar a classe Tweet
        api = setup()  #Deixar a API pronta

        if (op == "1"):  #Opcao de analise bruta

            #Setar as variaveis
            count = 40  #Quantos tweets vai pegar
            past = 0  #Quantos tweets nao foram polarizados
            posTot = 0  #Total positivo
            neuTot = 0  #Total negativo
            negTot = 0  #Total neutro
            comTot = 0  #Total geral

            #Filtro e busca
            filter = " -filter:retweets"  #Nao podemos aceitar retweets pois eles sujam os dados com repeticao de um mesmo tweet
            newsearch = "".join(
                (search, filter))  #Junta a pesquisa do usuario com o filtro
            tweets = tweepy.Cursor(api.search,
                                   q=newsearch,
                                   count=count,
                                   tweet_mode="extended").items(
                                       count)  #Faz a coleta do tweepy

            count = 0  #Zerar para caso tenha menos de 20 tweets

            #Iterar pelos tweets
            for tweet in tweets:
                count += 1  #Cada tweet adiciona um

                #Setando a classe
                analysis = Tweet()

                #Retirar os emojis do texto
                texto = tira_emoji(tweet.full_text)

                #Esse aqui eh o yandex, funciona mas fico duvidoso por precisar dar str() --> USAR DE BACKUP
                translate = YandexTranslate(
                    'trnsl.1.1.20180809T195441Z.ee8cb1461b193a37.718666e4f14fa0a45cf6e490af90863be18ab677'
                )

                lang = translate.detect(texto)
                textoen = translate.translate(texto, 'en')
                textobr = translate.translate(texto, 'pt')

                textotraden = str(textoen.get('text'))
                textotradbr = str(textobr.get('text'))
                extras = str(textoen.get('code'))
                '''
				#Para a traducao EN
				translationsen = translator.translate(['Erro na traducao EN', texto], dest='en')
				for translationen in translationsen:
					textotraden = translationen.text
					lang = translationen.src
				#Para a traducao BR
				translationsbr = translator.translate(['Error at translating BR', texto], dest='pt')
				for translationbr in translationsbr:
					textotradbr = translationbr.text
					extras = translationbr.extra_data
				'''

                #Polarizacao
                pol = unique_sentiment_score(textotraden)

                #Dar o setALL na classe Tweet
                analysis.setAll(texto,
                                textotraden,
                                textotradbr,
                                pol,
                                tweet.user.profile_image_url_https,
                                tweet.user.name,
                                tweet.user.url,
                                tweet.user.screen_name,
                                lang,
                                tweet.user.followers_count,
                                tweet.user.friends_count,
                                tweet.user.favourites_count,
                                tweet.user.verified,
                                extras,
                                links="")

                #Salvar os dados do setAll para ler no HTML
                analysis.save()

                #Caso a polarizacao falhe e neutralidade de 1
                if pol['neu'] == 1:
                    pol['neu'] = 0  #Impede que a neutralidade suje os dados
                    count -= 1  #Retira do total de tweets
                    past += 1  #Adiciona nos tweets sem polaridade

                #Adicionando os valores para os graficos
                posTot += pol['pos']
                negTot += pol['neg']
                neuTot += pol['neu']
                comTot += pol['compound']

            #Media dos valores totais para os graficos
            posTot = float(posTot / count)
            negTot = float(negTot / count)
            neuTot = float(neuTot / count)
            comTot = float(comTot / count)

            #Contexto de retorno para o HTML
            context = {
                'posTot': posTot,
                'negTot': negTot,
                'neuTot': neuTot,
                'comTot': comTot,
                'count': count,
                'past': past,
                'analysis': analysis.all(),
                'query': search,
            }
        elif (op == "2"):  #Se voce escolheu pesquisa livre

            #Filtro e busca
            filter = " -filter:retweets"  #Nao podemos aceitar retweets pois eles sujam os dados com repeticao de um mesmo tweet
            newsearch = "".join(
                (search, filter))  #Junta a pesquisa do usuario com o filtro
            tweets = tweepy.Cursor(api.search,
                                   q=newsearch,
                                   count=40,
                                   tweet_mode="extended").items(
                                       40)  #Faz a coleta do tweepy

            #se não houve resultado, manda para nothing.html AMANDA PAROU AQUI AAAAAAAA
            #if not tweets:
            #return render(request, 'tcc/nothing.html')

            #Contexto de retorno para o HTML
            context = {
                'tweets': tweets,
                'query': search,
            }

        else:  #Se voce escolheu pesquisa direcionada por usuario

            users = api.search_users(q=search,
                                     count=100)  #Faz a coleta do tweepy

            #se não houve resultado, manda para nothing.html
            if not users:
                return render(request, 'tcc/nothing.html')

            #Contexto de retorno para o HTML
            context = {
                'users': users,
                'query': search,
            }

    except ZeroDivisionError:
        return render(request, 'tcc/nothing.html')
    except tweepy.TweepError:
        return render(request, 'tcc/error.html')
    except KeyError:
        return render(request, 'tcc/error.html')
    else:
        return render(request, 'tcc/result_list.html', context)
Exemplo n.º 32
0
        authors.append((row["AUTHOR"]))
        titles.append(row["TITLE"])
        abstracts.append(row["ABSTRACT"])

texts = []
for i in range(0, len(titles)):
    content = titles[i] + '. ' + abstracts[i]
    texts.append(content)

step = 1
translated = []
with open('2_abs_translated.csv', 'wb') as f:
    writer = csv2.writer(f)
    writer.writerow(["AUTHOR", "YEAR", "LANGUAGE", "ABSTRACT", "TRANSLATED_ABSTRACT"])
    for text in range(0, len(texts)):
        language = translate.detect(texts[text])
        if language != 'en':
            translation = translate.translate(texts[text], 'en')
            print(step, '\n',
                  'ABSTRACT: ', texts[text], '\n',
                  'LANGUAGE: ', language, '\n',
                  'TRANSLATION: ',translation['text'][0])
            writer.writerow([authors[text], years[text], language,
                             texts[text], translation['text'][0]])
        else:
            print(step, '\n',
                  'ABSTRACT: ', texts[text], '\n',
                  'LANGUAGE: ', language)
            writer.writerow([authors[text], years[text], language,
                             texts[text], texts[text]])
        step += 1
Exemplo n.º 33
0
def analyze_user(request):
    try:
        #Setar as variaveis que serap usadas e o count de qntos tweets vai pegar
        count = 40
        past = 0
        posTot = 0
        neuTot = 0
        negTot = 0
        comTot = 0

        #Limpar a classe e instanciar as apis
        Tweet.remover()
        api = setup()
        #translator = Translator()	USAR COM GOOGLETRANS

        #Busca de tweets e instanciamento das ferramentas
        tweets = api.user_timeline(screen_name=request.POST.get('name'),
                                   count=count,
                                   tweet_mode="extended")

        count = 0  #Zerar para caso tenha menos de 20 tweets

        for tweet in tweets:
            count += 1  #Cada tweet adiciona um

            #Setando a classe para um tweet
            analysis = Tweet()

            #Retirar os emojis do texto pois eles dao crash no app
            full_text_emojiless = tira_emoji(tweet.full_text)

            #Esse aqui eh o yandex, funciona mas fico duvidoso por precisar dar str() --> USAR DE BACKUP
            translate = YandexTranslate(
                'trnsl.1.1.20180809T195441Z.ee8cb1461b193a37.718666e4f14fa0a45cf6e490af90863be18ab677'
            )

            lang = translate.detect(full_text_emojiless)
            textoen = translate.translate(full_text_emojiless, 'en')
            textobr = translate.translate(full_text_emojiless, 'pt')

            textotraden = str(textoen.get('text'))
            textotradbr = str(textobr.get('text'))
            extras = str(textoen.get('code'))
            '''
			#Para a traducao EN
			translationsen = translator.translate(['Erro na traducao EN', full_text_emojiless], dest='en')
			for translationen in translationsen:
				textotraden = translationen.text
				lang = translationen.src
			#Para a traducao BR
			translationsbr = translator.translate(['Error at translating BR', full_text_emojiless], dest='pt')
			for translationbr in translationsbr:
				textotradbr = translationbr.text
				extras = translationbr.extra_data
			'''

            #Para a polarizacao
            pol = unique_sentiment_score(textotraden)

            #Dar o setAll nas informacoes criadas nessa funcao
            analysis.setAll(full_text_emojiless,
                            textotraden,
                            textotradbr,
                            pol,
                            tweet.user.profile_image_url_https,
                            tweet.user.name,
                            tweet.user.url,
                            tweet.user.screen_name,
                            lang,
                            tweet.user.followers_count,
                            tweet.user.friends_count,
                            tweet.user.favourites_count,
                            tweet.user.verified,
                            extras,
                            links="")

            #Salvar os dados na classe
            analysis.save()

            #Caso a polarizacao falhe e neutralidade de 1
            if pol['neu'] == 1:
                pol['neu'] = 0  #Impede que a neutralidade suje os dados
                count -= 1  #Retira do total de tweets
                past += 1  #Adiciona nos tweets sem polaridade

            #Adicionando os valores para os graficos
            posTot += pol['pos']
            negTot += pol['neg']
            neuTot += pol['neu']
            comTot += pol['compound']

        #Fazer as medias de cada valor geral para os graficos
        posTot = float(posTot / count)
        negTot = float(negTot / count)
        neuTot = float(neuTot / count)
        comTot = float(comTot / count)

        #Contexto de retorno para o HTML
        context = {
            'posTot': posTot,
            'negTot': negTot,
            'neuTot': neuTot,
            'comTot': comTot,
            'count': count,
            'past': past,
            'analysis': analysis.all(),
            'query': request.POST.get('name'),
        }

    except tweepy.TweepError:
        return render(request, 'tcc/user_protected.html')
    except KeyError:
        return render(request, 'tcc/error.html')
    else:
        return render(request, 'tcc/user_analysis.html', context)
Exemplo n.º 34
0
class YandexTranslateTest(unittest.TestCase):
    def setUp(self):
        self.translate = YandexTranslate(
            "trnsl.1.1.20130421T140201Z.323e508a"
            "33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")

    def test_directions(self):
        directions = self.translate.directions
        self.assertGreater(len(directions), 1)

    def test_langs(self):
        languages = self.translate.langs
        self.assertEqual(
            languages,
            set([
                u"el", u"en", u"ca", u"it", u"hy", u"cs", u"et", u"az", u"es",
                u"ru", u"nl", u"pt", u"no", u"tr", u"lv", u"lt", u"ro", u"pl",
                u"be", u"fr", u"bg", u"hr", u"de", u"da", u"fi", u"hu", u"sr",
                u"sq", u"sv", u"mk", u"sk", u"uk", u"sl"
            ]))

    def test_blocked_key(self):
        translate = YandexTranslate(
            "trnsl.1.1.20130723T112255Z.cfcd2b1ebae9f"
            "ff1.86f3d1de3621e69b8c432fcdd6803bb87ef0e963")
        with self.assertRaises(YandexTranslateException,
                               msg="ERR_KEY_BLOCKED"):
            languages = translate.detect("Hello!")

    def test_detect_language(self):
        language = self.translate.detect(text="Hello, world!")
        self.assertEqual(language, "en")

    def test_translate(self):
        result = self.translate.translate(u"Hello!", "ru")
        self.assertEqual(result["text"][0], u"Здравствуйте!")
        self.assertEqual(result["code"], 200)

    def test_translate_in_another_direction(self):
        result = self.translate.translate(u"Здравствуйте", "en")
        self.assertEqual(result["text"][0], u"Hello")
        self.assertEqual(result["code"], 200)

    def test_language_detection_error(self):
        with self.assertRaises(YandexTranslateException,
                               msg="ERR_LANG_NOT_SUPPORTED"):
            self.translate.detect("なのです")

    # Yandex.Translate tries to translate this as english-to-russian
    # def test_translate_error(self):
    #   with self.assertRaises(YandexTranslateException,
    #              msg="ERR_LANG_NOT_SUPPORTED"):
    #     self.translate.translate("なのです", "ru")

    def test_without_key(self):
        with self.assertRaises(YandexTranslateException,
                               msg="Please, provide key for " \
                                   "Yandex.Translate API: " \
                                   "https://translate.yandex.ru/apikeys"):
            translate = YandexTranslate()

    def test_error_long_text(self):
        with self.assertRaises(YandexTranslateException,
                               msg="ERR_TEXT_TOO_LONG"):
            self.translate.translate("hi! " * 4098, "ru")

    def test_invalid_key(self):
        with self.assertRaises(YandexTranslateException,
                               msg="ERR_KEY_INVALID"):
            translate = YandexTranslate("my-invalid-key")
            language = translate.detect("Hello!")