Example #1
0
    class ExampleApp(QtGui.QMainWindow, main_form.Ui_MainWindow):
        def __init__(self):
            super(self.__class__, self).__init__()
            self.setupUi(
                self)  # This is defined in design.py file automatically

            # self.listWidget_Xoutput_files.addItem("deneme")

            cleaner_files_path = os.path.join(str(QDir.currentPath()),
                                              "cleaner_files")
            if not os.path.exists(cleaner_files_path):
                os.mkdir(cleaner_files_path)

            self.model = QFileSystemModel()
            self.model.setRootPath(cleaner_files_path)

            self.model.setNameFilters(QStringList(["Xoutput-n_analyses-*.txt"
                                                   ]))
            self.model.setNameFilterDisables(False)
            self.model.setFilter(QDir.Dirs | QDir.Files)

            self.treeView_Xoutput_files.setModel(self.model)

            self.treeView_Xoutput_files.setRootIndex(
                self.model.index(cleaner_files_path))

            self.treeView_Xoutput_files.setColumnWidth(0, 500)

            self.treeView_Xoutput_files.selectionModel(
            ).selectionChanged.connect(self.load_and_view_file_contents)

            self.rules_dict = {}

            self.special_button_for_level_01.setDisabled(True)

        def load_and_view_file_contents(self, current, previous):

            print current.indexes()
            model_index = current.indexes()[0]

            filename = self.model.data(model_index).toString()

            import re
            m = re.match(r"Xoutput-n_analyses-([0-9]+)", filename)
            if m:
                n_analyzes = int(m.group(1))
            else:
                n_analyzes = -1

            if n_analyzes == 1:
                self.special_button_for_level_01.setDisabled(False)
                self.special_button_for_level_01.clicked.connect(
                    self.add_all_level_01_to_rule_dict)
            else:
                self.special_button_for_level_01.setDisabled(True)

            with codecs.open(filename, "r", encoding="utf8") as f:
                lines = f.readlines()
                # print lines
                self.listWidget_selected_file_contents.clear()
                self.listWidget_selected_file_contents.addItems(
                    QStringList(lines))
                self.listWidget_selected_file_contents.selectionModel(
                ).selectionChanged.connect(
                    self.load_and_view_samples_from_train_and_dev)

            self.load_and_view_rule_file_contents(n_analyzes)

        def load_and_view_rule_file_contents(self, n_analyzes):
            # load rules file
            self.rules_dict = {}
            rules_filename = "Xoutput-n_analyses-%02d.txt.rules" % n_analyzes
            try:
                with codecs.open(rules_filename, "r") as rules_f:
                    self.output_file_load_status.setText("%s loaded." %
                                                         rules_filename)
                    self.output_file_load_status.setStyleSheet(
                        "QLabel { color : green; }")

                    rules = []

                    line = rules_f.readline().strip()
                    while line:
                        rules.append(line.split(" "))
                        self.rules_dict[int(
                            line.split(" ")[0])] = line.split(" ")
                        line = rules_f.readline().strip()

                    self.update_tableWidgetxxxx(
                        self.tableWidget_output_file_contents,
                        sorted(self.rules_dict.items(), key=lambda x: x[0]),
                        len(self.rules_dict.keys()), 1 + 1 + n_analyzes +
                        1)  # id + golden + FST analyzes + selected

            except IOError as e:
                # print "File not found"
                self.output_file_load_status.setText("File not found")
                self.output_file_load_status.setStyleSheet(
                    "QLabel { color : red; }")

                self.update_tableWidgetxxxx(
                    self.tableWidget_output_file_contents, [], 0,
                    1)  # id + golden + FST analyzes + selected

        def update_tableWidgetxxxx(self, table_widget, rules, row_count,
                                   col_count):
            table_widget.clear()
            table_widget.setColumnCount(col_count)
            table_widget.setRowCount(row_count)

            if rules:
                for row in range(table_widget.rowCount()):
                    row_items = rules[row]
                    print row_items
                    item = self.listWidget_selected_file_contents.item(
                        int(row_items[0]) - 1)  # type: QListWidgetItem
                    item.setBackgroundColor(QtGui.QColor(255, 0, 0, 127))
                    for column in range(table_widget.columnCount()):
                        if column < len(row_items[1]):
                            table_widget.setItem(
                                row, column,
                                QTableWidgetItem(
                                    row_items[1][column].decode("utf8")))

                # self.tableWidget_samples_from_train_and_dev.resizeColumnToContents()
                for column in range(table_widget.columnCount()):
                    table_widget.resizeColumnToContents(column)

        def update_corrected_morph_analysis(self, current, previous):

            model_index = current.indexes()[0]

            self.textEdit_2.setPlainText(
                self.listWidget_selected_row.model().data(
                    model_index).toString())

        def add_all_level_01_to_rule_dict(self):

            self.rules_dict = {}

            for idx in range(self.listWidget_selected_file_contents.count()):
                row_items = unicode(
                    self.listWidget_selected_file_contents.item(
                        idx).text()).strip().split(" ")

                rules_item = [
                    x.encode("utf8") for x in
                    [row_items[0], row_items[4], row_items[-1], row_items[-1]]
                ]

                self.rules_dict[int(row_items[0])] = rules_item

            self.update_tableWidgetxxxx(
                self.tableWidget_output_file_contents,
                sorted(self.rules_dict.items(), key=lambda x: x[0]),
                len(self.rules_dict.keys()),
                1 + 1 + 1 + 1)  # id + golden + FST analyzes + selected

        def add_to_the_rule_dict(self, state):

            n_analyzes, entry_id = [
                int(x) for x in self.label_8.text().split(" ")
            ]

            other_analyzes = [
                self.listWidget_selected_row.item(i)
                for i in range(self.listWidget_selected_row.count())
            ]  # type: list[QListWidgetItem]

            rules_item = [unicode(x).encode("utf8") for x in [entry_id,
                          self.textEdit_golden_morph_analysis.toPlainText()] + \
                          [x.text() for x in other_analyzes] + \
                         [self.textEdit_2.toPlainText()]]

            self.rules_dict[entry_id] = rules_item

            self.update_tableWidgetxxxx(
                self.tableWidget_output_file_contents,
                sorted(self.rules_dict.items(), key=lambda x: x[0]),
                len(self.rules_dict.keys()), 1 + 1 + n_analyzes +
                1)  # id + golden + FST analyzes + selected

        def load_and_view_samples_from_train_and_dev(self, current, previous):
            print current.indexes()

            model_index = current.indexes()[0]

            morph_analyzes = unicode(
                self.listWidget_selected_file_contents.model().data(
                    model_index).toString()).strip().split(" ")
            # print morph_analyzes
            golden_morph_analysis = morph_analyzes[4]
            target = golden_morph_analysis[1:]

            other_morph_analyzes = morph_analyzes[5:]

            n_analyzes = len(other_morph_analyzes)

            self.label_3.setText("selected row id: %d" %
                                 int(morph_analyzes[0]))
            self.label_8.setText("%d %d" %
                                 (int(n_analyzes), int(morph_analyzes[0])))

            self.listWidget_selected_row.clear()
            self.listWidget_selected_row.addItems(
                QStringList(other_morph_analyzes))

            self.textEdit_golden_morph_analysis.setPlainText(
                golden_morph_analysis)

            # self.addRuleToTheListButton.clicked.connect(
            #     partial(self.save_to_file, n_analyzes=n_analyzes, entry_id=int(morph_analyzes[0])))

            # from functools import partial
            self.addRuleToTheListButton.clicked.connect(
                self.add_to_the_rule_dict)

            if len(other_morph_analyzes) == 1:
                self.textEdit_2.setPlainText(other_morph_analyzes[0])

            self.listWidget_selected_row.selectionModel(
            ).selectionChanged.connect(self.update_corrected_morph_analysis)

            print type(target)
            print target
            print target.encode("utf8")

            # target = target.replace("?", "\?")

            lines = subprocess.check_output(
                ("grep -F -m 50 %s ./dataset/errors.gungor.ner.train_and_dev" %
                 target).split(" "),
                shell=False)

            # print lines

            lines = [x.decode("utf8") for x in lines.split("\n")]

            print type(lines[0])
            print len(lines)

            self.tableWidget_samples_from_train_and_dev.clear()

            self.tableWidget_samples_from_train_and_dev.setColumnCount(
                n_analyzes + 1)
            self.tableWidget_samples_from_train_and_dev.setRowCount(
                len(lines) - 1)

            for row in range(
                    self.tableWidget_samples_from_train_and_dev.rowCount()):
                row_items = lines[row].split(" ")[2:]
                for column in range(
                        self.tableWidget_samples_from_train_and_dev.
                        columnCount()):
                    if column < len(row_items):
                        self.tableWidget_samples_from_train_and_dev.setItem(
                            row, column, QTableWidgetItem(row_items[column]))

            # self.tableWidget_samples_from_train_and_dev.resizeColumnToContents()
            for column in range(
                    self.tableWidget_samples_from_train_and_dev.columnCount()):
                self.tableWidget_samples_from_train_and_dev.resizeColumnToContents(
                    column)

            self.sort_and_save_button.clicked.connect(self.sort_and_save)

        def sort_and_save(self):

            indexes = self.treeView_Xoutput_files.selectedIndexes()

            model_index = indexes[0]

            filename = self.model.data(model_index).toString()

            with open(filename + ".rules", "w") as f:
                for row in range(
                        self.tableWidget_output_file_contents.rowCount()):
                    row_content = []
                    for column in range(self.tableWidget_output_file_contents.
                                        columnCount()):
                        cell_content = self.tableWidget_output_file_contents.item(
                            row, column).text()  # type: QString
                        if cell_content:
                            row_content.append(
                                unicode(cell_content).encode("utf8"))
                    if row != 0:
                        f.write("\n")
                    f.write(" ".join(row_content))
