示例#1
0
    def translate(self, text: str, destination_language: str, source_language: str = "auto") -> TranslationResult:
        """
        Translates the given text to the given language

        i.e Good morning (en) --> おはようございます (ja)
        """
        dest_lang = Language(destination_language)
        source_lang = Language(source_language)

        def _translate(translator: BaseTranslator, index: int):
            translator = self._instantiate_translator(translator, self.services, index)
            result = translator.translate(
                text=text, destination_language=dest_lang, source_language=source_lang
            )
            if result is None:
                raise NoResult("{service} did not return any value".format(service=translator.__repr__()))
            return result

        def _fast_translate(queue: Queue, translator: BaseTranslator, index: int):
            try:
                queue.put(_translate(translator=translator, index=index))
            except Exception:
                pass

        if self.FAST_MODE:
            _queue = Queue()
            threads = []
            for index, service in enumerate(self.services):
                thread = Thread(target=_fast_translate, args=(_queue, service, index))
                thread.start()
                threads.append(thread)
            result = _queue.get(threads=threads)  # wait for a value and return it
            if result is None:
                raise NoResult("No service has returned a valid result")
            return result

        for index, service in enumerate(self.services):
            try:
                return _translate(translator=service, index=index)
            except Exception:
                continue
        else:
            raise NoResult("No service has returned a valid result")
示例#2
0
    def dictionary(self, text: str, destination_language: str, source_language="auto") -> DictionaryResult:
        """
        Returns a list of translations that are classified between two categories: featured and less common

        i.e Hello --> {'featured': ['ハロー', 'こんにちは'], 'less_common': ['hello', '今日は', 'どうも', 'こんにちわ', 'こにちは', 'ほいほい', 'おーい', 'アンニョンハセヨ', 'アニョハセヨ'}
        """
        dest_lang = Language(destination_language)
        source_lang = Language(source_language)

        def _dictionary(translator: BaseTranslator, index: int):
            translator = self._instantiate_translator(translator, self.services, index)
            result = translator.dictionary(
                text=text, destination_language=dest_lang, source_language=source_lang
            )
            if result is None:
                raise NoResult("{service} did not return any value".format(service=translator.__repr__()))
            return result

        def _fast_dictionary(queue: Queue, translator: BaseTranslator, index: int):
            try:
                queue.put(_dictionary(translator=translator, index=index))
            except Exception:
                pass

        if self.FAST_MODE:
            _queue = Queue()
            threads = []
            for index, service in enumerate(self.services):
                thread = Thread(target=_fast_dictionary, args=(_queue, service, index))
                thread.start()
                threads.append(thread)
            result = _queue.get(threads=threads)  # wait for a value and return it
            if result is None:
                raise NoResult("No service has returned a valid result")
            return result

        for index, service in enumerate(self.services):
            try:
                return _dictionary(translator=service, index=index)
            except Exception:
                continue
        else:
            raise NoResult("No service has returned a valid result")
示例#3
0
    def example(self, text: str, destination_language: str, source_language: str = "auto") -> ExampleResult:
        """
        Returns a set of examples / use cases for the given word

        i.e Hello --> ['Hello friends how are you?', 'Hello im back again.']
        """
        dest_lang = Language(destination_language)
        source_lang = Language(source_language)

        def _example(translator: BaseTranslator, index: int):
            translator = self._instantiate_translator(translator, self.services, index)
            result = translator.example(
                text=text, destination_language=dest_lang, source_language=source_lang
            )
            if result is None:
                raise NoResult("{service} did not return any value".format(service=translator.__repr__()))
            return result

        def _fast_example(queue: Queue, translator: BaseTranslator, index: int):
            try:
                queue.put(_example(translator=translator, index=index))
            except Exception:
                pass

        if self.FAST_MODE:
            _queue = Queue()
            threads = []
            for index, service in enumerate(self.services):
                thread = Thread(target=_fast_example, args=(_queue, service, index))
                thread.start()
                threads.append(thread)
            result = _queue.get(threads=threads)  # wait for a value and return it
            if result is None:
                raise NoResult("No service has returned a valid result")
            return result

        for index, service in enumerate(self.services):
            try:
                return _example(translator=service, index=index)
            except Exception:
                continue
        else:
            raise NoResult("No service has returned a valid result")
