English Dictionary on Python
Have you ever tried to read from the small Oxford Dictionaries? Those small yet bulky books have thousands of words cramped on a page the length of your finger! And on top of that, not to forget the hassle of flipping across the pages, searching for your word, God knows where is hiding on which page!
Nowadays though, hardly does anyone ever use the classic dictionaries. With the access of technology on our fingertips, one tap on Google, and you would be on with your way. But how about making a program of your own which can do the same for you? Sounds interesting?
Such a program can enable you to run it and keep it opened, while you are reading a book, so that you can search the definition of a new word you encountered. What if you are writing a book, perhaps a report? You know what you are supposed to write, but you choose to get a word for it, so that you sound professional. So you just search for a short definition and the program find the word with matching definition.
What if along with writing a book, you also require a way to get only one type of word, say only nouns? A program which takes any combination of above mentioned factors to give you matching results.
And a finishing touch: You wish to become a 'Word Enthusiast', and have your way with words? How about a subscription that sends you an email regularly with the definition of a new word.
That's what we are going to do today. And don't forget to download the entire python program from the link given in the end, free!
Downloading the Dictionary
We require a .csv file, which contains all the words in the English Dictionary. For this project, it shall contain the word, the type of word (noun, pronoun, preposition...) and the definition. I have downloaded this file: English Dictionary File (credits to Manas Sharma, taken from here: Bragitoff.com).
If you manage to find any other file with similar format, then no problem, yet if you proceed with this file, the code may not need any tweaks. Here's how the file looks:
As you can see, the first column consists of the words, the second has the type and the third the definition. Download this file wherever you prefer and copy its directory (along with file name) to declare it in the program. For example: C:\users\dictionary.csv
MySQL Part
For the program to send emails to those people who have subscribed to receive a new word with definition everyday, you need to create a database which stores the email ID of the subscribers. Create a Database called dictionary_subscriptions and create a table 'subscribers' using the following query:
create table subscribers(email varchar(100) not null unique);
And that's it!
Required Modules/Libraries
We need pandas, imported as pd, to get the information from the .csv file using pd.read_csv() command. We will also import Tkinter for the user interface, along with messagebox which comes with Tkinter. mysql.connector for the Python-MySQL connection and smtplib, ssl for sending emails. We also require random and datetime, but these are pre-installed.
Program Overview
The program will let us input any combination of word, word type and word definition, and search the dictionary if the inputted word is a part of or the entire word is present in the dictionary, looping the entire file. Results will be posted on a new window with each window providing the matched results. You also get an option to subscribe or unsubscribe from receiving a new word everyday by email.
Below I will be explaining the code, one snippet at a time. You may skip it to the end to get the source code download link.
The Code
results() is taking two arguments: result_indexes and page. result_indexes is the same list that we prepared, and page, which is a variable containing the page number of results, initiated from the last function. Since we are taking only as many number of words in a single page as in words_per_page, we will require multiple pages. This variable page, as a result will start from 1, and every time the user choses to see the next set of results, the value will get increased by 1, so that the function posts the next set of words. This will go on until page == last_page.
We also have a title_label which will show us which page we are on. We have initiated a variable n from 1, which is purely used for label placement, so you can ignore that stuff. We have a for loop for placing each word, word type, and its definition on the page. There are if-elif statements governing when the number of words posted have reached the words_per_page limit.
From what I had seen, some definitions were very long which made it posting them on a single line look unprofessional. So internally, you can see: for m in range(0, len(defi)) which is adding \n after a particular number of words have been inputted (mentioned as [20, 40, 60, 80, 100]). This \n signals the program to write the trailing words on the next line.
If you remember, I had stated earlier that we have stated root to be global. If you have noticed, the results page also uses the same root. Now here's something to remember: It is usually advised to use top = Toplevel() for every new window you open, instead of root. But in this case, I have used root for results page as well. This is because, with every click of the next page button we will provide, we are telling the program to destroy the current results page, open a new page and show the next set of results. If you use top = Toplevel(), the program fails to understand which window it had to destroy. Therefore, we had declared root global earlier, and now we are using the same for the results pages.
We have so many buttons for this page! We have buttons for going to the next page, or the previous page, which on clicked, restarts this entire function with page+1 or page-1 respectively. This makes the process convenient. There is a button for jumping back to the main menu, which access a small function called back_to_menu() which simply destroys the results page and opens the main_menu() function, so that the user can search for a different word. And then there are two buttons for jumping to the last page or the first page, which again restarts the function with the appropriate value for page. The page turning buttons are also made to become disabled, when they shouldn't be clicked, for example, when the user is on the last page, the jump to last page and next page buttons get disabled. Rest of the stuff is just the placement of different elements on the page.
Moving on to our next functions, subscibe() and to_unsubscribe(). Both of them are connected from the main menu from a single button with text 'subscribe'. Both of them have single entry fields each that ask the user for their email ID, to subscribe for the daily learning. They have submit buttons connecting to other functions to_submit() and submit2() which are passing the queries to add or delete the email IDs from the MySQL database, whose connection we have initiated on the top of the program. Here's their code:
Coming to the last function, daily_learning():
So we are fetching all the email IDs from our database, getting a random word, and sending them via smtp, the code for which you should copy as is, with tweaks you may want for the message, and also change the sender's email ID and password with that of yours. Also, you need to sign in to google with this account and turn on Allow Less Secure Apps for the email to be sent. Otherwise, Python will show you the error that the permission was denied.
In the end, at the bottom of the entire code, all you have to write is this:
that is, we are initiating the functions daily_learning() and main_menu() when the program is run, thus sending the emails to the subscribers as well as opening the main menu.
And that's it. We have completed our program and it is ready for use. Read below the summary of tweaks that you have to do to run it.
Your Tweaks
- Change the MySQL database argument in the mycon = mc.connect(.... with the name of the database that you have created for the purpose (for the subscribers).
- My table in my database is called subscribers. If you have created a table with a different name, replace subscribers with the name of your table under the functions to_submit() and submit2().
- Change file_url with that of the directory where you have downloaded your .csv file on your pc.
- Change the sender's email and password with that of yours under the function daily_learning().
Working
And here's the Email that the subscribers get:
So we have reached the end of this project! Look below for the download link of this project! Don't forget to go over the tweaks you have to do before running it yourself, as discussed above. Enjoy!
Comments
Post a Comment