def testUppercaseMatters(self):
        response = self.translator.translate(
            TranslationQuery(query="March", max_translations=5))
        response2 = self.translator.translate(
            TranslationQuery(query="march", max_translations=5))

        # The first is the month; the second is the verb
        assert response.translations != response2.translations
    def testNumberOfTranslationsWorks(self):

        response = self.translator.translate(
            TranslationQuery(query="genommen", max_translations=5))

        print(response.translations)

        response = self.translator.translate(
            TranslationQuery(query="genommen", max_translations=5))

        print(response.translations)
Example #3
0
    def testUppercase(self):
        response2 = self.translator.translate(TranslationQuery(
            query="Conjunction",
            max_translations=5
        ))
        response = self.translator.translate(TranslationQuery(
            query="conjunction",
            max_translations=5
        ))

        assert response.translations == response2.translations
Example #4
0
    def testNumberOfTranslationsWorks(self):

        response = self.translator.translate(
            TranslationQuery(query="cama", max_translations=5))

        self.assertEqual(response.translations[0]['translation'], 'bed')
        self.assertEqual(len(response.translations), 5)

        response = self.translator.translate(
            TranslationQuery(query="cama", max_translations=3))

        self.assertEqual(response.translations[0]['translation'], 'bed')
        self.assertEqual(len(response.translations), 3)
    def testQuotedWord(self):
        response1 = self.translator.translate(
            TranslationQuery(query="'insincere'", max_translations=5))

        response2 = self.translator.translate(
            TranslationQuery(query="insincere", max_translations=5))

        response3 = self.translator.translate(
            TranslationQuery(query=''
                             "insincere"
                             '', max_translations=5))

        assert (response1.translations == response2.translations ==
                response3.translations)
    def testSuffixStartsWithPunctuation(self):
        response = self.translators['es-en'].translate(
            TranslationQuery(before_context='Estoy en la',
                             query='cama',
                             after_context=', e soy dormiendo'))

        self.assertEqual(response.get_raw_translations()[0], 'bed')

        response = self.translators['es-en'].translate(
            TranslationQuery(before_context='Estoy en la',
                             query='cama',
                             after_context=' , e soy dormiendo'))

        self.assertEqual(response.get_raw_translations()[0], 'bed')
    def test_something(self):
        translator = GoogleTranslatorFactory.build_with_context(
            source_language='nl', target_language='en')
        translator.set_cache(MemoryCache(translator_type='google'))

        response = translator.translate(
            TranslationQuery(before_context='De directeur',
                             query='treedt af',
                             after_context=''))

        # Make the same request
        response2 = translator.translate(
            TranslationQuery(before_context='De directeur',
                             query='treedt af',
                             after_context=''))
Example #8
0
    def testNumberOfTranslationsWorks(self):
        response = self.translator.translate(TranslationQuery(
            query="conjunction",
            max_translations=5
        ))

        assert 2 <= len(response.translations)
    def testSuffixIsEmpty(self):
        response = self.translators['es-en'].translate(
            TranslationQuery(before_context='Estoy en la',
                             query='cama',
                             after_context=''))

        self.assertEqual(response.get_raw_translations()[0], 'bed')
    def testContextMatters(self):
        response = self.translators['en-nl'].translate(
            TranslationQuery(before_context='Dark',
                             query='matter',
                             after_context='is to be found in the universe'))

        self.assertEqual(response.get_raw_translations()[0], 'materie')
Example #11
0
 def test_issue_31__commma_in_response(self):
     res = self.translators['de-en'].translate(TranslationQuery(
         before_context='Nach ersten Abschnitten am Wochenende sei die Wasserstraße nun komplett  ',
         query='dicht',
         after_context=', sagte eine Sprecherin des Wasserstraßen- und Schifffahrtsamtes (WSA)'
     ))
     self.assertFalse("," in res.get_raw_translations()[0])
Example #12
0
    def testUnicodeCharactersInWordToTranslate(self):
        response = self.translators['de-en'].translate(TranslationQuery(
            before_context=u'Die schön',
            query=u'löwen',
            after_context=u' geht zum Wald'))

        self.assertIn(response.get_raw_translations()[0], ['lion', 'beautiful lion'])
    def test_unicode_outputs(self):
        response = self.translators['en-de'].translate(
            TranslationQuery(before_context='The ',
                             query='lion',
                             after_context='goes to the forest'))

        self.assertEqual(response.get_raw_translations()[0], u'Löwe')
