Esempio n. 1
0
    def loadFile(self):
        # try:
        fileName = self.lineEdit.text()
        if fileName:  #判断路径非空
            f = QFile(
                fileName
            )  #创建文件对象,不创建文件对象也不报错 也可以读文件和写文件                                                                                       #open()会自动返回一个文件对象
            f = open(fileName, "r")  #打开路径所对应的文件, "r"以只读的方式 也是默认的方式
            with f:
                data = f.read()
                self.scene = QtWidgets.QGraphicsScene()  # 创建一个图形管理场景
                self.graphicsView.setScene(self.scene)  # 为graphicsView设置图形管理器
                item = QtWidgets.QGraphicsTextItem()  # 创建文本对象
                item.setPlainText(data)  # 为文本对象添加文本
                item.setPos(50, 50)
                self.scene.addItem(item)  # 添加对象
            f.close()
            count = len(open(fileName, 'r').readlines())  # 读取边的数目即文本的行数
            G = nx.read_edgelist(fileName,
                                 create_using=nx.DiGraph(),
                                 nodetype=None,
                                 data=[('weight', int)])  # 读取图
            # print(len(G.nodes))
            # print(len(G.edges))
            self.lineEdit_2.setText(str(len(G.nodes)))  # 显示结点数
            self.lineEdit_3.setText(str(len(G.edges)))  # 显示边数

        self.loadFlag = True
Esempio n. 2
0
    def _copy_file(self, uid: str = "", source_path: str = "", dest_path: str = ""):
        self._current_uid = uid

        source_file = QFile(source_path)
        dest_file = QFile(dest_path)

        if not source_file.open(QFile.ReadOnly):
            self.copy_error.emit(uid, FileCopier.CannotOpenSourceFile)
            return

        dest_file_info = QFileInfo(dest_file)
        dest_dir = dest_file_info.absoluteDir()
        if not dest_dir.exists():
            if not dest_dir.mkpath(dest_dir.absolutePath()):
                self.copy_error.emit(uid, FileCopier.CannotCreateDestinationDirectory)
                return

        if not dest_file.open(QFile.WriteOnly):
            ic(dest_path, dest_file.errorString())
            self.copy_error.emit(uid, FileCopier.CannotOpenDestinationFile)
            return

        progress: int = 0
        total: int = source_file.size()
        error: int = FileCopier.NoError
        while True:
            if self._cancel_current:
                self._cancel_current = False
                self.copy_cancelled.emit(uid)
                break

            data: Union[bytes, int] = source_file.read(_COPY_BLOCK_SIZE)
            if isinstance(data, int):
                assert data == -1
                error = FileCopier.CannotReadSourceFile
                break

            data_len = len(data)
            if data_len == 0:
                self.copy_progress.emit(uid, progress, total)
                break

            if data_len != dest_file.write(data):
                error = FileCopier.CannotWriteDestinationFile
                break

            progress += data_len
            self.copy_progress.emit(uid, progress, total)

            qApp.processEvents()

        source_file.close()
        dest_file.close()
        if error != FileCopier.NoError:
            dest_file.remove()
            self.copy_error.emit(uid, error)
        else:
            dest_file.setPermissions(source_file.permissions())
            self.copy_complete.emit(uid)
Esempio n. 3
0
 def play(self,i):
     source = self.openPath + self.fileList[i]
     self.playObj.setMedia(QMediaContent(QUrl.fromLocalFile(source)))
     #解析文件中的ID3V2
     self.currentImg = ""
     f = QFile(source)
     if f.open(QIODevice.ReadOnly):
         #读取标签
         headData = f.read(10)
         data = headData[:0]
         if self.id3v2(headData):#检测是否有ID3
             #标签的大小计算
             tag = headData[6:10]
             tagSize = (tag[0]&0x7f)*0x200000+(tag[1]&0x7f)*0x4000+(tag[2]&0x7f)*0x80+(tag[3]&0x7f)
             data =f.read(tagSize)
             while len(data)>10:
                 data = self.resolve(data)
     f.close()
     self.playObj.play()
Esempio n. 4
0
 def supportsFile(self, fileName):
     # Check the file extension first.
     if (QFileInfo(fileName).suffix() != "bin"):
         return False
     # Since we may have lots of Android-related *.bin files that aren't
     # maps, check our signature byte, too.
     f = QFile(fileName)
     if (not f.open(QIODevice.ReadOnly)):
         return False
     read = 1
     signature = f.read(1)
     return (read == 1 or signature == 96)
