def test_file_area():
    def open_file(err_msg, files_to_open):
        assert not err_msg

        wl_file_area.Wl_Worker_Open_Files(
            main,
            dialog_progress=wl_dialogs_misc.Wl_Dialog_Progress(main, text=''),
            update_gui=update_gui,
            files_to_open=files_to_open).run()

    def update_gui(err_msg, new_files):
        assert not err_msg

        main.settings_custom['file_area']['files_open'].extend(new_files)

    # Reset custom settings
    main.settings_custom = copy.deepcopy(main.settings_default)

    # Clean cached files
    for file in glob.glob('imports/*.*'):
        os.remove(file)

    for file_path in glob.glob('wl_tests_files/wl_file_area/work_area/*.txt'):
        time_start = time.time()

        print(f'Loading file "{os.path.split(file_path)[1]}"... ', end='')

        table = QObject()
        table.files_to_open = []

        wl_file_area.Wl_Worker_Add_Files(
            main,
            dialog_progress=wl_dialogs_misc.Wl_Dialog_Progress(main, text=''),
            update_gui=open_file,
            file_paths=[file_path],
            table=table).run()

        new_file = main.settings_custom['file_area']['files_open'][-1]

        assert new_file['selected']
        assert new_file['name'] == new_file['name_old'] == os.path.splitext(
            os.path.split(file_path)[-1])[0]
        assert new_file['path'] == wl_misc.get_normalized_path(
            file_path).replace(
                os.path.join('wl_tests_files', 'wl_file_area', 'work_area'),
                'imports')
        assert new_file['path_original'] == wl_misc.get_normalized_path(
            file_path)
        assert new_file['encoding'] == 'utf_8'
        assert new_file['lang'] == re.search(r'(?<=\[)[a-z_]+(?=\])',
                                             file_path).group()
        assert new_file['tokenized'] == 'No'
        assert new_file['tagged'] == 'No'

        print(f'done! (In {round(time.time() - time_start, 2)} seconds)')

    # Save Settings
    with open('wl_tests/wl_settings.pickle', 'wb') as f:
        pickle.dump(main.settings_custom, f)
예제 #2
0
    def open_file(err_msg, files_to_open):
        assert not err_msg

        wl_file_area.Wl_Worker_Open_Files(
            main,
            dialog_progress=wl_dialogs_misc.Wl_Dialog_Progress(main, text=''),
            update_gui=update_gui,
            files_to_open=files_to_open).run()
예제 #3
0
    def find_all(self):
        def update_gui():
            if self.items_found:
                for table in self.tables:
                    table.disable_updates()

                for table, row, col in self.items_found:
                    if table.indexWidget(table.model().index(row, col)):
                        table.indexWidget(table.model().index(
                            row,
                            col)).setStyleSheet('border: 1px solid #E53E3A;')
                    else:
                        table.model().item(row, col).setForeground(
                            QBrush(QColor('#FFF')))
                        table.model().item(row, col).setBackground(
                            QBrush(QColor('#E53E3A')))

                for table in self.tables:
                    table.enable_updates()
            else:
                wl_msg_boxes.Wl_Msg_Box_Warning(
                    self.main,
                    title=self.tr('No Search Results'),
                    text=self.tr('''
                        <div>Searching has completed successfully, but there are no results found.</div>
                        <div>You can change your settings and try again.</div>
                    ''')).open()

            len_items_found = len(self.items_found)
            msg_item = self.tr('item') if len_items_found == 1 else self.tr(
                'items')

            self.main.statusBar().showMessage(
                self.tr('Found {} {}.').format(len_items_found, msg_item))

        if (not self.settings['multi_search_mode']
                and self.settings['search_term']
                or self.settings['multi_search_mode']
                and self.settings['search_terms']):
            self.clr_highlights()

            dialog_progress = wl_dialogs_misc.Wl_Dialog_Progress(
                self.main, text=self.tr('Searching in results...'))

            worker_results_search = Wl_Worker_Results_Search(
                self.main,
                dialog_progress=dialog_progress,
                update_gui=update_gui,
                dialog=self)

            thread_results_search = wl_threading.Wl_Thread(
                worker_results_search)
            thread_results_search.start_worker()
        else:
            wl_msg_boxes.wl_msg_box_missing_search_terms(self.main)

            self.main.statusBar().showMessage(
                self.tr('An error occured during searching!'))