Example #2
0
class AbstractPathCompleter(AbstractCompleter):
    """Base class for PathCompleter and GlobCompleter
    """
    
    # global object. Reused by all completers
    _fsModel = QFileSystemModel()

    _ERROR = 'error'
    _HEADER = 'currentDir'
    _STATUS = 'status'
    _DIRECTORY = 'directory'
    _FILE = 'file'
    
    def __init__(self, text):
        self._originalText = text
        self._dirs = []
        self._files = []
        self._error = None
        self._status = None
        
        """hlamer: my first approach is making self._model static member of class. But, sometimes it 
        returns incorrect icons. I really can't understand when and why.
        When it is private member of instance, it seems it works
        """
        self._model = None # can't construct in the construtor, must be constructed in GUI thread
    
    @staticmethod
    def _filterHidden(paths):
        """Remove hidden and ignored files from the list
        """
        return [path for path in paths \
                    if not os.path.basename(path).startswith('.') and \
                        not core.fileFilter().regExp().match(path)]

    def _classifyRowIndex(self, row):
        """Get list item type and index by it's row
        """

        if self._error:
            assert row == 0
            return (self._ERROR, 0)
        
        if row == 0:
            return (self._HEADER, 0)
        
        row -= 1
        if self._status:
            if row == 0:
                return (self._STATUS, 0)
            row -= 1
        
        if row in range(len(self._dirs)):
            return (self._DIRECTORY, row)
        row -= len(self._dirs)
        
        if row in range(len(self._files)):
            return (self._FILE, row)
        
        assert False

    def _formatHeader(self, text):
        """Format current directory for show it in the list of completions
        """
        return '<font style="background-color: %s; color: %s">%s</font>' % \
                (QApplication.instance().palette().color(QPalette.Window).name(),
                 QApplication.instance().palette().color(QPalette.WindowText).name(),
                 htmlEscape(text))

    def rowCount(self):
        """Row count in the list of completions
        """
        if self._error:
            return 1
        else:
            count = 1  # current directory
            if self._status:
                count += 1
            count += len(self._dirs)
            count += len(self._files)
            return count

    def _iconForPath(self, path):
        """Get icon for file or directory path. Uses QFileSystemModel
        """
        if self._model is None:
            self._model = QFileSystemModel()
        
        index = self._model.index(path)
        return self._model.data(index, Qt.DecorationRole)

    def text(self, row, column):
        """Item text in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return '<font color=red>%s</font>' % htmlEscape(self._error)
        elif rowType == self._HEADER:
            return self._formatHeader(self._headerText())
        elif rowType == self._STATUS:
            return '<i>%s</i>' % htmlEscape(self._status)
        elif rowType == self._DIRECTORY:
            return self._formatPath(self._dirs[index], True)
        elif rowType == self._FILE:
            return self._formatPath(self._files[index], False)

    def icon(self, row, column):
        """Item icon in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return QApplication.instance().style().standardIcon(QStyle.SP_MessageBoxCritical)
        elif rowType == self._HEADER:
            return None
        elif rowType == self._STATUS:
            return None
        elif rowType == self._DIRECTORY:
            return self._iconForPath(self._dirs[index])
        elif rowType == self._FILE:
            return self._iconForPath(self._files[index])

    def getFullText(self, row):
        """User clicked a row. Get inline completion for this row
        """
        row -= 1  # skip current directory
        path = None
        if row in range(len(self._dirs)):
            return self._dirs[row] + '/'
        else:
            row -= len(self._dirs)  # skip dirs
            if row in range(len(self._files)):
                return self._files[row]
        
        return None