Esempio n. 5
0
 def supportsFile(self, fileName):
     # Check the file extension first.
     if (QFileInfo(fileName).suffix() != "bin"):
         return False
     # Since we may have lots of Android-related *.bin files that aren't
     # maps, check our signature byte, too.
     f = QFile(fileName)
     if (not f.open(QIODevice.ReadOnly)):
         return False
     read = 1
     signature = f.read(1)
     return (read == 1 or signature == 96)
Esempio n. 6
0
 def readFile(self):
     fname = QFileDialog.getOpenFileName(
         self, "Open File", "./", "All Files(*);;Wav(*.wav);;Txt (*.txt)")
     # 该方法返回一个tuple,里面有两个内容,第一个是路径, 第二个是要打开文件的类型,所以用两个变量去接受
     # 如果用户主动关闭文件对话框,则返回值为空
     if fname[0]:  # 判断路径非空
         f = QFile(fname[0])  # 创建文件对象,不创建文件对象也不报错 也可以读文件和写文件
         # open()会自动返回一个文件对象
         f = open(fname[0], "r")  # 打开路径所对应的文件, "r"以只读的方式 也是默认的方式
         with f:
             data = f.read()
             print(data)
             #self.textEdit.setText(data)
         f.close()
 def readFile(self):
     self.file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                              'DiscordURL.txt')
     print(self.file)
     fname = (self.file, 'Txt (*.txt)')
     if fname[0]:
         f = QFile(fname[0])
         try:
             f = open(fname[0], "r")
         except:
             f = open(fname[0], "w")
             f = open(fname[0], "r")
         with f:
             data = f.read()
             self.ui.textEdit.setText(data)
             f.close()
