def test_source_provided_translation(self): '''Test1 fast translation with no language detect features used.''' name = 'T1' tx = Translator(source='en', destination='cs', mix=True) self.document.setname(extra=name) try: tx(self.document) except ValueError as e: logging.error( 'Google has blocked your current IP. Testing not possible.') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, TestNaming().filename + TestNaming().sep + TestNaming().id \ + TestNaming().sep + name + TestNaming().ext) # Test translation: doc = Document(local_filepath) line = doc.paragraphs[0].text self.assertEqual(line, 'Ahoj světe\nHello world')
def test_eng_2_eng(self): '''Test2 English to English translation.''' name = 'T2' tx = Translator(source='en', mix=True) self.document.setname(extra=name) try: tx(self.document) except ValueError as e: logging.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, TestNaming().filename + TestNaming().sep + TestNaming().id \ + TestNaming().sep + name + TestNaming().ext) # Test translation: doc = Document(local_filepath) line = doc.paragraphs[0].text self.assertEqual(line, 'Hello world\nHello world')
def test_translated_name(self): '''Test7 name should be translated''' name = 'T7' tx = Translator(source='en', destination='cs', mix=True) self.document.setname(translate=True, extra=name) try: tx(self.document) except ValueError as e: logging.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, 'Ahoj_svět_slovo_' \ + name + TestNaming().ext) # Test translation: doc = Document(local_filepath) line = doc.paragraphs[0].text self.assertEqual(line, 'Ahoj světe\nHello world')
def test_translated_and_dest_abbreviation_naming(self): '''Test8 name is translated and destination abbreviation added.''' name = 'T8' tx = Translator(source='en', destination='fr', mix=True, abbreviation=True, translate=True, extra=name) try: tx(self.document) except ValueError as e: logging.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, 'Bonjour_monde_mot' \ + '_fr_' + name + TestNaming().ext) # Test translation: doc = Document(local_filepath) line = doc.paragraphs[0].text self.assertEqual(line, 'Bonjour le monde\nHello world')
def test_eng_2_eng(self): '''Test2 English to English translation.''' name = 'T2' tx = Translator(source='en', mix=True) self.document.setname(extra=name) try: tx(self.document) except ValueError as e: logger.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, TestNaming().filename + TestNaming().sep + TestNaming().id \ + TestNaming().sep + name + TestNaming().ext) # Test translation: self.assertTrue(os.path.exists(local_filepath), 'This file has not been named correctly') with open(local_filepath, 'r+') as doc: line = doc.read() self.assertEqual( line, 'Hello world # say "Hello" to the world.\n say "Hello" to the world.', 'Text is not as expected.')
def test_quick_detect(self): '''Test3 quick language detection.''' name = 'T3' tx = Translator(destination='cs', quick=True, mix=True, extra=name) try: tx(self.document) except ValueError as e: logging.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, TestNaming().filename + TestNaming().sep + TestNaming().id \ + TestNaming().sep + name + TestNaming().ext) # Test translation: doc = Document(local_filepath) line = doc.paragraphs[0].text self.assertEqual(line, 'Ahoj světe\nHello world')
def test_file_output(self): document = WordDocx( os.path.join(TestTextExtraction.filepath, TestTextExtraction.filename)) name_extra = 'output_test' tx = Translator(source='cs', destination='cs', extra=name_extra) try: tx(document) except ValueError as e: logging.error( 'Google has blocked your current IP. Testing not possible.') # Save file document.save() f1 = Document( os.path.join(TestTextExtraction.filepath, TestTextExtraction.filename)) f2 = Document( os.path.join( TestTextExtraction.filepath, TestTextExtraction.filename.split('.')[0] + '_' + name_extra + '.' + TestTextExtraction.filename.split('.')[1])) self.assertTrue(TestTextExtraction.comp_docx(f1, f2), 'File output is not as expected.')
def test_source_provided_translation(self): '''Test1 fast translation with no language detect features used.''' name = 'T1' tx = Translator(source='en', destination='cs', mix=True) self.document.setname(extra=name) try: tx(self.document) except ValueError as e: logger.error( 'Google has blocked your current IP. Testing not possible.') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, TestNaming().filename + TestNaming().sep + TestNaming().id \ + TestNaming().sep + name + TestNaming().ext) # Test translation: self.assertTrue(os.path.exists(local_filepath), 'This file has not been named correctly') with open(local_filepath, 'r+') as doc: line = doc.read() self.assertEqual( line, 'Hello world # říci "Ahoj" světu.\n say "Hello" to the world.', 'Text is not as expected.')
def test_translated_and_dest_abbreviation_naming(self): '''Test8 name is translated and destination abbreviation added.''' name = 'T8' tx = Translator(source='en', destination='fr', mix=True, abbreviation=True, translate=True, extra=name) try: tx(self.document) except ValueError as e: logger.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n') try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, 'Bonjour_monde_code' \ + '_fr_' + name + TestNaming().ext) # Test translation: self.assertTrue(os.path.exists(local_filepath), 'This file has not been named correctly') with open(local_filepath, 'r+') as doc: line = doc.read() self.assertEqual( line, 'Hello world # dites "bonjour" au monde.\n say "Hello" to the world.', 'Text is not as expected.')
def test_best_certainty(self): '''Test5 best certainty language detection.''' name = 'T5' tx = Translator(destination='cs', mix=True) self.document.setname(extra=name) try: tx(self.document) except ValueError as e: logger.error( 'Google has blocked your current IP. We suggest you use a VPN if possible ' 'or switch the VPN server you are using.\n*******\n%s\n*******', e) try: self.document.save() except PermissionError: print( 'PermissionError: Close any instances of the files generated by this test unit and load the unit again.' ) local_filepath = os.path.join(TestNaming().test_directory, TestNaming().filename + TestNaming().sep + TestNaming().id \ + TestNaming().sep + name + TestNaming().ext) print(local_filepath) # Test translation: self.assertTrue(os.path.exists(local_filepath), 'This file has not been named correctly') with open(local_filepath, 'r+') as doc: line = doc.read() self.assertEqual( line, 'Hello world # říci "Ahoj" světu.\n say "Hello" to the world.' )
def test_anti_stripping(self): from TranslationTools.Translator import Translator a = 'Hello ' b = ' world' c = 'Hello\t' d = '\tworld' e = 'Hello\n' f = '\nworld' A = Translator().translate(a, 'en', 'en') B = Translator().translate(b, 'en', 'en') C = Translator().translate(c, 'en', 'en') D = Translator().translate(d, 'en', 'en') E = Translator().translate(e, 'en', 'en') F = Translator().translate(f, 'en', 'en') self.assertEqual(a + b, A + B) self.assertEqual(c + d, C + D) self.assertEqual(e + f, E + F)
def setUpClass(cls): cls.filepath = TEST_OUT_DIR cls.filename = 'TestDoc.docx' document = Document() document.add_heading(source_text_for_test[0]) document.add_paragraph(source_text_for_test[1]) heading = document.add_heading(level=2) heading.add_run(source_text_for_test[2]) bold_run1 = heading.add_run(source_text_for_test[3]) bold_run1.bold heading.add_run(source_text_for_test[4]) bullet1 = document.add_paragraph(style='List Bullet') bold_run1 = bullet1.add_run(source_text_for_test[5]) bold_run1.bold = True bullet1.add_run(source_text_for_test[6]) bullet2 = document.add_paragraph(style='ListBullet') bold_run1 = bullet2.add_run(source_text_for_test[7]) bold_run1.bold = True bullet2.add_run(source_text_for_test[8]) bullet3 = document.add_paragraph(style='ListBullet') bold_run1 = bullet3.add_run(source_text_for_test[9]) bold_run1.bold = True bullet3.add_run(source_text_for_test[10]) document.add_heading(source_text_for_test[11], level=2) document.add_paragraph(source_text_for_test[12]) document.add_heading(source_text_for_test[13], level=2) document.add_paragraph(source_text_for_test[14]) document.add_heading(source_text_for_test[15], level=2) document.add_paragraph(source_text_for_test[16]) document.add_paragraph(source_text_for_test[17]) try: document.save(os.path.join(cls.filepath, cls.filename)) except PermissionError: base, ext = cls.filename.split('.') i = 1 while i < 100: name = base + str(i) + '.' + ext if not os.path.exists(os.path.join(cls.filepath, name)): cls.filename = name break i += 1 logging.info( 'User warning: TestDoc.docx is open in another program. ' 'Document has been saved to {}'.format(cls.filename)) try: document.save(os.path.join(cls.filepath, cls.filename)) except PermissionError: logging.error('Permission Error: Failed to save document') cls.document = WordDocx( os.path.join(TestTextExtraction.filepath, TestTextExtraction.filename)) cls.tx = Translator(source='cs', destination='en', extra='Extraction') try: cls.tx(cls.document) except ValueError as e: logging.error( 'Google has blocked your current IP. Testing not possible.') sys.exit()
logger.info('unpacking paths..') if Interpres_Globals.VERBOSITY < 20: # Debug or less for f in files: logger.debug(f) # remove duplicates files = list(set(files)) logger.info('removing duplicates..') # create translator instance logger.info("Creating translator.") translator = Translator(source=args.source, destination=args.target, dyn=args.dyn, mix=args.mix, extra=args.extra, abbreviation=args.abbreviation, translate=args.translate) logger.debug('src=%s, dest=%s, dyn=%s, mix=%s', args.source, args.target, args.dyn, args.mix) extension_counter = {} # create concrete objects for path in files: try: with fragile(DocumentFactory.make_dao(path)(path)) as doc: if args.keywords: if not [True for key in args.keywords if key in doc]: doc.close() # safely close document handle fragile.Break