Example #14
0
    def test_strange_span_in_return(self):
        response = self.translators['de-en'].translate(TranslationQuery(
            before_context='Ich hatte mich',
            query='eigentlich schon',
            after_context=' mit dem 1:1-Unentschieden abgefunden'))

        self.assertNotIn("</span>", response.get_raw_translations()[0])
    def test_merging_cache_results(self):
        # Build a query
        query = TranslationQuery(before_context='De directeur',
                                 query='treedt af',
                                 after_context='')

        # Some made up results
        translations = [
            make_translation('resigns', 60, 'Google'),
            make_translation('ends', 40, 'Google')
        ]

        lang_config = dict(
            source_language='nl',
            target_language='en',
        )

        # Store results in the cache
        self.cache.store(query=query, **lang_config, translations=translations)

        more_translations = [
            make_translation('stops', 55, 'Microsoft'),
        ]

        self.cache.store(
            query=query,
            **lang_config,
            translations=[make_translation('stops', 55, 'Microsoft')])

        results_from_cache = self.cache.fetch(query=query, **lang_config)

        self.assertEqual(results_from_cache, translations + more_translations)
    def test_ca_translations(self):
        response = self.translators['nl-en'].translate(TranslationQuery(
            before_context='De directeur',
            query='treedt af',
            after_context=''
        ))
        translation = response.get_raw_translations()[0]
        self.assertIn(translation, ['shall resign', 'resigns'])  # was 'shall resign' but not anymore as of 2020

        response = self.translators['en-nl'].translate(TranslationQuery(
            before_context='Dark',
            query='matter',
            after_context='is an unidentified type of matter distinct from dark energy.'
        ))
        translation = response.get_raw_translations()[0]
        self.assertEqual('materie', translation)
Example #17
0
def minimize_context(context_str, from_lang_code, word_str):
    _query = TranslationQuery.for_word_occurrence(word_str, context_str, 1, 7)
    processor = RemoveUnnecessarySentences(from_lang_code)
    query = processor.process_query(_query)
    minimal_context = (
        query.before_context + ' ' + query.query + query.after_context)
    return minimal_context, query
Example #18
0
    def test_escaped_characters_in_translation(self):
        response = self.translators['de-en'].translate(TranslationQuery(
            before_context=u'Um Fernbusse aus dem Geschäft zu drängen, hat das Unternehmen damit begonnen, das',
            query='konzerneigene Flaggschiff',
            after_context=u'ICE straßentauglich zu machen. '))

        self.assertNotIn('&#39;', response.get_raw_translations()[0])
    def testQueryEndsWithPunctuation(self):
        translation = self.translators['es-en'].translate(
            TranslationQuery(before_context='Estoy en la',
                             query='cama,',
                             after_context=' e soy dormiendo'))

        self.assertIn(translation.get_raw_translations()[0], ['bed,', 'bed'])
Example #20
0
def minimize_context(context_str, from_lang_code, word_str):
    _query = TranslationQuery.for_word_occurrence(word_str, context_str, 1, 7)
    processor = RemoveUnnecessarySentences(from_lang_code)
    query = processor.process_query(_query)
    minimal_context = (query.before_context + ' ' + query.query +
                       query.after_context)
    return minimal_context, query
Example #21
0
def hello():
    data = request.get_json()

    source_language, target_language = data['source_language'], data[
        'target_language']

    if (source_language, target_language) not in translators:
        create_translator(source_language, target_language)

    query = TranslationQuery(
        before_context=data['before_context']
        if 'before_context' in data else '',
        query=data['query'] if 'query' in data else '',
        after_context=data['after_context'] if 'after_context' in data else '',
        max_translations=data['max_translations']
        if 'max_translations' in data else 10,
        budget=TranslationBudget(
            money=data['budget']['money'] if
            ('budget' in data and 'money' in data['budget']) else math.inf,
            time=data['budget']['time'] if
            ('budget' in data and 'time' in data['budget']) else math.inf))

    translator = translators[(source_language, target_language)]

    response = translator.translate(query)

    return Response(response.to_json(), mimetype='text/json')
Example #22
0
    def _translate(self, query: TranslationQuery) -> TranslationResponse:
        if not query.is_context_aware_request():
            return TranslationResponse(
                costs=TranslationCosts(
                    money=GoogleTranslator._cost_of_query(query.query)),
                translations=[
                    self.make_translation(
                        translation=self._simple_translate(query.query))
                ])

        google_query = f'{query.before_context}<{HTML_TAG}>{query.query}</{HTML_TAG}>{query.after_context}'

        costs = TranslationCosts(
            money=GoogleTranslator._cost_of_query(query.query))

        translation = self._simple_translate(google_query)

        translation_response = TranslationResponse(costs=costs)

        try:
            result = GoogleTranslator.parse_spanned_string(translation)
            translation_response.add_translation(self.make_translation(result))

        except ValueError:
            pass

        return translation_response
