Finding sound in books, or my data science project. Week 4
ANOUK DYUSSEMBAYEVA | SEPTEMBER, 24 / 2020
I started a project in hopes of creating a software that can analyze how much sound there is in a certain book, and whether it could add sound to the text in real time to produce an audio drama on the go. Here is what I did during this week to get closer to my goal. To read the introductory article, click here
Photo by bongkarn thanyakij from Pexels
Last week, I was able to solve a few issues regarding the code to enhance the outcome and analyze different books to test the algorithm. I also set a goal of creating an interface that would allow other people to upload their favorite book and see how much sound it has.

Deciding to go with tkinter to build my Graphic User Interface (GUI), I started by reading Python documentation of it and learning more about how to use it. Tkinter is short for Tk Interface, which is the standard Python interface to the Tk GUI toolkit. When running it, a simple window opens, displaying all the functions that you assigned.

Before I could build my own interface, I had to grasp the basics of tkinter and its applications. As always, Google was happy to help — I found a myriad of tutorials and simple steps to create buttons, labels, and titles. Practicing the code from the tutorials, I tried adjusting it to fit the purpose of what I wanted to develop.
import tkinter 

window = tkinter.Tk()
window.title("Sound Book")

label = tkinter.Label(window, text = "Welcome to Sound Book!").pack() 

button_widget = tkinter.Button(window,text="Start")
button_widget.pack()
tkinter.mainloop() 
The code in my Jupyter Notebook
In the snippet above from a tutorial on DataCamp, I imported tkinter to start the program, created a window named "Sound Book" that had "Welcome to Sound Book!" written inside, and included a button that said "Start", and ran the code.

However, even though I was able to create buttons with customized text, background color, and a window with a customized title, that was pretty much it. My next step was finding out how to make multiple windows, the most easiest way of doing so being through the help of buttons. Although searching for a solution took quite some time, actually implementing the function wasn't that hard.
#creating a function that would open the window
def new_window():
    window = tkinter.Toplevel(root)
    canvas = tkinter.Canvas(window, height=HEIGHT, width=WIDTH).pack() 

HEIGHT = 400
WIDTH = 300 

#the original window
root = tkinter.Tk()
root.title("Sound Book")
label = tkinter.Label(root, text = "Find how much sound your favorite book has").pack() 
canvas = tkinter.Canvas(root, height=HEIGHT, width=WIDTH).pack()

#create button that will be placed
button = tkinter.Button(root, text="Let's Start", bg = 'white', fg='#469A00', command=lambda: new_window()).pack()

root.mainloop() 
First, we are making a function new_window that would be the second window. The original window, root, is titled "Sound Book", says "Find how much sound your favorite book has", includes a button that states "Let's Start", and when that button is clicked, the new_window opens with the help of the command function.

While I marveled at the fact that I now knew how to make multiple windows, I also realized that it doesn't get me anywhere closer to my goal, which was to write a code in a way that would allow users to upload PDF and see how much sound it has. In fact, I didn't even need to know how to create several windows at once.

Once again, I spent a solid hour hunting for something that would show me how to do just that — upload a PDF file when using the interface. For that, importing filedialog was crucial, and GeeksforGeeks had a great tutorial on using the askopenfile function.
from tkinter import filedialog
from tkinter.filedialog import askopenfile

window1 = tkinter.Tk()
  
def open_file(): 
    file = askopenfile(mode ='r', filetypes =[('PDF Files', '*.pdf')]) 
    if file is not None: 
        content = file.read() 
        print(content) 
  
button = Button(window1, text ='Open', command = lambda:open_file()) .pack() 
  
mainloop()
Essentially, it was the same concept as creating multiple windows, since both use the def ____ function and then implement it via command when the button is clicked.

It seemed to me that now the user interface code is close to the finishing line and everything would come together. Little did I know that implementing the behind-the-scenes part, where the PDF is actually analyzed and compared to the list of sound words, wouldn't be working. I would always get the same mistake, yet no matter how many times I looked for answers to solving it, there wasn't any advice that was of real help.

Subscribe to the newsletter and stay tuned for next week's advances!
Made on
Tilda