예제 #4
0
    def preview_pos_tagger_changed(self):
        self.settings_custom['preview_pos_tagger'][self.settings_custom[
            'preview_lang']] = wl_nlp_utils.to_lang_util_code(
                self.main,
                util_type='pos_taggers',
                util_text=self.combo_box_tagsets_pos_tagger.currentText())

        if 'spacy' not in self.settings_custom['preview_pos_tagger'][
                self.settings_custom['preview_lang']]:
            self.combo_box_tagsets_lang.setEnabled(False)
            self.combo_box_tagsets_pos_tagger.setEnabled(False)
            self.button_tagsets_reset.setEnabled(False)
            self.button_tagsets_reset_all.setEnabled(False)
            self.table_mappings.setEnabled(True)

            dialog_progress = wl_dialogs_misc.Wl_Dialog_Progress(
                self.main, text=self.tr('Fetching data...'))

            worker_fetch_data = Wl_Worker_Fetch_Data_Tagsets(
                self.main,
                dialog_progress=dialog_progress,
                update_gui=self.update_gui)

            thread_fetch_data = wl_threading.Wl_Thread(worker_fetch_data)
            thread_fetch_data.start()

            dialog_progress.show()
            dialog_progress.raise_()

            thread_fetch_data.quit()
            thread_fetch_data.wait()
        else:
            self.stacked_widget_num_pos_tags.setCurrentIndex(1)

            self.button_tagsets_reset.setEnabled(False)
            self.button_tagsets_reset_all.setEnabled(False)

            self.table_mappings.clr_table()
            self.table_mappings.setEnabled(False)
