Ejemplo n.º 1
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
Ejemplo n.º 2
0
class TestRemoveUnnecessarySentences(TestCase):
    def setUp(self):
        self.query_processor = RemoveUnnecessarySentences(language_code='nl')

    def test_single_sentence(self):
        q = TranslationQuery(
            before_context='De directeur',
            query='treedt',
            after_context=' af.'
        )

        result = self.query_processor.process_query(q)

        self.assertEqual(q, result)

    def test_multiple_sentences(self):
        query = TranslationQuery(
            before_context='Justitieminister Koen Geens (CD&V) werkt aan een wetsontwerp dat burgerinfiltranten '
                           'mogelijk maakt in de strijd tegen',
            query='terrorisme',
            after_context='en georganiseerde misdaad. Dat zegt hij woensdag in Knack, nadat Brussels '
                          'procureur-generaal Johan Delmulle daar vorig najaar een lans voor had gebroken. '
                          'Daarnaast werkt Geens ook aan een regeling rond spijtoptanten. Een akkoord binnen de '
                          'meerderheid is er nog niet..'
        )

        result = self.query_processor.process_query(query)

        self.assertEqual(query.before_context, result.before_context)  # before context should remain the same
        self.assertEqual(query.query, result.query)  # query should be untouched
        self.assertEqual(result.after_context, 'en georganiseerde misdaad.')
Ejemplo n.º 3
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
Ejemplo n.º 4
0
    def build_with_context(source_language: str,
                           target_language: str,
                           key=None) -> MicrosoftTranslator:
        """
        Builds a Microsoft translator with suitable context processors for the given source and target language.

        :param source_language: 
        :param target_language: 
        :param key: 
        :return: 
        """

        translator = MicrosoftTranslatorFactory.build_clean(
            source_language, target_language, key)

        # Right now only apply the processor to Dutch, English, German, French, Spanish
        if source_language in ['nl', 'en', 'de', 'fr', 'es']:
            translator.add_query_processor(
                RemoveUnnecessarySentences(source_language))

        if source_language in list(conjunctions.keys()):
            translator.add_query_processor(
                RemoveUnnecessaryConjunctions(conjunctions[source_language]))

        translator.translator_name = 'Microsoft - with context'
        translator.service_name = 'Microsoft - with context'

        return translator
Ejemplo n.º 5
0
 def setUp(self):
     self.query_processor = RemoveUnnecessarySentences(language_code='nl')