Creating a Word Counter in Python
In this post, we are going to see how we can make a word counter using Python.
For this, we will be using the split function to make our job easier by reducing the length
of code and increasing its productivity.
split() function is a quite useful and quite generic method to get words out of the list.
But this approach is applicable only till we avoid any special characters.
The code for word counter in Python
#string on which the operation is to be performed string_example = "All is well that ends well" #printing original string print("The original string is= " + string_example) #using split #counting words res = len(string_example.split()) #printing the number of words print("The number of words in the string are : " + str(res))
The original string is= All is well that ends well
The number of words in the string are: 6
The string_example variable is the one that stores the string we wish to use for counting the words it holds.
After printing the original string we use another variable res and deploy the len function along with the split() function embedded within it.
The res variable stores the number of words in the string_example holds as the split() function has broken up it into words.
The next what we do is we print the number of words by printing the values stored in the res variable which is 6 in this case.
Creating a Word Counter GUI in Python
GUI(Graphic User Interface) is nothing but what we see on our systems and access it with. It is a graphically more rich interface of what happens inside the system whenever we command it to do something. Our aim here is to create a GUI which takes input as a string and gives the output in terms of number of characters present in it.
#import library import tkinter as tk #counting function def charcount(): output.delete(0.0,"end") w=inputUser.get(0.0,"end") sp=decision.get() c=0 #specifying conditions if sp==1: for k in w: if k=="\n": continue c=c+1 elif sp==2: for k in w: if k==" " or k=="\n": continue c=c+1 output.insert(tk.INSERT,c) #creating interface window=tk.Tk() window.title("Count Characters") window.geometry("500x600") label=tk.Label(window,text="Input") #Formatting inputUser=tk.Text(window,width=450,height=10,font=("Helvetica",16),wrap="word") decision=tk.IntVar() #Radio buttons for space counting r1=tk.Radiobutton(window,text="with spaces",value=1,variable=decision) r2=tk.Radiobutton(window,text="without spaces",value=2,variable=decision) #BUtton to count button=tk.Button(window,text="Count the number of characters",command=charcount) label2=tk.Label(window,text="number of characters") #Output Block output=tk.Text(window,width=20,height=2,font=("Helvetica",16),wrap="word") #Function calling label.pack() inputUser.pack() r1.pack() r2.pack() label2.pack() output.pack() button.pack() window.mainloop()
The program above is a GUI code for counting characters in a user-specified string.
The char-count function is responsible for the counting of characters in the given string. More importantly, we have made the GUI more friendly and adaptive. This has been done by putting up conditions that help the user to choose whether spaces are to be counted or not, for instance. The interface is created using the Tkinter library imported as tk. The specifics of dimensions and formatting help design our GUI as per our wish. Buttons are added along with labels in the next part of the code which are followed by calling of functions to execute the code.
There are many software codes that involve the processing of the text, for instance, linguistic data provided to it so that it can be studied more effectively.
Many fields involving Natural language Processing and Data Science require us to provide the parent software with data is the simplest and fraction-ed form.
Python is the most widely used language in these fields. It makes it very easy to work with by providing tools that work effectively.
The split () function and many others like this one in the python language help a lot achieve our goals faster.
The code to break words apart from a sentence usually follows a very complex and lengthy code in other and older languages. Our job is simplified by using these shorthand tools and implementing them to get
results in a single line of code.
Also read: splitlines() function in Python