Example #3
0
 def data(self, index, role):
     if role == Qt.ToolTipRole:
         return self.filePath(index)
     else:
         return QFileSystemModel.data(self, index, role)
Example #4
0
 def data(self, index, role=None):
     if role == Qt.CheckStateRole and index.column() == 0:
         return self.checkState(index)
     return QFileSystemModel.data(self, index, role)
Example #5
0
 def data(self, index, role):
     if role == Qt.ToolTipRole:
         return self.filePath(index)
     else:
         return QFileSystemModel.data(self, index, role)
Example #6
0
class AbstractPathCompleter(AbstractCompleter):
    """Base class for PathCompleter and GlobCompleter
    """

    # global object. Reused by all completers
    _fsModel = QFileSystemModel()

    _ERROR = 'error'
    _HEADER = 'currentDir'
    _STATUS = 'status'
    _DIRECTORY = 'directory'
    _FILE = 'file'

    def __init__(self, text):
        self._originalText = text
        self._dirs = []
        self._files = []
        self._error = None
        self._status = None
        """hlamer: my first approach is making self._model static member of class. But, sometimes it 
        returns incorrect icons. I really can't understand when and why.
        When it is private member of instance, it seems it works
        """
        self._model = None  # can't construct in the construtor, must be constructed in GUI thread

    @staticmethod
    def _filterHidden(paths):
        """Remove hidden and ignored files from the list
        """
        return [path for path in paths \
                    if not os.path.basename(path).startswith('.') and \
                        not core.fileFilter().regExp().match(path)]

    def _classifyRowIndex(self, row):
        """Get list item type and index by it's row
        """

        if self._error:
            assert row == 0
            return (self._ERROR, 0)

        if row == 0:
            return (self._HEADER, 0)

        row -= 1
        if self._status:
            if row == 0:
                return (self._STATUS, 0)
            row -= 1

        if row in range(len(self._dirs)):
            return (self._DIRECTORY, row)
        row -= len(self._dirs)

        if row in range(len(self._files)):
            return (self._FILE, row)

        assert False

    def _formatHeader(self, text):
        """Format current directory for show it in the list of completions
        """
        return '<font style="background-color: %s; color: %s">%s</font>' % \
                (QApplication.instance().palette().color(QPalette.Window).name(),
                 QApplication.instance().palette().color(QPalette.WindowText).name(),
                 htmlEscape(text))

    def rowCount(self):
        """Row count in the list of completions
        """
        if self._error:
            return 1
        else:
            count = 1  # current directory
            if self._status:
                count += 1
            count += len(self._dirs)
            count += len(self._files)
            return count

    def _iconForPath(self, path):
        """Get icon for file or directory path. Uses QFileSystemModel
        """
        if self._model is None:
            self._model = QFileSystemModel()

        index = self._model.index(path)
        return self._model.data(index, Qt.DecorationRole)

    def text(self, row, column):
        """Item text in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return '<font color=red>%s</font>' % htmlEscape(self._error)
        elif rowType == self._HEADER:
            return self._formatHeader(self._headerText())
        elif rowType == self._STATUS:
            return '<i>%s</i>' % htmlEscape(self._status)
        elif rowType == self._DIRECTORY:
            return self._formatPath(self._dirs[index], True)
        elif rowType == self._FILE:
            return self._formatPath(self._files[index], False)

    def icon(self, row, column):
        """Item icon in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return QApplication.instance().style().standardIcon(
                QStyle.SP_MessageBoxCritical)
        elif rowType == self._HEADER:
            return None
        elif rowType == self._STATUS:
            return None
        elif rowType == self._DIRECTORY:
            return self._iconForPath(self._dirs[index])
        elif rowType == self._FILE:
            return self._iconForPath(self._files[index])

    def getFullText(self, row):
        """User clicked a row. Get inline completion for this row
        """
        row -= 1  # skip current directory
        path = None
        if row in range(len(self._dirs)):
            return self._dirs[row] + '/'
        else:
            row -= len(self._dirs)  # skip dirs
            if row in range(len(self._files)):
                return self._files[row]

        return None