Esempio n. 8
0
class TcpS(QDialog, Ui_TcpServer):
    """
    文件传输服务器
    """

    sendFileName = pyqtSignal(str)

    def __init__(self, parent=None):
        """
        一些初始设置
        """
        super(TcpS, self).__init__(parent)
        self.setupUi(self)
        self.payloadSize = 64 * 1024
        # 读取数据64KB

        self.totalBytes = 0
        # 总大小

        self.bytesWritten = 0
        # 保存的数据

        self.bytesToWrite = 0
        # 每次减少连接写的数据量大小

        self.theFileName = ""
        # 文件名(不含路径)

        self.fileName = ""
        # 文件全名

        self.localFile = QFile()
        self.outBlock = QByteArray()
        # QByteArray()的对象,即字节数组

        self.time = QTime()
        self.initServer()

    def initServer(self):
        """
        网络设置初始化
        """
        self.tcpPort = 7788
        # 指定了TCP端口为7788

        self.tcpServer = QTcpServer(self)
        self.clientConnection = QTcpSocket(self)
        # 创建一个Tcp服务器和一个Tcp套接字

        self.tcpServer.newConnection.connect(self.sendMessage)
        # 当有新的连接来的时候发出newConnection信号,我们连接到sendMessage()函数。

        self.serverStatuslabel.setText("请选择要传送的文件")
        self.progressBar.reset()
        self.serverOpenBtn.setEnabled(True)
        self.serverSendBtn.setEnabled(False)
        self.tcpServer.close()
        # 显示我们开始创建的对话框,打开按钮是可用的,发送按钮是不可用的,进度条复位,先关闭服务器。

    def refused(self):
        """
        对端拒绝接收文件,主程序会调用服务器的refused()函数,关闭服务器。
        """
        self.tcpServer.close()
        self.serverStatuslabel.setText("对方拒绝接收")

    def closeEvent(self, event):
        """
        关闭事件
        """
        self.on_serverCloseBtn_clicked()
        # 产生关闭事件,直接调用关闭窗口按钮函数。

    def sendMessage(self):
        """
        发送文件
        """
        self.serverSendBtn.setEnabled(False)
        # 发送按钮不可用

        self.clientConnection = self.tcpServer.nextPendingConnection()
        # self.clientConnection作为连接的QTcpSocket对象返回下一个挂起的连接。

        self.clientConnection.bytesWritten.connect(self.updateClientProgress)
        # 当连接中每次将数据有效载荷写入设备的当前写通道时,都会发出此信号。在此有效负载中写入的数据量为字节数。

        self.serverStatuslabel.setText("开始传送文件 {} !".format(self.theFileName))

        self.localFile = QFile(self.fileName)
        if not (self.localFile.open(QFile.ReadOnly)):
            errorMsg = "无法读取文件 {}:\n {}".format(self.fileName,
                                                self.localFile.errorString())
            QMessageBox.warning(self, "应用程序", errorMsg)
            return
        # 尝试打开文件,要是存在问题就报错。

        self.serverCloseBtn.setText("取消")

        self.totalBytes = self.localFile.size()
        # 记录一下需要传输的文件大小。单位:字节

        sendOut = QDataStream(self.outBlock, QIODevice.WriteOnly)
        # 这里的self.outBlock是QByteArray()的对象,即字节数组;QIODevice的模式为WriteOnly

        sendOut.setVersion(QDataStream.Qt_5_4)
        # 设定QDataStream的版本为Qt_5_4

        self.time.start()
        # 开始计时

        currentFile = self.fileName.split("/")[-1]
        # 传输的文件名

        sendOut.writeInt64(0)
        sendOut.writeInt64(0)
        sendOut.writeQString(currentFile)
        self.totalBytes += self.outBlock.size()
        # 在sendOut中写入文件名以及文件名和文件的大小,大小都是以字节为单位的。

        sendOut.device().seek(0)
        sendOut.writeInt64(self.totalBytes)
        sendOut.writeInt64(self.outBlock.size() - 2)
        # QIODevice读写位置移动到0。然后分别写入总的大小和文件名大小。

        self.bytesToWrite = self.totalBytes - self.clientConnection.write(
            self.outBlock)
        # 待传输文件的大小。

        self.outBlock.resize(0)
        # outBlock清零。

    def updateClientProgress(self, numBytes):
        """
        发送进度显示
        """
        qApp.processEvents()
        # 长时间工作用,以免窗口假死

        self.bytesWritten += numBytes
        if self.bytesWritten > 0:
            self.block = self.localFile.read(
                min(self.bytesToWrite, self.payloadSize))
            self.bytesToWrite -= self.clientConnection.write(self.block)
        else:
            self.localFile.close()
        # 当我们待写入的字节数大于0时,我们每次读取的数据都是小于等于self.payloadSize的,这个self.payloadSize我们定义是64KB。
        # self.bytesToWrite每次减少连接写的数据量大小。
        # 要是待写入的字节数小于等于0,则关闭文件。

        byteSent = self.bytesWritten / (1024 * 1024)
        # 已经写了多少文件
        useTime = self.time.elapsed() / 1000
        # 传输用了多长时间
        speed = self.bytesWritten / useTime / (1024 * 1024)
        # 传输速度
        total = self.totalBytes / (1024 * 1024)
        # 总大小
        left = (total - byteSent) / speed
        # 表示剩余时间

        if byteSent < 0.01:
            byteSent = self.bytesWritten / 1024
            speed = self.bytesWritten / useTime / 1024
            total = self.totalBytes / 1024
            if left > 0:
                sendInfo = "已发送 {0:.2f}KB({1:.2f}KB/s)\n共{2:.2f}KB 已用时:{3:.1f}秒\n 估计剩余时间:{4:.1f}秒".format(
                    byteSent, speed, total, useTime, left)
            else:
                sendInfo = "已发送 {0:.2f}KB({1:.2f}KB/s)\n共{2:.2f}KB 用时:{3:.1f}秒\n".format(
                    byteSent, speed, total, useTime)
        else:
            if left > 0:
                sendInfo = "已发送 {0:.2f}MB({1:.2f}MB/s)\n共{2:.2f}MB 已用时:{3:.1f}秒\n 估计剩余时间:{4:.1f}秒".format(
                    byteSent, speed, total, useTime, left)
            else:
                sendInfo = "已发送 {0:.2f}MB({1:.2f}MB/s)\n共{2:.2f}MB 用时:{3:.1f}秒\n".format(
                    byteSent, speed, total, useTime)

        self.progressBar.setMaximum(total)
        self.progressBar.setValue(byteSent)

        if self.bytesWritten == self.totalBytes:
            self.serverCloseBtn.setText("关闭")
        # 进度条显示的方式,以及当传输的字节数等于总的字节数的时候,按钮就显示关闭。

        self.serverStatuslabel.setText(sendInfo)

    @pyqtSlot()
    def on_serverOpenBtn_clicked(self):
        """
        打开文件准备发送
        """
        self.fileName = QFileDialog.getOpenFileName(self, '打开文件', './')[0]
        if self.fileName:
            self.theFileName = self.fileName.split("/")[-1]
            self.serverStatuslabel.setText("要传送的文件为:{}".format(
                self.theFileName))
            self.serverSendBtn.setEnabled(True)
            self.serverOpenBtn.setEnabled(False)

    @pyqtSlot()
    def on_serverSendBtn_clicked(self):
        """
        发送文件,等待接收
        """
        if not (self.tcpServer.listen(QHostAddress.Any, self.tcpPort)):
            errorMsg = self.tcpServer.errorString()
            QMessageBox.warning(self, "错误", "发送失败:\n {}".format(errorMsg))
            self.TcpServer.close()
            return

        self.serverStatuslabel.setText("等待对方接收... ...")
        self.serverSendBtn.setEnabled(False)
        self.sendFileName.emit(self.theFileName)

    @pyqtSlot()
    def on_serverCloseBtn_clicked(self):
        """
        取消或者关闭
        """
        if self.tcpServer.isListening():
            self.tcpServer.close()
            if self.localFile.isOpen():
                self.localFile.close()
            self.clientConnection.abort()

        if self.serverCloseBtn.text() == "取消":
            self.serverCloseBtn.setText("关闭")
        else:
            self.close()
            self.serverOpenBtn.setEnabled(True)
            self.serverSendBtn.setEnabled(False)
            self.progressBar.reset()
            self.totalBytes = 0
            self.bytesWritten = 0
            self.bytesToWrite = 0
            self.serverStatuslabel.setText("请选择要传送的文件")