示例#4
0
    def spellcheck(self, text: str, source_language: str = "auto") -> SpellcheckResult:
        """
        Checks the spelling of a given text

        i.e God morning --> Good morning
        """
        source_lang = Language(source_language)

        def _spellcheck(translator: BaseTranslator, index: int):
            translator = self._instantiate_translator(translator, self.services, index)
            result = translator.spellcheck(
                text=text, source_language=source_lang
            )
            if result is None:
                raise NoResult("{service} did not return any value".format(service=translator.__repr__()))
            return result

        def _fast_spellcheck(queue: Queue, translator: BaseTranslator, index: int):
            try:
                queue.put(_spellcheck(translator=translator, index=index))
            except Exception:
                pass

        if self.FAST_MODE:
            _queue = Queue()
            threads = []
            for index, service in enumerate(self.services):
                thread = Thread(target=_fast_spellcheck, args=(_queue, service, index))
                thread.start()
                threads.append(thread)
            result = _queue.get(threads=threads)  # wait for a value and return it
            if result is None:
                raise NoResult("No service has returned a valid result")
            return result

        for index, service in enumerate(self.services):
            try:
                return _spellcheck(translator=service, index=index)
            except Exception:
                continue
        else:
            raise NoResult("No service has returned a valid result")
示例#5
0
    def language(self, text: str) -> LanguageResult:
        """
        Returns the language of the given text

        i.e 皆さんおはようございます! --> Japanese
        """
        def _language(translator: BaseTranslator, index: int):
            translator = self._instantiate_translator(translator, self.services, index)
            result = translator.language(
                text=text
            )
            if result is None:
                raise NoResult("{service} did not return any value".format(service=translator.__repr__()))
            return result

        def _fast_language(queue: Queue, translator: BaseTranslator, index: int):
            try:
                queue.put(_language(translator=translator, index=index))
            except Exception:
                pass

        if self.FAST_MODE:
            _queue = Queue()
            threads = []
            for index, service in enumerate(self.services):
                thread = Thread(target=_fast_language, args=(_queue, service, index))
                thread.start()
                threads.append(thread)
            result = _queue.get(threads=threads)  # wait for a value and return it
            if result is None:
                raise NoResult("No service has returned a valid result")
            return result

        for index, service in enumerate(self.services):
            try:
                return _language(translator=service, index=index)
            except Exception:
                continue
        else:
            raise NoResult("No service has returned a valid result")
示例#6
0
 def _fast_text_to_speech(queue: Queue, translator: BaseTranslator, index: int):
     try:
         queue.put(_text_to_speech(translator=translator, index=index))
     except Exception:
         pass
示例#7
0
    def text_to_speech(self, text: str, speed: int = 100, gender: str = "female", source_language: str = "auto") -> TextToSpechResult:
        """
        Gives back the text to speech result for the given text

        Args:
          text: the given text
          source_language: the source language

        Returns:
            the mp3 file as bytes

        Example:
            >>> from translatepy import Translator
            >>> t = Translator()
            >>> result = t.text_to_speech("Hello, how are you?")
            >>> with open("output.mp3", "wb") as output: # open a binary (b) file to write (w)
            ...     output.write(result.result)
                    # or:
                    result.write_to_file(output)
            # Or you can just use write_to_file method:
            >>> result.write_to_file("output.mp3")
            >>> print("Output of Text to Speech is available in output.mp3!")

            # the result is an MP3 file with the text to speech output
        """
        source_lang = Language(source_language)

        def _text_to_speech(translator: BaseTranslator, index: int):
            translator = self._instantiate_translator(translator, self.services, index)
            result = translator.text_to_speech(
                text=text, speed=speed, gender=gender, source_language=source_lang
            )
            if result is None:
                raise NoResult("{service} did not return any value".format(service=translator.__repr__()))
            return result

        def _fast_text_to_speech(queue: Queue, translator: BaseTranslator, index: int):
            try:
                queue.put(_text_to_speech(translator=translator, index=index))
            except Exception:
                pass

        if self.FAST_MODE:
            _queue = Queue()
            threads = []
            for index, service in enumerate(self.services):
                thread = Thread(target=_fast_text_to_speech, args=(_queue, service, index))
                thread.start()
                threads.append(thread)
            result = _queue.get(threads=threads)  # wait for a value and return it
            if result is None:
                raise NoResult("No service has returned a valid result")
            return result

        for index, service in enumerate(self.services):
            try:
                return _text_to_speech(translator=service, index=index)
            except Exception:
                continue
        else:
            raise NoResult("No service has returned a valid result")
示例#8
0
 def _fast_dictionary(queue: Queue, translator: BaseTranslator, index: int):
     try:
         queue.put(_dictionary(translator=translator, index=index))
     except Exception:
         pass
示例#9
0
 def _fast_example(queue: Queue, translator: BaseTranslator, index: int):
     try:
         queue.put(_example(translator=translator, index=index))
     except Exception:
         pass
示例#10
0
 def _fast_spellcheck(queue: Queue, translator: BaseTranslator, index: int):
     try:
         queue.put(_spellcheck(translator=translator, index=index))
     except Exception:
         pass