예제 #5
0
    def sort_results_parallel(self):
        def update_gui(results_src, results_tgt):
            results = []

            # Create new labels
            for ((left_old, node_old, right_old, no_seg_src, no_seg_pct_src),
                 (parallel_text_old, no_seg_tgt,
                  no_seg_pct_tgt)) in zip(results_src, results_tgt):
                left_new = wl_labels.Wl_Label_Html('', self.tables[0])
                node_new = wl_labels.Wl_Label_Html(node_old.text(),
                                                   self.tables[0])
                right_new = wl_labels.Wl_Label_Html('', self.tables[0])
                parallel_text_new = wl_labels.Wl_Label_Html(
                    parallel_text_old.text(), self.tables[1])

                left_new.text_raw = left_old.text_raw.copy()
                node_new.text_raw = node_old.text_raw.copy()
                right_new.text_raw = right_old.text_raw.copy()
                parallel_text_new.text_raw = parallel_text_old.text_raw.copy()

                left_new.text_search = left_old.text_search.copy()
                node_new.text_search = node_old.text_search.copy()
                right_new.text_search = right_old.text_search.copy()
                parallel_text_new.text_search = parallel_text_old.text_search.copy(
                )

                results.append([(left_new, node_new, right_new, no_seg_src,
                                 no_seg_pct_src),
                                (parallel_text_new, no_seg_tgt, no_seg_pct_tgt)
                                ])

            # Sort results
            for sorting_col, sorting_order in reversed(
                    self.settings['sorting_rules']):
                reverse = 0 if sorting_order == self.tr('Ascending') else 1

                if sorting_col == self.tr('Node'):
                    results.sort(key=lambda item: item[0][1].text_raw,
                                 reverse=reverse)
                elif sorting_col == self.tr('Segment No.'):
                    results.sort(key=lambda item: item[0][3], reverse=reverse)
                else:
                    span = int(sorting_col[1:])

                    if self.tr('L') in sorting_col:
                        results.sort(
                            key=lambda item: item[0][0].text_raw[-span],
                            reverse=reverse)
                    elif self.tr('R') in sorting_col:
                        results.sort(
                            key=lambda item: item[0][2].text_raw[span - 1],
                            reverse=reverse)

            self.tables[0].disable_updates()
            self.tables[1].disable_updates()

            for i, ((left, node, right, no_seg_src, no_seg_pct_src),
                    (parallel_text, no_seg_tgt,
                     no_seg_pct_tgt)) in enumerate(results):
                # Remove empty tokens
                text_left = [token for token in left.text_raw if token]
                text_right = [token for token in right.text_raw if token]

                highlight_colors = self.main.settings_custom['concordancer'][
                    'sort_results']['highlight_colors']

                i_highlight_color_left = 1
                i_highlight_color_right = 1

                for sorting_col, _ in self.settings['sorting_rules']:
                    if re.search(self.tr(r'^L[0-9]+$'), sorting_col) and int(
                            sorting_col[1:]) <= len(text_left):
                        hightlight_color = highlight_colors[
                            i_highlight_color_left % len(highlight_colors)]

                        text_left[-int(sorting_col[1:])] = f'''
                            <span style="color: {hightlight_color}; font-weight: bold;">
                                {text_left[-int(sorting_col[1:])]}
                            </span>
                        '''

                        i_highlight_color_left += 1
                    elif re.search(self.tr(r'^R[0-9]+$'), sorting_col) and int(
                            sorting_col[1:]) - 1 < len(text_right):
                        hightlight_color = highlight_colors[
                            i_highlight_color_right % len(highlight_colors)]

                        text_right[int(sorting_col[1:]) - 1] = f'''
                            <span style="color: {hightlight_color}; font-weight: bold;">
                                {text_right[int(sorting_col[1:]) - 1]}
                            </span>
                        '''

                        i_highlight_color_right += 1

                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 0)).setText(' '.join(text_left))
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 1)).setText(node.text())
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 2)).setText(' '.join(text_right))

                self.tables[0].indexWidget(self.tables[0].model().index(
                    i,
                    0)).text_raw = [token for token in left.text_raw if token]
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 1)).text_raw = node.text_raw
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 2)).text_raw = [
                        token for token in right.text_raw if token
                    ]

                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 0)).text_search = left.text_search
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 1)).text_search = node.text_search
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 2)).text_search = right.text_search

                self.tables[0].set_item_num_val(i, 3, no_seg_src)
                self.tables[0].set_item_num_val(i, 4, no_seg_pct_src)

                self.tables[1].indexWidget(self.tables[1].model().index(
                    i, 0)).setText(parallel_text.text())

                self.tables[1].indexWidget(self.tables[1].model().index(
                    i, 0)).text_raw = [
                        token for token in parallel_text.text_raw if token
                    ]
                self.tables[1].indexWidget(self.tables[1].model().index(
                    i, 0)).text_search = parallel_text.text_search

                self.tables[1].set_item_num_val(i, 1, no_seg_tgt)
                self.tables[1].set_item_num_val(i, 2, no_seg_pct_tgt)

            self.tables[0].enable_updates(emit_signals=False)
            self.tables[1].enable_updates(emit_signals=False)

        if not self.tables[0].is_empty():
            dialog_progress = wl_dialogs_misc.Wl_Dialog_Progress(
                self.main, text=self.tr('Sorting results...'))

            worker_results_sort_concordancer_parallel = Wl_Worker_Results_Sort_Concordancer_Parallel(
                self.main,
                dialog_progress=dialog_progress,
                update_gui=update_gui,
                dialog=self)

            thread_results_sort_concordancer_parallel = wl_threading.Wl_Thread(
                worker_results_sort_concordancer_parallel)
            thread_results_sort_concordancer_parallel.start_worker()

        self.main.statusBar().showMessage(
            self.tr('The results in the table has been successfully sorted.'))