Esempio n. 9
0
    def _perform_migrations(self):
        qry = self.getQuery()
        d = self.getDatabase()

        # check if we can query a version of
        # the schema_changes table, if not possible
        # we likely need to create a completly new db
        if qry.exec(
                "SELECT version FROM schema_changes ORDER BY version DESC LIMIT 1"
        ) and qry.next():
            db_version = int(qry.value(0))
            print(f"Found Database Version to be {db_version}")
        else:
            db_version = -1
            print(
                "Could not determine database version, will create new schema from scratch..."
            )

        # check to see if we can find any migration files
        # we will prefer the ones in sql/*.sql on disk over the ones
        # bundled with qrc for easier development, but the release version
        # will likely use the ones bundled with the applications ressources
        files = glob.glob("sql/*.sql")
        if len(files) == 0:
            # could not find any files on disk in sql subdir
            if QDir(":/sql").exists():
                dir_ = QDir(":/sql")
                dir_.setNameFilters(["*.sql"])
                # in QDir.entryList files will be stripped of path
                # and we also need to append :
                files = [
                    ":/sql/" + x for x in dir_.entryList(filters=QDir.Files)
                ]

        # if the number of files is still zero we could not find any migrations
        # this would be a bug and we can terminate
        if len(files) == 0:
            print(
                "Could not find any Schema Files in sql/*.sql - Please reinstall application."
            )
            print("Exiting now ...")
            sys.exit(1)
        else:
            # next we sort the files in the correct order
            files = sorted(files,
                           key=lambda x: float(re.findall(r"(\d+)", x)[0]))
            # next up, we check the highest migration file version number
            # this should be the last list entry
            # if thats higher than db version we migrate
            # otherwise we return early doing nothing
            highvers = self._get_migrationfile_version(
                os.path.basename(files[-1]))
            if highvers <= db_version:
                print(
                    f"Found highest Version of migration files is {highvers}")
                print("Nothing needs to be migrated.")
                return

            print("Performing outstanding Database migrations...")
            qry.exec(
                "SELECT version, apply_date FROM schema_changes ORDER BY version ASC"
            )
            all_migrations = dict()
            while qry.next():
                all_migrations[qry.value(0)] = qry.value(1)

            d.transaction()
            for file in files:
                scriptname: str = os.path.basename(file)
                file_version = self._get_migrationfile_version(scriptname)

                # if the database version is already higher then the version in the filename
                # we may skip this sql file
                if db_version >= file_version:
                    print(
                        f"Skipping {scriptname}, because migration {file_version} was already applied on {all_migrations.get(file_version, 'NULL')}"
                    )
                else:
                    # otherwise we will execute the sql code and apply the migration
                    try:
                        if file.startswith(":"):
                            fd = QFile(file)
                            fd.open(QFile.ReadOnly | QFile.Text)
                            sql = QTextStream(fd).readAll()
                        else:
                            with open(file, 'rt', encoding='utf-8') as fd:
                                sql = fd.read()
                    except OSError:
                        print(f"Could not open file for reading: {file}")
                        sys.exit(1)
                    finally:
                        if file.startswith(":"):
                            fd.close()

                    # We will have to use sqlparser to split our migration files
                    # into atomic statements since the sqlite qt driver does not
                    # work with multiple stmts in one exec call and offers itself
                    # no alternative like the sqlite3.executescript() that comes
                    # with python3... :(
                    for stmt in sqlparse.split(sql):
                        if not qry.exec(stmt):
                            print(f"Applying {scriptname} to schema failed")
                            print(
                                f"The error appeared with the following statement:"
                            )
                            print(stmt)
                            print(qry.lastError().text())
                            d.rollback()
                            sys.exit(1)

                    if not qry.exec(f"""
                            INSERT INTO schema_changes 
                                (version, scriptname, apply_date)
                            VALUES
                                ({file_version}, '{scriptname}', DATETIME('now'))
                            """):
                        print(qry.lastError().text())
                        d.rollback()
                        sys.exit(1)
                    else:
                        print(f"Successfully applied {scriptname} to schema")

            # if we come this far we've applied all outstanding migrations
            # and can commit all changes to disk
            d.commit()
            print(f"All outstanding db migrations were applied")
            print(f"Database schema is now at version: {file_version}")