Example #23
0
    def compute_money_costs(self, query: TranslationQuery) -> float:
        money = len(query.query) * COST_PER_CHARACTER

        if query.is_context_aware_request():
            money += (len(f'<{HTML_TAG}>') +
                      len(f'</{HTML_TAG}>')) * COST_PER_CHARACTER

        return money
Example #24
0
 def test_issue_20__many_words_returned(self):
     res = self.translators['de-en'].translate(TranslationQuery(
         before_context='Die Nato-Partner Amerikas ',
         query='guckten',
         after_context=' betreten, als Trump ihnen in Brüssel vorhielt, sie sollten gefälligst ihre Verteidigungsbudgets ausbauen und entsprechend ihrer Zusage zwei Prozent des Bruttoinlandsproduktes fürs Militär ausgeben. Damit nährt er den alten Verdacht, Trump '
     ))
     first_translation = res.get_raw_translations()[0]
     self.assertTrue(len(first_translation)<20)
    def test_ca_translations(self):
        response = self.translators['nl-en'].translate(
            TranslationQuery(before_context='De directeur',
                             query='treedt af',
                             after_context=''))

        self.assertEqual(response.get_raw_translations()[0], 'resigns')

        response = self.translators['en-nl'].translate(
            TranslationQuery(
                before_context='Dark',
                query='matter',
                after_context=
                'is an unidentified type of matter distinct from dark energy.')
        )

        self.assertEqual(response.get_raw_translations()[0], 'materie')
Example #26
0
    def test_key_is_hashable(self):
        d = {self.k1: 'k1', self.k3: 'k3'}

        self.assertEquals(
            d[caches.CacheKey(query=TranslationQuery(
                before_context='De directeur',
                query='treedt af',
                after_context=''),
                              source_language='nl',
                              target_language='en')], 'k1')

        self.assertEquals(
            d[caches.CacheKey(query=TranslationQuery(
                before_context='De directeur',
                query='treedt af',
                after_context=''),
                              source_language='nl',
                              target_language='es')], 'k3')
    def testEnsureContextualHasPriority(self):
        response = self.bet.translate(
            TranslationQuery(
                before_context='Hunderte Züge und Dutzende Flüge wurden ',
                query='gestrichen',
                after_context=' .',
                max_translations=3))

        assert (response.translations[0]['translation'] == "canceled")
    def testUnicodeCharactersInContext(self):
        response = self.translators['de-en'].translate(
            TranslationQuery(before_context='Ein',
                             query='klein',
                             after_context=u'löwe'))

        # our translators change their mind sometimes, so we provide
        # two options for the translation so this test does not fail
        assert (response.get_raw_translations()[0] in ["little", "small"])
    def test_empty_cache(self):
        result = self.cache.fetch(
            TranslationQuery(before_context='De directeur',
                             query='treedt af',
                             after_context=''),
            source_language='nl',
            target_language='en')

        self.assertEqual(result, [])
    def _translate(self, query: TranslationQuery) -> TranslationResponse:
        """

        :param query: 
        :return: 
        """

        pos = self._get_pos_tag(query)
        response = self.word_api.getDefinitions(query.query, partOfSpeech=pos)
        if not response:

            if self._query_is_uppercase(query):
                query.query = query.query.lower()
                return self._translate(query)

            elif self._query_starts_with_quote(query):
                # try to see whether the problem is due to 'quotes' around the word name
                self._remove_quotes(query)
                return self._translate(query)

        if not response:
            response = []

        translations = []
        quality = int(self.get_quality())

        for d in response[:query.max_translations]:
            quality -= 1

            try:
                definition = self.definition_without_example_and_without_see_synonims(d)
                if self.not_too_long(definition):
                    translations.append(self.make_translation(definition, quality))

                meta_defined_word = self.is_meta_definition(definition)
                if meta_defined_word:
                    response2 = self.word_api.getDefinitions(meta_defined_word)
                    for d2 in response2[:query.max_translations]:
                        d2clean = self.definition_without_example_and_without_see_synonims(d2)
                        if self.not_too_long(d2clean):
                            translations.append(self.make_translation(meta_defined_word + ": " + d2clean, quality))
            except Exception as e:
                logger.info(f"Can't parse definition: {e}")

        if not translations:
            # if we don't know the translation, just parrot back the question
            translations.append(self.make_translation(query.query, quality))

        rez = TranslationResponse(
            translations=translations,
            costs=TranslationCosts(
                money=0  # API is free
            )
        )

        return rez
    def test_de2de(self):

        self.translator = GlosbeTranslator(source_language='de',
                                           target_language='de')

        response = self.translator.translate(
            TranslationQuery(query="Wunder", max_translations=20))

        self.assertIn("Mirakel",
                      [each['translation'] for each in response.translations])