예제 #6
0
    def sort_results(self):
        def update_gui(results):
            # Create new labels
            for i, (left_old, node_old, right_old, _, _, _, _, _, _, _,
                    _) in enumerate(results):
                left_new = wl_labels.Wl_Label_Html('', self.tables[0])
                node_new = wl_labels.Wl_Label_Html(node_old.text(),
                                                   self.tables[0])
                right_new = wl_labels.Wl_Label_Html('', self.tables[0])

                left_new.text_raw = left_old.text_raw.copy()
                node_new.text_raw = node_old.text_raw.copy()
                right_new.text_raw = right_old.text_raw.copy()

                left_new.text_search = left_old.text_search.copy()
                node_new.text_search = node_old.text_search.copy()
                right_new.text_search = right_old.text_search.copy()

                results[i][0] = left_new
                results[i][1] = node_new
                results[i][2] = right_new

            # Sort results
            for sorting_col, sorting_order in reversed(
                    self.settings['sorting_rules']):
                reverse = 0 if sorting_order == self.tr('Ascending') else 1

                if sorting_col == self.tr('Node'):
                    results.sort(key=lambda item: item[1].text_raw,
                                 reverse=reverse)
                # Sort first by type (strings after floats), then sort numerically or alphabetically
                elif sorting_col == self.tr('Sentiment'):
                    results.sort(key=lambda item:
                                 (str(type(item[3])), item[3]),
                                 reverse=reverse)
                elif sorting_col == self.tr('Token No.'):
                    results.sort(key=lambda item: item[4], reverse=reverse)
                elif sorting_col == self.tr('File'):
                    results.sort(key=lambda item: item[10], reverse=reverse)
                else:
                    span = int(sorting_col[1:])

                    if re.search(r'^L[0-9]+$', sorting_col):
                        results.sort(key=lambda item: item[0].text_raw[-span],
                                     reverse=reverse)
                    elif re.search(r'^R[0-9]+$', sorting_col):
                        results.sort(
                            key=lambda item: item[2].text_raw[span - 1],
                            reverse=reverse)

            self.tables[0].disable_updates()

            for i, (left, node, right, sentiment, no_token, no_token_pct,
                    no_sentence, no_sentence_pct, no_para, no_para_pct,
                    file) in enumerate(results):
                # Remove empty tokens
                text_left = [token for token in left.text_raw if token]
                text_right = [token for token in right.text_raw if token]

                highlight_colors = self.main.settings_custom['concordancer'][
                    'sort_results']['highlight_colors']

                i_highlight_color_left = 1
                i_highlight_color_right = 1

                for sorting_col, _ in self.settings['sorting_rules']:
                    if re.search(self.tr(r'^L[0-9]+$'), sorting_col) and int(
                            sorting_col[1:]) <= len(text_left):
                        hightlight_color = highlight_colors[
                            i_highlight_color_left % len(highlight_colors)]

                        text_left[-int(sorting_col[1:])] = f'''
                            <span style="color: {hightlight_color}; font-weight: bold;">
                                {text_left[-int(sorting_col[1:])]}
                            </span>
                        '''

                        i_highlight_color_left += 1
                    elif re.search(self.tr(r'^R[0-9]+$'), sorting_col) and int(
                            sorting_col[1:]) - 1 < len(text_right):
                        hightlight_color = highlight_colors[
                            i_highlight_color_right % len(highlight_colors)]

                        text_right[int(sorting_col[1:]) - 1] = f'''
                            <span style="color: {hightlight_color}; font-weight: bold;">
                                {text_right[int(sorting_col[1:]) - 1]}
                            </span>
                        '''

                        i_highlight_color_right += 1

                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 0)).setText(' '.join(text_left))
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 1)).setText(node.text())
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 2)).setText(' '.join(text_right))

                self.tables[0].indexWidget(self.tables[0].model().index(
                    i,
                    0)).text_raw = [token for token in left.text_raw if token]
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 1)).text_raw = node.text_raw
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 2)).text_raw = [
                        token for token in right.text_raw if token
                    ]

                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 0)).text_search = left.text_search
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 1)).text_search = node.text_search
                self.tables[0].indexWidget(self.tables[0].model().index(
                    i, 2)).text_search = right.text_search

                if isinstance(sentiment, float):
                    self.tables[0].set_item_num(i, 3, sentiment)
                # No Support
                else:
                    self.tables[0].set_item_error(i, 3, text=sentiment)

                self.tables[0].set_item_num_val(i, 4, no_token)
                self.tables[0].set_item_num_val(i, 5, no_token_pct)
                self.tables[0].set_item_num_val(i, 6, no_sentence)
                self.tables[0].set_item_num_val(i, 7, no_sentence_pct)
                self.tables[0].set_item_num_val(i, 8, no_para)
                self.tables[0].set_item_num_val(i, 9, no_para_pct)
                self.tables[0].model().item(i, 10).setText(file)

            self.tables[0].enable_updates(emit_signals=False)

        if not self.tables[0].is_empty():
            dialog_progress = wl_dialogs_misc.Wl_Dialog_Progress(
                self.main, text=self.tr('Sorting results...'))

            worker_results_sort_concordancer = Wl_Worker_Results_Sort_Concordancer(
                self.main,
                dialog_progress=dialog_progress,
                update_gui=update_gui,
                dialog=self)

            thread_results_sort_concordancer = wl_threading.Wl_Thread(
                worker_results_sort_concordancer)
            thread_results_sort_concordancer.start_worker()

        self.main.statusBar().showMessage(
            self.tr('The results in the table has been successfully sorted.'))