python TK庫 統計word文件單詞詞頻程式 UI選擇文件

恰合發表於2020-12-27

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

import tkinter as tk
import docx
import webbrowser
import winreg
import win32ui
import tkinter.messagebox
from tkinter import *
from tkinter import filedialog


filePath = ''


def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                         r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')  # 利用系統的連結串列
    # winreg.QueryValueEx(key, "Desktop")[0] #返回的是Unicode型別資料
    Desktop_path = str(winreg.QueryValueEx(key, "Desktop")[0])  # Unicode轉化為str
    return Desktop_path


class App:
    def __init__(self, width=500, height=300):
        self.w = width
        self.h = height
        self.title = 'count word'
        self.root = tk.Tk(className=self.title)
        self.url = tk.StringVar()
        frame_1 = tk.Frame(self.root)
        frame_2 = tk.Frame(self.root)
        frame_3 = tk.Frame(self.root)
        global label1
        play = tk.Button(frame_1, text='選擇檔案', font=('楷體', 12), fg='Purple', width=20, height=10, command=self.get_FileDialogPath)
        label1 = tk.Label(frame_2, text="等待您的選擇~"+filePath)
        play2 = tk.Button(frame_3, text="開始", font=('楷體', 12), fg='Purple', width=2, height=1, command=self.count)
        frame_1.pack()
        frame_2.pack()
        frame_3.pack()
        play.grid(row=0, column=3, ipadx=10, ipady=10)
        play2.grid(row=0, column=3, ipadx=10, ipady=10)
        label1.grid(row=0, column=0)

    def count(self):
        excludes = ['the', 'and', 'to', 'of', 'i', 'a', 'in', 'it', 'that', 'is',
                    'you', 'my', 'with', 'not', 'his', 'this', 'but', 'for',
                    'me', 's', 'he', 'be', 'as', 'so', 'him', 'your', 'her', 'us', 'are']

        def getText():
            file = docx.Document(filePath)
            txt = '\n'.join([paragraph.text for paragraph in file.paragraphs])
            txt = txt.lower()
            txt = txt.replace('"', " ")
            for ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'":
                txt = txt.replace(ch, ' ')
            return txt

        hamletTxt = getText()
        words = hamletTxt.split()
        counts = {}
        sumcount = 0
        for word in words:
            counts[word] = counts.get(word, 0) + 1
            sumcount = sumcount + 1

        counts_ex = counts.copy()
        for key in counts.keys():
            if key in excludes:
                counts_ex.pop(key)
        items = list(counts_ex.items())
        items.sort(key=lambda x: x[1], reverse=True)
        for i in range(10):
            word, count = items[i]
            print('{0:<10}{1:>5}'.format(word, count))
        window = tk.Tk()
        window.title('outcome')
        window.geometry('500x300')
        t = tk.Text(window, width=200, height=100,font=('楷體', 30))
        t.pack()
        lines = []
        lines.append('單詞種類:' + str(len(items)) + '\n')
        lines.append('單詞總數:' + str(sumcount) + '\n')
        lines.append('詞頻排序如下:\n')
        lines.append('word\tcounts\n')
        s = ''
        for i in range(len(items)):
            s = '\t'.join([str(items[i][0]), str(items[i][1])])
            s += '\n'
            lines.append(s)
        print('\n統計完成!\n')
        out = ' '.join(lines)
        t.insert('insert', out)

    def center(self):
        ws = self.root.winfo_screenwidth()
        hs = self.root.winfo_screenheight()
        x = int((ws / 2) - (self.w / 2))
        y = int((hs / 2) - (self.h / 2))
        self.root.geometry('{}x{}+{}+{}'.format(self.w, self.h, x, y))

    def loop(self):
        # 禁止修改視窗大小
        self.root.resizable(False, False)
        # 視窗居中
        self.center()
        self.root.mainloop()

    def get_FileDialogPath(self):
        global filePath
        dlg = win32ui.CreateFileDialog(1)  # 1表示開啟檔案對話方塊
        dlg.SetOFNInitialDir(get_desktop())  # 設定開啟檔案對話方塊中的初始顯示目錄
        dlg.DoModal()
        filename = dlg.GetPathName()  # 獲取選擇的檔名稱
        filePath = filename
        label1.config(text='請確認您選擇的檔案:' + filePath)
        label1.pack()




if __name__ == '__main__':
    app = App()  # 例項化APP物件
    app.loop()  # loop等待使用者事件

相關文章