Esempio n. 10
0
class TcpS(QDialog, Ui_TcpServer):
    """
    Class documentation goes here.
    """

    sendFileName = pyqtSignal(str)

    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(TcpS, self).__init__(parent)
        self.setupUi(self)
        self.payloadSize = 64 * 1024
        self.totalBytes = 0
        self.bytesWritten = 0
        self.bytesToWrite = 0
        self.theFileName = ""
        self.fileName = ""
        self.localFile = QFile()
        self.outBlock = QByteArray()
        self.time = QTime()
        self.initServer()

    def initServer(self):
        """
        网络设置初始化
        """
        self.tcpPort = 7788
        self.tcpServer = QTcpServer(self)
        self.clientConnection = QTcpSocket(self)
        self.tcpServer.newConnection.connect(self.sendMessage)
        self.serverStatuslabel.setText("请选择要传送的文件")
        self.progressBar.reset()
        self.serverOpenBtn.setEnabled(True)
        self.serverSendBtn.setEnabled(False)
        self.tcpServer.close()

    def refused(self):
        """
        对端拒绝接收文件
        """
        self.tcpServer.close()
        self.serverStatuslabel.setText("对方拒绝接收")

    def closeEvent(self, event):
        """
        关闭事件
        """
        self.on_serverCloseBtn_clicked()

    def sendMessage(self):
        """
        发送文件
        """
        self.serverSendBtn.setEnabled(False)
        self.clientConnection = self.tcpServer.nextPendingConnection()
        self.clientConnection.bytesWritten.connect(self.updateClientProgress)
        self.serverStatuslabel.setText("开始传送文件 {} !".format(self.theFileName))

        self.localFile = QFile(self.fileName)
        if not (self.localFile.open(QFile.ReadOnly)):
            errorMsg = "无法读取文件 {}:\n {}".format(self.fileName,
                                                self.localFile.errorString())
            QMessageBox.warning(self, "应用程序", errorMsg)
            return

        self.serverCloseBtn.setText("取消")

        self.totalBytes = self.localFile.size()  #单位:字节
        sendOut = QDataStream(self.outBlock, QIODevice.WriteOnly)
        sendOut.setVersion(QDataStream.Qt_5_4)
        self.time.start()
        currentFile = self.fileName.split("/")[-1]
        sendOut.writeInt64(0)
        sendOut.writeInt64(0)
        sendOut.writeQString(currentFile)
        self.totalBytes += self.outBlock.size()
        sendOut.device().seek(0)
        sendOut.writeInt64(self.totalBytes)
        sendOut.writeInt64(self.outBlock.size() - 2)
        self.bytesToWrite = self.totalBytes - self.clientConnection.write(
            self.outBlock)
        self.outBlock.resize(0)

    def updateClientProgress(self, numBytes):
        """
        发送进度显示
        """
        qApp.processEvents()
        self.bytesWritten += numBytes
        if self.bytesWritten > 0:
            self.block = self.localFile.read(
                min(self.bytesToWrite, self.payloadSize))
            self.bytesToWrite -= self.clientConnection.write(self.block)
        else:
            self.localFile.close()

        byteSent = self.bytesWritten / (1024 * 1024)
        useTime = self.time.elapsed() / 1000
        speed = self.bytesWritten / useTime / (1024 * 1024)
        total = self.totalBytes / (1024 * 1024)
        left = (total - byteSent) / speed

        if byteSent < 0.01:
            byteSent = self.bytesWritten / 1024
            speed = self.bytesWritten / useTime / 1024
            total = self.totalBytes / 1024
            if left > 0:
                sendInfo = "已发送 {0:.2f}KB({1:.2f}KB/s)\n共{2:.2f}KB 已用时:{3:.1f}秒\n 估计剩余时间:{4:.1f}秒".format(
                    byteSent, speed, total, useTime, left)
            else:
                sendInfo = "已发送 {0:.2f}KB({1:.2f}KB/s)\n共{2:.2f}KB 用时:{3:.1f}秒\n".format(
                    byteSent, speed, total, useTime)
        else:
            if left > 0:
                sendInfo = "已发送 {0:.2f}MB({1:.2f}MB/s)\n共{2:.2f}MB 已用时:{3:.1f}秒\n 估计剩余时间:{4:.1f}秒".format(
                    byteSent, speed, total, useTime, left)
            else:
                sendInfo = "已发送 {0:.2f}MB({1:.2f}MB/s)\n共{2:.2f}MB 用时:{3:.1f}秒\n".format(
                    byteSent, speed, total, useTime)

        self.progressBar.setMaximum(total)
        self.progressBar.setValue(byteSent)

        if self.bytesWritten == self.totalBytes:
            self.serverCloseBtn.setText("关闭")

        self.serverStatuslabel.setText(sendInfo)

    @pyqtSlot()
    def on_serverOpenBtn_clicked(self):
        """
        打开文件
        """
        self.fileName = QFileDialog.getOpenFileName(self, '打开文件', './')[0]
        if self.fileName:
            self.theFileName = self.fileName.split("/")[-1]
            self.serverStatuslabel.setText("要传送的文件为:{}".format(
                self.theFileName))
            self.serverSendBtn.setEnabled(True)
            self.serverOpenBtn.setEnabled(False)

    @pyqtSlot()
    def on_serverSendBtn_clicked(self):
        """
        发送文件
        """
        if not (self.tcpServer.listen(QHostAddress.Any, self.tcpPort)):
            errorMsg = self.tcpServer.errorString()
            QMessageBox.warning(self, "错误", "发送失败:\n {}".format(errorMsg))
            self.TcpServer.close()
            return

        self.serverStatuslabel.setText("等待对方接收... ...")
        self.serverSendBtn.setEnabled(False)
        self.sendFileName.emit(self.theFileName)

    @pyqtSlot()
    def on_serverCloseBtn_clicked(self):
        """
        取消或者关闭
        """
        if self.tcpServer.isListening():
            self.tcpServer.close()
            if self.localFile.isOpen():
                self.localFile.close()
            self.clientConnection.abort()

        if self.serverCloseBtn.text() == "取消":
            self.serverCloseBtn.setText("关闭")
        else:
            self.close()
            self.serverOpenBtn.setEnabled(True)
            self.serverSendBtn.setEnabled(False)
            self.progressBar.reset()
            self.totalBytes = 0
            self.bytesWritten = 0
            self.bytesToWrite = 0
            self.serverStatuslabel.setText("请选择要传送的文件")
