def extract(self): self.timer.start(1000) if not self.is_merged: self.errors = error.ErrorWindow("请先进行句子对齐操作") self.errors.show() else: source_language = -1 text = self.source_lang.currentText().strip() if text in languages.keys(): source_language = languages[text] if self.aligned_phrase is None: if source_language >= 0: self.save_current_text() process_file(self.save_source_sentence, self.save_target_sentence, source_language, self.args) self.main_window.statusBar().showMessage( 'Processing, please wait!') if not self.phrase_alignment.started: self.process_window = None self.phrase_alignment.start() self.aligned_phrase = self.phrase_alignment.values self.process_window = WaitWindow() self.process_window.show() else: if self.process_window is None: self.process_window = WaitWindow() self.process_window.show() if len(self.aligned_phrase) > 0: self.timer.stop() if self.process_window: self.process_window.close() self.main_window.statusBar().showMessage('Process finished!') win = Window(text=self.aligned_phrase) win.showMaximized()
def delete(self): index = -1 if len(self.table.selectedItems()) > 0: select_row = self.table.selectedItems()[0].row() if select_row > 0: index = int(self.table.item(select_row, 0).text()) - 1 if index >= 0: if self.is_merged: self.save_source_sentence.remove( self.save_source_sentence[index]) self.save_target_sentence.remove( self.save_target_sentence[index]) self.current_source = self.current_target = int( self.table.item(1, 0).text()) - 1 self.fill_table(False, True) else: if self.source_text is not None and len(self.source_text) > 0: self.source_text.remove(self.source_text[index]) self.current_source = int(self.table.item(1, 0).text()) - 1 self.fill_table(True) if self.target_text is not None and len(self.target_text) > 0: self.target_text.remove(self.target_text[index]) self.current_target = int(self.table.item(1, 0).text()) - 1 self.fill_table(False) else: self.errors = error.ErrorWindow("请选择需要删除的行") self.errors.show()
def save(self): fileName2, ok = QFileDialog.getSaveFileName( self, "文件保存", self.initial, "All Files (*);;Text Files (*.txt)") if len(fileName2) > 0 and self.is_merged: for i in range(self.table.rowCount() - 1): index = int(self.table.item(i + 1, 0).text()) - 1 self.save_source_sentence[index] = str( self.table.item(i + 1, 1).text()) self.save_target_sentence[index] = str( self.table.item(i + 1, 2).text()) f = open(fileName2, 'w', encoding='gbk') for i in range(len(self.save_source_sentence)): f.write(self.save_source_sentence[i].strip() + '\n') f.write(self.save_target_sentence[i].strip() + '\n') f.close() else: self.errors = error.ErrorWindow("请按照正确操作使用") self.errors.show()
def merge(self): source_language = -1 text = self.source_lang.currentText().strip() if text in languages.keys(): source_language = languages[text] if source_language >= 0: save_content = None if len(self.source_text) > 0 and len(self.target_text) > 0: source_text = '\n'.join(self.source_text) target_text = '\n'.join(self.target_text) if source_language == Globals.ENGLISH: self.args.init_from_params = self.args.en_init_from_params self.args.src_vocab_fpath = self.args.en_src_vocab_fpath self.args.trg_vocab_fpath = self.args.en_trg_vocab_fpath self.args.bpe_model_dir = self.args.en_bpe_model_dir save_content = Chinese_English.pip_line( source_text, target_text, False) elif source_language == Globals.FRENCH: self.args.init_from_params = self.args.fr_init_from_params self.args.src_vocab_fpath = self.args.fr_src_vocab_fpath self.args.trg_vocab_fpath = self.args.fr_trg_vocab_fpath self.args.bpe_model_dir = self.args.fr_bpe_model_dir save_content = Chinese_French.pip_line( source_text, target_text, False) elif source_language == Globals.GERMAN: self.args.init_from_params = self.args.de_init_from_params self.args.src_vocab_fpath = self.args.de_src_vocab_fpath self.args.trg_vocab_fpath = self.args.de_trg_vocab_fpath self.args.bpe_model_dir = self.args.de_bpe_model_dir save_content = Chinese_German.pip_line( source_text, target_text, False) elif source_language == Globals.RUSSIAN: self.args.init_from_params = self.args.ru_init_from_params self.args.src_vocab_fpath = self.args.ru_src_vocab_fpath self.args.trg_vocab_fpath = self.args.ru_trg_vocab_fpath self.args.bpe_model_dir = self.args.ru_bpe_model_dir save_content = Chinese_Russian.pip_line( source_text, target_text, False) elif source_language == Globals.JAPAN: save_content = Chinese_Japan.pip_line( source_text, target_text, False) elif source_language == Globals.ARABIA: save_content = Chinese_Arabia.pip_line( source_text, target_text, False) elif source_language == Globals.KOREAN: save_content = Chinese_Korean.pip_line( source_text, target_text, False) elif source_language == Globals.SPAIN: self.args.init_from_params = self.args.es_init_from_params self.args.src_vocab_fpath = self.args.es_src_vocab_fpath self.args.trg_vocab_fpath = self.args.es_trg_vocab_fpath self.args.bpe_model_dir = self.args.es_bpe_model_dir save_content = Chinese_Spain.pip_line( source_text, target_text, False) elif source_language == Globals.PORTUGAL: save_content = Chinese_Portugal.pip_line( source_text, target_text, False) if save_content is not None: self.is_merged = True self.current_source = self.current_target = 0 self.save_source_sentence = [] self.save_target_sentence = [] for para in save_content: for i in range(len(para) // 2): self.save_source_sentence.append(para[2 * i]) self.save_target_sentence.append(para[2 * i + 1]) self.fill_table(False, True) elif self.source_text is None or self.target_text is None: self.errors = error.ErrorWindow("请选择文件") self.errors.show() elif source_language == -1: self.errors = error.ErrorWindow("请选择语言") self.errors.show() if self.phrase_alignment is None: self.phrase_alignment = phrase_extract(source_language, self.args)
def fill_table(self, is_source, is_list=False): self.table.setGeometry( QtCore.QRect(0, 0, self.width, self.height - 150)) self.table.setStyleSheet( "QWidget:hover{background-color:rgb(223,223,223);}") if self.initial_flag: self.current_source = self.current_target = 0 if is_list: if len(self.save_source_sentence) > 0: self.source_count = self.target_count = len( self.save_source_sentence) count = self.source_count - self.current_source if count <= self.max_table_row: if self.table.rowCount() != count: self.table.setRowCount(count + 1) for i in range(count): if i + self.current_source < self.source_count: self.table.setItem( i + 1, 0, QtWidgets.QTableWidgetItem( " " + str(i + 1 + self.current_source))) self.table.setItem( i + 1, 1, QtWidgets.QTableWidgetItem( self.save_source_sentence[ i + self.current_source].strip())) self.table.setItem( i + 1, 2, QtWidgets.QTableWidgetItem( self.save_target_sentence[ i + self.current_source].strip())) else: if self.table.rowCount() <= self.max_table_row: self.table.setRowCount(self.max_table_row + 1) for i in range(self.max_table_row): if i + self.current_source < self.source_count: self.table.setItem( i + 1, 0, QtWidgets.QTableWidgetItem( " " + str(i + 1 + self.current_source))) self.table.setItem( i + 1, 1, QtWidgets.QTableWidgetItem( self.save_source_sentence[ i + self.current_source].strip())) self.table.setItem( i + 1, 2, QtWidgets.QTableWidgetItem( self.save_target_sentence[ i + self.current_target].strip())) self.current_source += self.max_table_row self.current_target += self.max_table_row else: self.errors = error.ErrorWindow("请按照正确操作使用") self.errors.show() else: if is_source: sentence = self.source_text count = self.source_count = len(sentence) if self.source_count < self.target_count: count = self.target_count count -= self.current_source else: sentence = self.target_text count = self.target_count = len(sentence) if self.target_count < self.source_count: count = self.source_count count -= self.current_target if count <= self.max_table_row: if self.table.rowCount() != count: self.table.setRowCount(count + 1) if is_source: # print(self.current_source) for i in range(count): if i + self.current_source < self.source_count: self.table.setItem( i + 1, 0, QtWidgets.QTableWidgetItem( " " + str(i + 1 + self.current_source))) # print(sentence[i+self.current_source]) self.table.setItem( i + 1, 1, QtWidgets.QTableWidgetItem( sentence[i + self.current_source].strip())) else: self.table.setItem(i + 1, 1, QtWidgets.QTableWidgetItem("")) else: for i in range(count): if i + self.current_target < self.target_count: self.table.setItem( i + 1, 0, QtWidgets.QTableWidgetItem( " " + str(i + 1 + self.current_target))) self.table.setItem( i + 1, 2, QtWidgets.QTableWidgetItem( sentence[i + self.current_target].strip())) else: self.table.setItem(i + 1, 2, QtWidgets.QTableWidgetItem("")) else: if self.table.rowCount() <= self.max_table_row: self.table.setRowCount(self.max_table_row + 1) if is_source: index = 0 for i in range(self.max_table_row): if i + self.current_source < self.source_count: index += 1 self.table.setItem( i + 1, 0, QtWidgets.QTableWidgetItem( " " + str(1 + i + self.current_source))) self.table.setItem( i + 1, 1, QtWidgets.QTableWidgetItem( sentence[i + self.current_source].strip())) else: self.table.setItem(i + 1, 1, QtWidgets.QTableWidgetItem("")) self.current_source += index else: index = 0 for i in range(self.max_table_row): if i + self.current_target < self.target_count: index += 1 self.table.setItem( i + 1, 0, QtWidgets.QTableWidgetItem( " " + str(1 + i + self.current_target))) self.table.setItem( i + 1, 2, QtWidgets.QTableWidgetItem( sentence[i + self.current_target].strip())) else: self.table.setItem(i + 1, 2, QtWidgets.QTableWidgetItem("")) self.current_target += index # print('current_source', self.current_source) # print('current_target', self.current_target) self.table.resizeRowsToContents() if not self.button_show: self.add_button()