Exemplo n.º 1
0
 def translate(self,
               text,
               dest=None,
               src=None):
     """
     Translate text manually.
     """
     if self.trans_doc_name.endswith("docx"):
         trans_doc = docx.Document()
         trans_doc.add_paragraph(text=text)
         trans_doc.save(self.trans_doc_name)
         if self.input_m:
             self.input_m(_("Wait for the manual translation. "
                            "Press Enter to continue."))
         else:
             print(_("Wait 20 seconds for the manual translation. "))
             widgets = [_("Manual translation: "),
                        progressbar.Percentage(), ' ',
                        progressbar.Bar(), ' ',
                        progressbar.ETA()]
             pbar = progressbar.ProgressBar(widgets=widgets, maxval=20).start()
             for i in range(20):
                 pbar.update(i)
                 time.sleep(1)
             pbar.finish()
         trans_doc = docx.Document(self.trans_doc_name)
         para_list = []
         for para in trans_doc.paragraphs:
             para_list.append(para.text)
         trans_doc_str = '\n'.join(para_list)
     else:
         trans_doc_name = sub_utils.str_to_file(
             str_=text,
             output=self.trans_doc_name,
             input_m=self.input_m)
         if self.input_m:
             self.input_m(_("Wait for the manual translation. "
                            "Press Enter to continue."))
         else:
             print(_("Wait 20 seconds for the manual translation. "))
             widgets = [_("Manual translation: "),
                        progressbar.Percentage(), ' ',
                        progressbar.Bar(), ' ',
                        progressbar.ETA()]
             pbar = progressbar.ProgressBar(widgets=widgets, maxval=20).start()
             for i in range(20):
                 pbar.update(i)
                 time.sleep(1)
             pbar.finish()
         trans_doc = open(trans_doc_name, encoding=constants.DEFAULT_ENCODING)
         trans_doc_str = trans_doc.read()
         trans_doc.close()
     constants.DELETE_PATH(self.trans_doc_name)
     temp_str = "manual"
     temp_part = googletrans.client.TranslatedPart(
         text=temp_str,
         candidates=[temp_str])
     return googletrans.client.Translated(
         src=src, dest=dest, origin=temp_str, text=trans_doc_str,
         pronunciation=temp_str, parts=[temp_part], extra_data=temp_str)
Exemplo n.º 2
0
    def man_get_vtt_words_index(self):
        """
        Get end timestamps from a SSAEvent list automatically by external regions.
        """
        events = []
        path = self.path[:-3] + "txt"
        path = str_to_file(str_=self.to_text_str(), output=path, input_m=input)
        input(
            _("Wait for the events manual adjustment. "
              "Press Enter to continue."))
        line_count = 0
        i = 0
        j = 0
        vtt_len = len(self.vtt_words)
        is_paused = False
        trans = str.maketrans(string.punctuation,
                              " " * len(string.punctuation))
        while True:
            file_p = open(path, encoding=constants.DEFAULT_ENCODING)
            line_list = file_p.readlines()
            line_list_len = len(line_list)
            file_p.close()
            k = line_count
            while k < line_list_len:
                word_list = line_list[k].split()
                event = pysubs2.SSAEvent(start=self.vtt_words[i].start)
                word_list_len = len(word_list)
                while j < word_list_len:
                    if self.vtt_words[i].word != word_list[j]:
                        if fuzz.partial_ratio(
                                self.vtt_words[i].word.lower().translate(
                                    trans).replace(" ", ""),
                                word_list[j].lower().translate(trans).replace(
                                    " ", "")) != 100:
                            if self.vtt_words_index:
                                start_delta = self.vtt_words_index[-1]
                            else:
                                start_delta = 0
                            if i < vtt_len - 5:
                                end_delta = i + 6
                            else:
                                end_delta = vtt_len
                            print(
                                _("\nLine {num}, word {num2}").format(
                                    num=len(events), num2=j))
                            cur_line = ""
                            for vtt_word in self.vtt_words[
                                    start_delta:end_delta]:
                                cur_line = "{cur_line} {word}".format(
                                    cur_line=cur_line, word=vtt_word.word)
                            print(cur_line)
                            print(" ".join(word_list))
                            print("{word} | {word2}".format(
                                word=self.vtt_words[i].word,
                                word2=word_list[j]))
                            result = input(
                                _("Press Enter to manual adjust. "
                                  "Input 1 to overwrite."))
                            if result != "1":
                                line_count = k
                                is_paused = True
                                break

                            self.vtt_words[i].word = word_list[j]
                            is_paused = False
                        else:
                            if is_paused:
                                is_paused = False
                            self.vtt_words[i].word = word_list[j]

                    i = i + 1
                    j = j + 1
                    if i > vtt_len:
                        break
                if is_paused:
                    break
                j = 0
                self.vtt_words_index.append(i)
                if i:
                    event.end = self.vtt_words[i - 1].end
                events.append(event)
                k = k + 1
            if not is_paused:
                break
        constants.DELETE_PATH(path)
        return events