Write a Python Program To Determine the frequency of words from text file using wxpython

  The following program will help to determine the frequency of words from text file using wxpython.

Required Libraries/Modules :

pip install wxPython == 4.1.1

Python Program

   
 
/*;==========================================
; Title: Determine the frequency of words from text file using wxpython.
; Author: codenaive Santosh Kumar  <[email protected]> <[email protected]>
; Date:   22 Jan 2021
; Programming Language: Python
;==========================================*/	
import os
import wx
from collections import Counter

class  PanelFrequency(wx.Panel):
    store_words=[]
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        button = wx.Button(self, label='Click to open Text File')
        button.Bind(wx.EVT_BUTTON, self.onFileOpen)
        sizer_h = wx.BoxSizer(wx.HORIZONTAL)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer_h.Add(button, 0, wx.CENTER)
        sizer.Add((0,0), 1, wx.EXPAND)
        sizer.Add(sizer_h, 0, wx.CENTER)
        sizer.Add((0,0), 1, wx.EXPAND)
       

        self.SetSizer(sizer)

    def onFileOpen(self, event):
        supportedFile = "TXT files (*.txt)|*.txt"
        dialog_Box = wx.FileDialog(self, "Open Text Files", wildcard=supportedFile,
                               style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

        if dialog_Box.ShowModal() == wx.ID_CANCEL:
            return

        filePath = dialog_Box.GetPath()

        if os.path.exists(filePath):
            with open(filePath) as fobj:
                for singleLine in fobj:
                    self.singleWords=(singleLine.split());
                    for w in self.singleWords:
                        self.store_words.append(w);

    
        self.store_words=Counter(self.store_words);
        frequency_number_max=4;
        string1=str(self.store_words.most_common(frequency_number_max)[0][0])+" : "+str(self.store_words.most_common(frequency_number_max)[0][1])
        string2=str(self.store_words.most_common(frequency_number_max)[1][0])+" : "+str(self.store_words.most_common(frequency_number_max)[1][1])
        string3=str(self.store_words.most_common(frequency_number_max)[2][0])+" : "+str(self.store_words.most_common(frequency_number_max)[2][1])
        string4=str(self.store_words.most_common(frequency_number_max)[3][0])+" : "+str(self.store_words.most_common(frequency_number_max)[3][1])
        dlg = wx.RichMessageDialog(self, "Four most word with high frequency : \n"+str(string1)+"\n"+str(string2)+"\n"+str(string3)+"\n"+str(string4)+"\n")
        dlg.ShowModal() 
        self.store_words=[]


class FrameM(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Count the frequency of the words")
        obj = PanelFrequency(self)
        self.Show()

if __name__ == '__main__':
    myApplication = wx.App(False)
    mainFrame = FrameM()
    myApplication.MainLoop()
	
	
						
						

Output

Frequency of words Python GUI
Frequency of words Python GUI

Leave a Reply

Your email address will not be published. Required fields are marked *