系统分析实验 Python实验代码

运行环境

pycharm

实验代码

# 导入库
import os
import shutil
import tkinter as tk
from tkinter import messagebox, scrolledtext
from tkinter.constants import END

# 文档中文分词
class Button1_Tk:
    def button1_click(self):
        global scr1, scr2, frame1

        frame1 = tk.Frame(root1, height=600, width=800)
        frame1.pack(side='top')
        button1 = tk.Button(frame1, text=" 返回 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame1.destroy)
        button2 = tk.Button(frame1, text=" 添加 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=button1_tk.wordcount_text)
        button3 = tk.Button(frame1, text=" 开始 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=button1_tk.text_get)
        button5 = tk.Button(frame1, text=" 退出 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame1.destroy)
        Label1 = tk.Label(frame1, text="待分词文本", font=('黑体', 12, 'bold'))
        Label2 = tk.Label(frame1, text="分词结果", font=('黑体', 12, 'bold'))
        scr1 = scrolledtext.ScrolledText(frame1, width=108, height=15, font=("隶书", 10), highlightthickness=1, bd=4)
        scr2 = scrolledtext.ScrolledText(frame1, width=108, height=18, font=("隶书", 10), highlightthickness=1, bd=4)
        button1.place(x=20, y=550)
        button2.place(x=700, y=0)
        button3.place(x=700, y=250)
        button5.place(x=700, y=550)
        Label1.place(x=20, y=18)
        Label2.place(x=20, y=280)
        scr1.place(x=20, y=38)
        scr2.place(x=20, y=300)
        #root1.mainloop()

    # 输入文本的信息
    def wordcount_text(self):
        global e7, e8, root5

        root5 = tk.Toplevel()
        root5.title("选择待分词文本")
        root5.geometry("400x350+300+200")
        var1 = tk.StringVar()
        e7 = tk.Entry(root5, textvariable=var1)
        e7.place(x=30, y=40, width=340, height=30)
        e7.insert(0, "")
        Label1 = tk.Label(root5, text="请输入数据库名", bd=5, font=('黑体', 12, 'bold'))
        var2 = tk.StringVar()
        e8 = tk.Entry(root5, textvariable=var2)
        e8.place(x=30, y=120, width=340, height=30)
        e8.insert(0, "")
        Label2 = tk.Label(root5, text="请输入待分词文本文件名", bd=5, font=('黑体', 12, 'bold'))
        button1 = tk.Button(root5, text="确定", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=button1_tk.text_chose)
        button2 = tk.Button(root5, text="取消", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root5.destroy)
        Label1.place(x=24, y=12)
        Label2.place(x=24, y=80)
        button1.place(x=12, y=300)
        button2.place(x=320, y=300)
        root5.mainloop()

    # 获取待分词文本
    def text_chose(self):
        scr1.delete(1.0, END)
        global count_txtSavepath, count_txtSaveflage
        database_name = e7.get()
        file_name = e8.get()
        path = path_root + database_name + "/txt_file/" + file_name + ".txt"
        count_txtSavepath = path_root + database_name + "/wordcount_file/" + file_name + ".txt"
        text_exits = os.path.exists(path)
        if not text_exits:
            messagebox.showerror('错误', '不存在该文档,请确认数据库和文件名是否正确', parent=root5)
        else:
            with open(path, "r", encoding="utf-8-sig") as f:
                text = f.read()
            scr1.insert(END, text)
            scr1.see(END)
            scr1.update()
            count_txtSaveflage = True
        root5.destroy()

    def text_get(self):
        scr2.delete(1.0, END)
        global count_txtSaveflage, count_txtSavepath
        with open("知网中文词典.txt", "r", encoding="utf-8-sig") as dic_txt:
            dic = []
            text = dic_txt.readlines()
            for line in text:
                line = line.strip()
                dic.append(line)
            dic_txt.close()
        texts = scr1.get(1.0, "end")
        scr2.insert(END, "\t".join(button1_tk.max_match_segment(texts.strip(), dic)))
        scr2.see(END)
        scr2.update()
        if count_txtSaveflage:
            fpo = open(count_txtSavepath, "w", encoding="utf-8-sig")
            fpo.write("\t".join(button1_tk.max_match_segment(texts.strip(), dic)))
            fpo.close()
    #正向最大匹配算法
    def max_match_segment(self, text, dic):
        window_size = 5
        chars = text
        words = []
        idx = 0
        i = 0
        while idx < len(text):
            matched = False
            for i in range(window_size, 0, -1):
                cand = chars[idx:idx + i]
                if cand in dic:
                    words.append(cand)
                    matched = True
                    break
            if not matched:
                i = 1
                words.append(chars[idx])
            idx += i
        return words

# 文档去停用词
class Button2_Tk:
    def button2_click(self):
        global scr3, scr4, scr5, frame2
        frame2 = tk.Frame(root1, height=600, width=800)
        frame2.pack(side='top')
        button1 = tk.Button(frame2, text="返回", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame2.destroy)
        button2 = tk.Button(frame2, text="添加", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=button2_tk.checkWord_text)
        button3 = tk.Button(frame2, text="开始", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=button2_tk.check_word)
        button5 = tk.Button(frame2, text="退出", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame2.destroy)
        Label1 = tk.Label(frame2, text="待去停用词文本", font=('黑体', 12, 'bold'))
        Label2 = tk.Label(frame2, text="结果", font=('黑体', 12, 'bold'))
        Label3 = tk.Label(frame2, text="去掉的停用词", font=('黑体', 12, 'bold'))
        scr3 = scrolledtext.ScrolledText(frame2, width=108, height=15, font=("隶书", 10))
        scr4 = scrolledtext.ScrolledText(frame2, width=56, height=18, font=("隶书", 10))
        scr5 = scrolledtext.ScrolledText(frame2, width=45, height=18, font=("隶书", 10))
        button1.place(x=20, y=550)
        button2.place(x=700, y=0)
        button3.place(x=700, y=250)
        button5.place(x=700, y=550)
        Label1.place(x=20, y=18)
        Label2.place(x=20, y=280)
        Label3.place(x=450, y=280)
        scr3.place(x=20, y=38)
        scr4.place(x=20, y=300)
        scr5.place(x=450, y=300)
        #frame2.mainloop()  #
    #打开文档
    def checkWord_text(self):
        global e9, e10, root6

        root6 = tk.Toplevel()
        root6.title("选择待去停用词文本")
        root6.geometry("400x350+300+200")
        var1 = tk.StringVar()
        e9 = tk.Entry(root6, textvariable=var1)
        e9.place(x=30, y=40, width=340, height=30)
        Label_1 = tk.Label(root6, text="请输入数据库名", bd=5, font=('黑体', 12, 'bold'))
        e9.insert(0, "")
        var2 = tk.StringVar()
        e10 = tk.Entry(root6, textvariable=var2)
        e10.place(x=30, y=120, width=340, height=30)
        Label_2 = tk.Label(root6, text="请输入待分词文本文件名", bd=5, font=('黑体', 12, 'bold'))
        e10.insert(0, "")
        button1 = tk.Button(root6, text="确定", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=button2_tk.text1_chose)
        button2 = tk.Button(root6, text="取消", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root6.destroy)
        Label_1.place(x=24, y=12)
        Label_2.place(x=24, y=80)
        button1.place(x=12, y=300)
        button2.place(x=320, y=300)
        root6.mainloop()
    def text1_chose(self):
        database_name = e9.get()
        file_name = e10.get()
        path = path_root + database_name + "/wordcount_file/" + file_name + ".txt"
        text_exits = os.path.exists(path)
        if not text_exits:
            messagebox.showerror('错误', '不存在该文档,请确认数据库和文件名是否正确', parent=root6)
        else:
            with open(path, "r", encoding="utf-8-sig") as f:
                text = f.read()
            f.close()
            scr3.insert(END, text)
            scr3.see(END)
            scr3.update()
        root6.destroy()
    #去停用词
    def check_word(self):
        check_flage = False
        scr4.delete(1.0, END)
        scr5.delete(1.0, END)
        with open("中文停用词表.txt", "r", encoding="utf-8-sig") as dic_txt:
            out_txt = []
            find_word = []
            dic = []
            text = dic_txt.readlines()
        dic_txt.close()
        for line in text:
            line = line.strip()
            dic.append(line)
        text1 = scr3.get(1.0, "end")
        text1 = text1.strip()
        if "\t" in text1:
            text1 = text1.split("\t")
            check_flage = True
        for word in text1:
            if word in dic:
                find_word.append(word)
            else:
                out_txt.append(word)
        if check_flage:
            scr4.insert(END, "\t".join(str(i) for i in out_txt))
        else:
            scr4.insert(END, "".join(str(i) for i in out_txt))
        scr5.insert(END, "\t".join(str(i) for i in find_word))
        scr4.see(END)
        scr5.see(END)
        scr4.update()
        scr5.update()

# 建立倒排文档
class Button3_Tk:
    def button3_click(self):
        global e11, scr6, frame3

        frame3 = tk.Frame(root1, height=600, width=800)
        frame3.pack(side='top')
        var = tk.StringVar()
        e11 = tk.Entry(frame3, textvariable=var)
        e11.place(x=20, y=50, width=650, height=30)
        Label = tk.Label(frame3, text='请输入数据库名,将为此数据库下的所有文本建立倒排文档', bd=5, font=('黑体', 12, 'bold'))
        e11.insert(0, "")
        scr6 = scrolledtext.ScrolledText(frame3, width=93, height=35, font=("隶书", 10))
        scr6.place(x=20, y=85)
        button1 = tk.Button(frame3, text="返回", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame3.destroy)
        button2 = tk.Button(frame3, text="确定", bd=5, font=('黑体', 12, 'bold'), command=button3_tk.daopaitext_chose)
        button5 = tk.Button(frame3, text="退出", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame3.destroy)
        Label.place(x=20, y=20)
        button1.place(x=20, y=550)
        button2.place(x=700, y=40)
        button5.place(x=700, y=550)
        #frame3.mainloop()  #

    def daopaitext_chose(self):
        scr6.delete(1.0, END)
        daopai_dic = {}
        wrodcount_filesName = []
        txt_filesName = []
        database_name = e11.get()
        path = path_root + database_name
        txt_path = path_root + database_name + "/txt_file"
        wrodcount_filePath = path_root + database_name + "/wordcount_file"
        daopai_txtPath = path_root + database_name + "/daopai_file/" + database_name + ".txt"
        for root, dirs, files in os.walk(wrodcount_filePath):
            wrodcount_filesName = files
        for root, dirs, files in os.walk(txt_path):
            txt_filesName = files
        database_exits = os.path.exists(path)
        wrodcount_filenum = len(wrodcount_filesName)
        txt_filesnum = len(txt_filesName)
        if not database_exits:
            messagebox.showerror('错误', '不存在该数据库,请确认数据库名是否正确', parent=frame3)
        elif wrodcount_filesName == []:
            messagebox.showerror('错误', '找不到分词文件来建立倒排文档,\n请先对此数据库下的文件进行文本分词', parent=frame3)
        else:
            if wrodcount_filenum < txt_filesnum:
                messagebox.showwarning('警告', '该数据库下存在文件未进行文本分词操作\n可能会影响倒排文档建立,'
                                             '导致后续查找,找不到指定文档', parent=frame3)
            for i in wrodcount_filesName:
                wrodcount_txtPath = wrodcount_filePath + "/" + i
                with open(wrodcount_txtPath, "r", encoding="utf-8-sig")as f:
                    word = f.read()
                    content = word.strip()
                    content = content.split("\t")
                f.close()
                for j in content:
                    a = j in daopai_dic
                    if a:
                        if i not in daopai_dic[j]:
                            daopai_dic[j].append(i)
                    elif not a:
                        daopai_dic[j] = [i]
                    else:
                        pass

        scr6.insert(END, "词" + "\t\t" + "含词的文档\n")
        with open(daopai_txtPath, "w", encoding='utf-8-sig') as f:
            f.close()

        for key in daopai_dic:
            values = "\t".join(str(i) for i in daopai_dic[key])
            with open(daopai_txtPath, "a") as f:
                f.writelines(key + "\t\t" + values + "\n")
                f.close()
            scr6.insert(END, key + "\t\t" + values + "\n")
            scr6.see(END)
            scr6.update()

# 多词联合查询文档链接
class Button4_Tk:
    def button4_click(self):
        global e12, e13, scr6, frame4
        frame4 = tk.Frame(root1, height=600, width=800)
        frame4.pack(side='top')
        var = tk.StringVar()
        e12 = tk.Entry(frame4, textvariable=var)
        e12.place(x=20, y=40, width=650, height=30)
        Label_1 = tk.Label(frame4, text='请输入几个词,以逗号分隔', bd=5, font=('黑体', 12, 'bold'))
        e12.insert(0, "")
        var1 = tk.StringVar()
        e13 = tk.Entry(frame4, textvariable=var1)
        e13.place(x=20, y=100, width=650, height=30)
        Label_2 = tk.Label(frame4, text='请输入数据库名', bd=5, font=('黑体', 12, 'bold'))
        e13.insert(0, "")
        scr6 = scrolledtext.ScrolledText(frame4, width=93, height=30, font=("隶书", 10))
        scr6.place(x=20, y=150)
        button1 = tk.Button(frame4, text="返回", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame4.destroy)
        button2 = tk.Button(frame4, text="查询", bd=5, font=('黑体', 12, 'bold'), command=button4_tk.txt_find)
        button5 = tk.Button(frame4, text="退出", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=frame4.destroy)
        Label_1.place(x=20, y=10)
        Label_2.place(x=20, y=70)
        button1.place(x=20, y=550)
        button2.place(x=700, y=100)
        button5.place(x=700, y=550)
        frame4.mainloop()  #

    # 多词查找
    def txt_find(self):
        scr6.delete(1.0, END)
        database_name = e13.get()
        words = e12.get()
        word_list = words.split(",")
        a = os.path.exists(path_root + database_name)
        b = "," not in words
        if (not a and b):
            messagebox.showerror('错误', '不存在该数据库,且所输入词组未用中文逗号分隔', parent=frame4)
        elif not a:
            messagebox.showerror('错误', '不存在该数据库', parent=frame4)
        elif b:
            messagebox.showwarning('警告', '所输入词组未用中文逗号分隔', parent=frame4)
        else:
            dic = {}
            path = path_root + database_name + "/daopai_file/" + database_name + ".txt"
            with open(path, "r", encoding="gbk") as f:
                lines = f.readlines()
                f.close()
            for line in lines:
                line = line.replace("\t\t", ",")
                line = line.replace("\t", ",")
                line = line.strip()
                line = line.split(",")
                key = line[0]
                values = line[1:]
                dic[key] = values
        scr6.insert(END, "字\t文件名\t\t文件路径\n")
        for m in range(len(word_list)):
            i = word_list[m]
            if i not in dic:
                messagebox.showerror('错误', '找不到文本' + i, parent=frame4)
            elif i in dic:
                list_1 = dic[i]
                count = 0
                path_len = len(list_1)
                for path in list_1:
                    flage = True
                    for j in word_list:
                        if j not in dic:
                            flage = False
                        elif path not in dic[j]:
                            flage = False
                            break
                    if flage:
                        scr6.insert(END, i +'\t' + path + "\t\t" + path_root + database_name + "/txt_file/" + path + "\n")
                    if not flage:
                        count += 1
                if count == path_len:
                    messagebox.showerror('错误', '找不到文本', parent=frame4)


#数据库文件操作
class DataBase:
    #新建数据库,root2,e1
    def database_set(self):
        global root2, e1
        root2 = tk.Toplevel()
        root2.title("数据库建立")
        root2.geometry("400x350+350+250")
        #输入框
        var = tk.StringVar()
        e1 = tk.Entry(root2, textvariable=var)
        e1.place(x=30, y=40, width=340, height=30)
        Label = tk.Label(root2, text='请输入新建数据库名字', font=('黑体', 12, 'bold'))
        e1.insert(0, "")
        #确定,取消按钮
        button1 = tk.Button(root2, text=" 确定 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=database.database_new)
        button2 = tk.Button(root2, text=" 取消 ",  bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root2.destroy)
        Label.place(x=24, y=12)
        button1.place(x=12, y=300)
        button2.place(x=320, y=300)
        root2.mainloop()
    def database_new(self):
        database_name = e1.get()
        path = path_root + database_name
        exist_name = os.path.exists(path)#判断文件夹是否存在,是返回true
        if exist_name:
            messagebox.showerror('错误', '已存在同名数据库,请更换数据库名', parent=root2)
        else:
            os.makedirs(path_root + database_name + '/txt_file')#递归创建目录
            os.makedirs(path_root + database_name + '/daopai_file')
            os.makedirs(path_root + database_name + '/wordcount_file')
            messagebox.showinfo('消息框', '新建数据库成功', parent=root2)
    #查询数据库文件
    def file_name(self, file_dir):
        database_str = []
        for root, dirs, files in os.walk(file_dir):
            database_name = dirs
            break
        for i in database_name:
            txt_filePath = path_root + i + '/txt_file'
            for root, dirs, files in os.walk(txt_filePath):
                database_str.append([i, files])
        return database_str
    def database_info(self):
        root10 = tk.Toplevel()
        root10.geometry("600x350+350+250")
        scr = scrolledtext.ScrolledText(root10, width=70, height=18, font=("隶书", 12))
        button1 = tk.Button(root10, text="返回", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root10.destroy)
        button5 = tk.Button(root10, text="退出", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root10.destroy)
        scr.place(x=20, y=10)
        button1.place(x=20, y=305)
        button5.place(x=520, y=305)
        scr.insert(END, "数据库名\t\t文件名\n")
        for i in self.file_name(path_root):
            database_name = i[0]
            txt_name = "\t".join(str(i) for i in i[1])
            scr.insert(END, database_name + "\t\t" + txt_name + "\n")
    #删除数据库
    def database_del(self):
        global e2, root3
        root3 = tk.Toplevel()
        root3.title("数据库删除")
        root3.geometry("400x350+350+250")
        var = tk.StringVar()
        e2 = tk.Entry(root3, textvariable=var)
        e2.place(x=30, y=40, width=340, height=30)
        e2.insert(0, "")
        Label = tk.Label(root3, text="请输入待删除数据库名", font=('黑体', 12, 'bold'))
        button1 = tk.Button(root3, text="确定", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=database.database_lost)
        button2 = tk.Button(root3, text="取消", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root3.destroy)
        Label.place(x=24, y=12)
        button1.place(x=12, y=300)
        button2.place(x=320, y=300)
        root3.mainloop()
    def database_lost(self):
        database_name = e2.get()
        path = path_root + database_name
        exist_name = os.path.exists(path)
        if not exist_name:
            messagebox.showerror('错误', '不存在该数据库,请确保数据库名正确', parent=root3)
        else:
            self.del_dir_all(path)
            shutil.rmtree(path)
            messagebox.showinfo('消息框', database_name + '数据库删除成功', parent=root3)
    def del_dir_all(self, path):
        x = os.listdir(path)
        for i in x:
            new_dir = os.path.join(path, i)
            if not os.path.isdir(new_dir):
                os.remove(new_dir)
                return self.del_dir_all(os.path.dirname(new_dir))
            else:
                self.del_dir_all(new_dir)
            os.rmdir(new_dir)

    # 为数据库添加文档
    def file_add(self):
        global e3, e4, root4

        root4 = tk.Toplevel()
        root4.title("数据库添加文件")
        root4.geometry("400x350+350+250")
        var1 = tk.StringVar()
        e3 = tk.Entry(root4, textvariable=var1)
        e3.place(x=30, y=40, width=340, height=30)
        e3.insert(0, "")
        Label1 = tk.Label(root4, text="请输入数据库名", font=('黑体', 12, 'bold'))
        var2 = tk.StringVar()
        e4 = tk.Entry(root4, textvariable=var2)
        e4.place(x=30, y=120, width=340, height=30)
        e4.insert(0, "")
        Label2 = tk.Label(root4, text="请输入待添加文件名", font=('黑体', 12, 'bold'))
        button1 = tk.Button(root4, text="确定", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=database.file_add_action)
        button2 = tk.Button(root4, text="取消", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root4.destroy)
        Label1.place(x=24, y=12)
        Label2.place(x=24, y=80)
        button1.place(x=12, y=300)
        button2.place(x=320, y=300)
        root4.mainloop()

    def file_add_action(self):
        database_name = e3.get()
        file_name = e4.get()
        path = path_root + database_name + "/txt_file/" + file_name + ".txt"
        exist_name = os.path.exists(path)
        exist_database = os.path.exists(path_root + database_name)
        if exist_name:
            messagebox.showerror('错误', '已存在同名文件,请更换文件名', parent=root4)
        elif not exist_database:
            messagebox.showerror('错误', '不存在该数据库', parent=root4)
        else:
            with open(path, "w") as f:
                f.close()
            messagebox.showinfo('消息框', file_name + '文件添加成功\n请对该文件进行分词后,为该数据库重新建立倒排文档', parent=root4)

    # 删除数据库中的文件
    def file_del(self):
        global e5, e6, root5

        root5 = tk.Toplevel()
        root5.title("数据库删除文件")
        root5.geometry("400x350+350+250")
        var1 = tk.StringVar()
        e5 = tk.Entry(root5, textvariable=var1)
        e5.place(x=30, y=40, width=340, height=30)
        e5.insert(0, "")
        Label1 = tk.Label(root5, text="请输入数据库名", font=('黑体', 12, 'bold'))
        var2 = tk.StringVar()
        e6 = tk.Entry(root5, textvariable=var2)
        e6.place(x=30, y=120, width=340, height=30)
        e6.insert(0, "")
        Label2 = tk.Label(root5, text="请输入待删除文件名", font=('黑体', 12, 'bold'))
        button1 = tk.Button(root5, text="确定", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=database.files_del)
        button2 = tk.Button(root5, text="取消", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root5.destroy)
        Label1.place(x=24, y=12)
        Label2.place(x=24, y=80)
        button1.place(x=12, y=300)
        button2.place(x=320, y=300)
        root5.mainloop()

    def files_del(self):
        database_name = e5.get()
        file_name = e6.get()
        path = path_root + database_name + "/txt_file/" + file_name + ".txt"
        wordcount_path = "D:/" + database_name + "/wordcount_file" + file_name + ".txt"
        exist_name = os.path.exists(path)
        exist_wordcount = os.path.exists(wordcount_path)
        if not exist_name:
            print(path)
            messagebox.showerror('错误', '不存在该文件,请确保文件名正确', parent=root5)
        else:
            os.remove(path)
            if exist_wordcount:
                os.remove(wordcount_path)
            messagebox.showinfo('消息框', file_name + '文件删除成功\n请为该数据库重新建立倒排文档', parent=root5)

    # 添加文本内容
    def file_change(self):
        global e14, e15, scr7, root9

        root9 = tk.Toplevel()
        root9.geometry("600x600+200+150")
        var = tk.StringVar()
        e14 = tk.Entry(root9, textvariable=var)
        e14.place(x=20, y=40, width=400, height=30)
        e14.insert(0, "")
        Label1 = tk.Label(root9, text="请输入数据库名", font=('黑体', 12, 'bold'))
        var1 = tk.StringVar()
        e15 = tk.Entry(root9, textvariable=var1)
        e15.place(x=20, y=110, width=400, height=30)
        e15.insert(0, "")
        Label2 = tk.Label(root9, text="请输入文件名", font=('黑体', 12, 'bold'))
        scr7 = scrolledtext.ScrolledText(root9, width=75, height=29, font=("隶书", 10))
        scr7.place(x=20, y=160)
        button1 = tk.Button(root9, text=" 返回 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root9.destroy)
        button2 = tk.Button(root9, text=" 打开 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=database.file_read)
        button3 = tk.Button(root9, text=" 保存 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=database.file_save)
        button5 = tk.Button(root9, text=" 退出 ", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root9.destroy)
        Label1.place(x=20, y=12)
        Label2.place(x=20, y=80)
        button1.place(x=20, y=550)
        button2.place(x=520, y=18)
        button3.place(x=285, y=550)
        button5.place(x=520, y=550)

    def file_read(self):
        global fileread_path
        scr7.delete(1.0, END)
        database_name = e14.get()
        file_name = e15.get()
        fileread_path = path_root + database_name + "/txt_file/" + file_name + ".txt"
        if not os.path.exists(fileread_path):
            messagebox.showerror('错误', '不存在该文件,请确保文件名正确', parent=root9)
        else:
            with open(fileread_path, "r", encoding="utf-8-sig") as f:
                text = f.read()
                f.close()
            if text == "":
                scr7.insert(END, file_name + "文件内容为空!请将文件内容写进该文本框,然后保存")
            scr7.insert(END, text)
            scr7.see(END)
            scr7.update()

    def file_save(self):
        filesave_path = fileread_path
        text = scr7.get(1.0, END)
        with open(filesave_path, "w", encoding='utf-8-sig') as f:
            f.write(text)
            f.close()
        messagebox.showinfo('提示', '保存文件成功', parent=root9)
        f.close()

# 程序说明
class Use_info:
    def help_info(self):
        root7 = tk.Toplevel()
        root7.geometry("600x500+350+250")
        helpbook = "此程序具备功能:\n1.文档中文分词\n2.文档去除停用词" \
                   "\n3.建立倒排文档\n4.多词联合查询\n5.数据库建立、查找、删除" \
                   "\n使用流程:\n1.新建数据库\n2.数据库添加文件 \n3.为数据库下所有文档进行分词\n" \
                   "4.为数据库建立倒排文档\n5.多词联合查询"
        scr = scrolledtext.ScrolledText(root7, width=67, height=27, font=("隶书", 12))
        scr.place(x=20, y=10)
        scr.insert(END, helpbook)
        button1 = tk.Button(root7, text="关闭", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root7.destroy)
        button1.place(x=520, y=450)

    def database_structure(self):
        global img
        root8 = tk.Toplevel()
        root8.geometry("600x600+350+250")
        button1 = tk.Button(root8, text="关闭", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root8.destroy)
        my_text = tk.Text(root8, width=80, height=35)
        my_text.pack(padx=10, pady=10)
        img = tk.PhotoImage(file="database.gif")
        my_text.image_create(END, image=img)
        button1.pack()

#主函数
if __name__ == '__main__':
    path_root = 'D:/数据库/'
    root1 = tk.Tk()
    root1.title("系统分析实验")
    root1.geometry("800x600+200+100")
    # 实例化DataBase()类
    database = DataBase()
    use_info = Use_info()
    button1_tk = Button1_Tk()
    button2_tk = Button2_Tk()
    button3_tk = Button3_Tk()
    button4_tk = Button4_Tk()
    #创建菜单栏
    menubar = tk.Menu(root1)
    filemenu1 = tk.Menu(menubar, tearoff=False)
    filemenu2 = tk.Menu(menubar, tearoff=False)
    filemenu1.add_command(label="新建数据库", font=('黑体', 10), command=database.database_set)
    filemenu1.add_command(label="查看数据库", font=('黑体', 10), command=database.database_info)
    filemenu1.add_command(label="删除数据库", font=('黑体', 10), command=database.database_del)
    filemenu1.add_command(label="数据库添加文件", font=('黑体', 10), command=database.file_add)
    filemenu1.add_command(label="数据库删除文件", font=('黑体', 10), command=database.file_del)
    filemenu1.add_command(label="数据库文件内容修改", font=('黑体', 10), command=database.file_change)
    filemenu2.add_command(label="功能介绍", font=('黑体', 10), command=use_info.help_info)
    filemenu2.add_command(label="数据库结构", font=('黑体', 10), command=use_info.database_structure)
    menubar.add_cascade(label="文件", menu=filemenu1)
    menubar.add_cascade(label="帮助", menu=filemenu2)
    button1 = tk.Button(root1, text="文档中文分词", activebackground='sky blue', bd=5, font=("隶书", 15),
                        command=button1_tk.button1_click, bg="light grey")
    button2 = tk.Button(root1, text="文档去停用词", activebackground='sky blue', bd=5, font=("隶书", 15),
                        command=button2_tk.button2_click, bg="light grey")
    button3 = tk.Button(root1, text="建立倒排文档", activebackground='sky blue', bd=5, font=("隶书", 15),
                        command=button3_tk.button3_click, bg="light grey")
    button4 = tk.Button(root1, text="多词联合查询", activebackground='sky blue', bd=5, font=("隶书", 15),
                        command=button4_tk.button4_click, bg="light grey")
    button5 = tk.Button(root1, text="退出", bd=5, font=('黑体', 12, 'bold'), bg="light grey", command=root1.destroy)
    button1.place(x=130, y=180)
    button2.place(x=330, y=180)
    button3.place(x=130, y=280)
    button4.place(x=330, y=280)
    button5.place(x=550, y=550)
    root1.configure(menu=menubar)

    root1.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值