Esempio n. 11
0
    def open(self):
        global sum, pos, neg
        sum = pos = neg = 0
        if self.sender() == self.open_txt:
            file = QFileDialog.getOpenFileName(self, "打开文件", "./",
                                               "txt文件(*.txt)")

            if file[0]:
                f = QFile(file[0])
                f = open(file[0], "r", encoding='utf-8')
                with f:
                    data = f.read()
                    text_lists = data.split('\n')

                f.close()
            self.OutputBox.append("---------txt文件内容----------")

            def predict_sentiment(text):
                global sum, pos, neg
                print(text)
                # 除去除中文外所有字符
                text = re.sub("[^\u4E00-\u9FA5]", "", text)
                self.OutputBox.append('数据清洗后:' + text)
                # 分词
                cut = jieba.cut(text)
                cut_list = [i for i in cut]
                # tokenize
                for i, word in enumerate(cut_list):
                    try:
                        cut_list[i] = cn_model.vocab[word].index
                    except KeyError:
                        cut_list[i] = 0
                # padding
                tokens_pad = pad_sequences([cut_list],
                                           maxlen=max_tokens,
                                           padding='pre',
                                           truncating='pre')
                # 预测
                result = model.predict(x=tokens_pad)
                coef = result[0][0]
                sum += coef
                if coef >= 0.5:
                    pos += 1
                else:
                    neg += 1

            for text in text_lists:
                self.OutputBox.append('-' + text)
                predict_sentiment(text)
            self.sum_show.setText(str(len(text_lists)))
            self.pos_show.setText(str(pos))
            self.neg_show.setText(str(neg))
            # self.aver_show.setText(str("%.1f"%(sum/(pos+neg))))
            self.aver_show.setText('%d' % (sum / (pos + neg) * 100) + '/100')

        elif self.sender() == self.open_csv:
            file = QFileDialog.getOpenFileName(self, "打开文件", "./",
                                               "Csv files(*.csv)")

            if file[0]:
                f = QFile(file[0])
                f = open(file[0], "r", encoding='utf-8')
                with f:
                    data = f.read()
                    text_lists = data.split('\n')

                f.close()
            self.OutputBox.append("---------CSV文件内容----------")

            def predict_sentiment(text):
                global sum, pos, neg
                print(text)
                # 除去除中文外所有字符
                text = re.sub("[^\u4E00-\u9FA5]", "", text)
                self.OutputBox.append('数据清洗后:' + text)
                # 分词
                cut = jieba.cut(text)
                cut_list = [i for i in cut]
                # tokenize
                for i, word in enumerate(cut_list):
                    try:
                        cut_list[i] = cn_model.vocab[word].index
                    except KeyError:
                        cut_list[i] = 0
                # padding
                tokens_pad = pad_sequences([cut_list],
                                           maxlen=max_tokens,
                                           padding='pre',
                                           truncating='pre')
                # 预测
                result = model.predict(x=tokens_pad)
                coef = result[0][0]
                sum += coef
                if coef >= 0.5:
                    pos += 1
                else:
                    neg += 1

            for text in text_lists:
                self.OutputBox.append(text)
                predict_sentiment(text)

            self.sum_show.setText(str(len(text_lists)))
            self.pos_show.setText(str(pos))
            self.neg_show.setText(str(neg))
            self.aver_show.setText('%d' % (sum / (pos + neg) * 100) + '/100')
        elif self.sender() == self.open_sql:
            file = QFileDialog.getOpenFileName(self, "打开文件", "./",
                                               "SQL files(*.sql)")
        else:
            file = QFileDialog.getOpenFileName(self, "打开文件", "./",
                                               "JSON files(*.json)")

            if file[0]:
                f = QFile(file[0])
                f = open(file[0], "r", encoding='utf-8')
                with f:
                    data = json.loads(f.read())
                    text_lists = np.array(data['comment'])

                f.close()
            self.OutputBox.append("---------JSON文件内容----------")

            def predict_sentiment(text):
                global sum, pos, neg

                print(text)

                # 除去除中文外所有字符
                text = re.sub("[^\u4E00-\u9FA5]", "", text)
                self.OutputBox.append('数据清洗后:' + text)
                # 分词
                cut = jieba.cut(text)
                cut_list = [i for i in cut]
                # tokenize
                for i, word in enumerate(cut_list):
                    try:
                        cut_list[i] = cn_model.vocab[word].index
                    except KeyError:
                        cut_list[i] = 0
                # padding
                tokens_pad = pad_sequences([cut_list],
                                           maxlen=max_tokens,
                                           padding='pre',
                                           truncating='pre')
                # 预测
                result = model.predict(x=tokens_pad)
                coef = result[0][0]
                sum += coef
                if coef >= 0.5:
                    pos += 1
                else:
                    neg += 1

            for index in range(len(text_lists)):
                self.OutputBox.append(str(text_lists[index]))
                predict_sentiment(str(text_lists[index]))

            self.sum_show.setText(str(len(text_lists)))
            self.pos_show.setText(str(pos))
            self.neg_show.setText(str(neg))
            self.aver_show.setText('%d' % (sum / (pos + neg) * 100) + '/100')