Esempio n. 1
0
    def TranslateCaptions(self, translationContext, terminology_names=[]):

        marker = "<span>"
        sourceLanguageCode = translationContext["sourceLang"]
        targetLanguageCodes = translationContext["targetLangList"]
        translate_role = translationContext["roleArn"]
        bucket = translationContext["bucket"]
        inputPath = translationContext["inputLocation"]
        outputPath = translationContext["outputlocation"]
        jobPrefix = translationContext["jobPrefix"]
        try:
            translate_client = AwsHelper().getClient('translate')
            targetLanguageCode = targetLanguageCodes[0]
            self.logger.debug(
                "Starting translation to {}".format(targetLanguageCode))
            singletonTargetList = []
            singletonTargetList.append(targetLanguageCode)
            millis = int(round(time.time() * 1000))
            job_name = jobPrefix + str(millis)
            self.logger.debug("JobName: {}".format(job_name))

            terminology_name = []
            if len(terminology_names) > 0:
                for item in terminology_names:
                    if targetLanguageCode in item['TargetLanguageCodes']:
                        terminology_name.append(item['Name'])
                        break
                if len(terminology_name) == 0:
                    self.logger.debug(int("No custom terminology specified."))
                else:
                    self.logger.debug(
                        "Using custom terminology {}".format(terminology_name))

            # Save the delimited transcript text to S3
            response = translate_client.start_text_translation_job(
                JobName=job_name,
                InputDataConfig={
                    'S3Uri': "s3://{}/{}".format(bucket, inputPath),
                    'ContentType': "text/html"
                },
                OutputDataConfig={
                    'S3Uri': "s3://{}/{}".format(bucket, outputPath)
                },
                DataAccessRoleArn=translate_role,
                SourceLanguageCode=sourceLanguageCode,
                TargetLanguageCodes=singletonTargetList,
                TerminologyNames=terminology_name)
            jobinfo = {
                "JobId": response["JobId"],
                "TargetLanguageCode": targetLanguageCode
            }
            return jobinfo

        except Exception as e:
            self.logger.error(e)
            raise e
def startTranslationJob(bucketName, sourceCode, destCode, access_role):
    translate = AwsHelper().getClient('translate')
    try:
        millis = int(round(time.time() * 1000))
        response = translate.start_text_translation_job(
            JobName="TranslateJob-json-{}".format(millis),
            InputDataConfig={
                'S3Uri': "s3://{}/xmlin/".format(bucketName),
                'ContentType': 'text/html'
            },
            OutputDataConfig={'S3Uri': "s3://{}/xmlout/".format(bucketName)},
            DataAccessRoleArn=access_role,
            SourceLanguageCode=sourceCode,
            TargetLanguageCodes=[destCode])
        print(response["JobId"])
    except ClientError as e:
        logger.error("An error occured starting the Translate Batch Job: %s" %
                     e)