基于Python检索系统(4)最终版

from tkinter import *
import jieba
import jieba.analyse

#b1函数为 全部新闻显示页面
#b2函数为 检索页面 检索成功后,跳转另外一个页面


def b1():  #全部新闻显示页面
    #记录文件中有多少行标题 记录在count中
    count=0
    for line in open("jia.txt","r",encoding='utf-8'):
        count=count+1
        
    #新建字典title_dict,键对应(1-5294)的数字,值是对应的新闻标题
    
    title_dict={}

    f=open("jia.txt","r",encoding='utf-8')
    i=1
    #将文字的每行标题存入字典
    for line in open("jia.txt","r",encoding='utf-8'):
    #添加一个删除字符串末尾/n的操作
        title_dict[i]=line
        i=i+1
    # print(line)

    #将字典title_dict中的值分别进行分词操作
    seg_list={}
    tags={}
    #将分词存入tags字典,键为1-5294,值为对应的分词组成的列表
    for j in range(1,count+1):
        #搜索引擎模式
        seg_list[j] =jieba.cut_for_search(title_dict.get(j))
        #精确模式
        #seg_list[j]=jieba.cut(title_dict.get(j),cut_all=True)
        tags[j]=jieba.analyse.extract_tags(title_dict.get(j), topK=40)
    
    # print(tags)

    #将分词后的结果存入词项字典
    word_dict={}
    for k in range(1,count+1):
        for z in range(0,int(len(tags.get(k))-1)):   #字典中每个值中,元素的个数
            word_dict.setdefault(tags.get(k)[z])
            if(word_dict.get(tags.get(k)[z])==None):
                word_dict[tags.get(k)[z]]=[]
                word_dict[tags.get(k)[z]].append(k)
            else:
                word_dict[tags.get(k)[z]].append(k)
            
    #gui部分
    root=Tk()
    root.title("上海理工大学新闻检索")
    root.geometry('800x500+200+100')
    sb=Scrollbar(root)
    sb.pack(side=RIGHT,fill=Y)

    #创建一个空列表
    theLB=Listbox(root,width=100,height=80,yscrollcommand=sb.set)

    count=0
    for line in open("jia.txt","r",encoding='utf-8'):
        count=count+1
    
    for a in range(1,count+1):
        theLB.insert(a,title_dict.get(a))

    theLB.pack(padx=20,pady=20,fill=BOTH)
    sb.config(command=theLB.yview)
    mainloop()

def b2():
    def compare():
        count=0
        for line in open("jia.txt","r",encoding='utf-8'):
            count=count+1
        title_dict={}
        f=open("jia.txt","r",encoding='utf-8')
        i=1
        for line in open("jia.txt","r",encoding='utf-8'):
            title_dict[i]=line
            i=i+1
        seg_list={}
        tags={}
        for j in range(1,count+1):
            seg_list[j] =jieba.cut_for_search(title_dict.get(j))
            tags[j]=jieba.analyse.extract_tags(title_dict.get(j), topK=40)
        word_dict={}
        for k in range(1,count+1):
            for z in range(0,int(len(tags.get(k))-1)):   
                word_dict.setdefault(tags.get(k)[z])
                if(word_dict.get(tags.get(k)[z])==None):
                    word_dict[tags.get(k)[z]]=[]
                    word_dict[tags.get(k)[z]].append(k)
                else:
                    word_dict[tags.get(k)[z]].append(k)


        user_input=a.get()
        for i in range(1,len(word_dict.keys())):
            if user_input==list(word_dict.keys())[i-1]:
                print("标题匹配成功\n")

                root=Tk()
                root.title("上海理工大学新闻检索")
                root.geometry('800x500+200+100')
                sb=Scrollbar(root)
                sb.pack(side=RIGHT,fill=Y)
                theLB=Listbox(root,width=100,height=80,yscrollcommand=sb.set)
                
                for k in range(1,len(word_dict[list(word_dict.keys())[i-1]])):
                    theLB.insert(k,title_dict[word_dict[list(word_dict.keys())[i-1]][k]])


                theLB.pack(padx=20,pady=20,fill=BOTH)
                sb.config(command=theLB.yview)
                mainloop()
                #print(title_dict[word_dict[list(word_dict.keys())[i-1]][k]])
                #print('\n')    # word_dict[list(word_dict.keys())[i-1]][k]
            else:
                pass
        
    root=Tk()
    root.title("上海理工大学新闻检索")
    #a是文本框,用于输入要检索的文字,按下回车或者单击Button按钮可以接受用户输入
   
    a=Entry(root,width=40,validate="focusout",validatecommand=compare)
    a.pack(side=LEFT,padx=40,pady=60)


    mainloop()


root=Tk()
root.title("上海理工大学新闻")

#插入一个图片
photo = PhotoImage(file="logo.gif")
Label(root,image=photo).grid(row=0,column=0,rowspan=4,padx=15,pady=10)
#标签的属性
Label(root,text="欢迎进入本系统",font=("华康少女字体",30),fg="red").grid(row=0,column=1,columnspan=2,padx=10,pady=10)

photo1 = PhotoImage(file="a.gif")
Label(root,image=photo1).grid(row=1,column=1)
a = Button(root,text="查看所有内容",width=10,command=b1).grid(row=1,column=2,columnspan=2,padx=10,pady=5)

photo2 = PhotoImage(file="b.gif")
Label(root,image=photo2).grid(row=2,column=1)
b = Button(root,text="检索",width=10,command=b2).grid(row=2,column=2,columnspan=2,padx=10,pady=5)

photo3 = PhotoImage(file="c.gif")
Label(root,image=photo3).grid(row=3,column=1)
c = Button(root,text="退出",width=10,command=root.quit).grid(row=3,column=2,columnspan=2,padx=10,pady=5)

mainloop()




  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值