Text/Profanity Checker in Python

Daniel Bourke builds a Python script that reads a text file and sends it to a web service to check for profanity. The script uses urllib.urlopen to call Udacity's API, then prints either a profanity alert or confirmation that the document is clean.
Chapters
Alright, what's going on guys? Daniel Bourke here. Welcome back to another episode of me going through some programming. Just to let you know, I'm learning how to code in Python using Udacity and another of our other resources, and I'm just going to take you through one of the projects I've just done in Programming Foundations with Python. Essentially, this is a profanity editor. What does that mean? Let me just start off, actually, this is— all of this that I'm doing is available on Udacity for free. It's the course, as you can see it there, called Programming Foundations with Python. If you just Google Udacity Programming Foundations with Python, you'll be able to find it there. Sign up, check it out if you want, if you're interested in learning Python, one of the most popular languages on the web. But enough from that. Oh, and just remember, I'm no expert. I'm learning as well. I'm making these videos to practice and cement my learning. So, what was the goal of this project through Udacity? It was to essentially take a piece of text like this here. It could be any text that you want. It could be something as simple as, "Hello, my name is Daniel Bourke." Right? And it's— the goal was to scan the text using Python and then determine whether there was any profanities within that text. And now, what are profanities? Well, swear words essentially. You know what they are. You can Google them, or if you've watched YouTube for long enough, I'm sure you'll be able to find them. And so, here's my code here from the— I had a lot of help from the Udacity videos. But essentially what I'm going to do is I'll show you an example of me scanning through this particular text here. It's just a whole bunch of movie quotes. And then show you what, what my code outputs, and then I'll go through each of these things here step by step so I can cement my knowledge, and hopefully you guys can learn something too. So let's run this. We go run. Beautiful. So bring over the results here in the shell. So this document has no curse words. Right, so it's saying that this piece of text, this group of text here, has no curse words. And that you can see here, this document has no curse words, that has printed out to here. So how, how does this all work? Let's go right from the top. So to start with, we're importing urllib, or URL lib. Now what does that mean? Well, we're importing a library called urllib. That means if someone's coded a whole bunch of things in urllib before, we don't have to recode them in our file. We can use them, and I'll refer to where we use urllib later on in the code. But what are we doing here? First of all, we're defining a function. So if you break this problem down, you would have two main steps. You would have the step to read the actual text in the file, and then another step will be to check whether any of the text any of the words in that text is a curse word or is profanity. So the first function, we're going to break it down. Function 1, read text. Function 2, check whether there's a parameter— uh, profanity, sorry. So read text, so we're going to define a variable here called quotes, and now we're going to use the Python keyword open. If you're not sure what that is, you can Google Python keyword open, and it will show you what it does. Essentially, you throw in a pathname for a file that exists on your computer, and it will open that file. So this particular path, /Users/Daniel/Desktop/moviequotes.txt, is where this file lives on my computer. Now, just a quick note, if you're making a text file in on Mac and you're using TextEdit, the default saving type is.rf,.tr,.rtf, otherwise known as rich text file. But you can change it to.txt by going into Format, and then if, if it was already.rtf, this will be changed to normal text file. Which will give you the.txt extension. That's a— I ran into that trouble doing the project on Udacity on here. This code won't work properly if you've got an RTF file, so make sure it's a.txt file. And then what do we do next? So we're defining another variable here called contents_of_file equals quotes.read. So we've defined a variable here, quotes equals opening this folder, and then we're defining another variable called the context of the file is quotes. So this, this folder, this file that we've opened,.read. And now.read is another function built into the Python library that allows us to read the text within a file. So we've opened the file and now we're reading the file within the read_text function. And then I've commented out print contents of file here, but essentially what that will do is print out the contents of this file in the shell if we wanted it to, but we're not going to do that for now. We'll go over, we'll go over the crux of the problem first. And so then we're going to close quotes, so we're closing this file. We don't really need it. It won't actually close, but it's just telling Python get rid of that file out of memory. We don't need it anymore. And so we've saved what we need in the variable contents_of_file. And so then we've got the, the function we're calling check_profanity at the bottom of read_text because read_text is the final function that we call. So we're going check_profanity and we're throwing it, we're giving it the argument contents_of_file. So whatever's saved in here is going to go into the function check_profanity. So the next step is defining check_profanity. Now if we define check, check_profanity, and then it's got a parameter here called text_to_check. And now remember, up here we've already passed it contents_of_file. So what happens here? We're defining a variable called connection, and connection equals urllib urlopen, then a web address, and on the end of that web address is text-to-check. And remember, you can, you can assume here that contents of file is the same as text-to-check. So we're opening this web address with text-to-check on the end of it. So what is this web address? Well, I've got it up here, and it's a web address that Udacity have made to give a true or false reading if you put in a profanity in this final word. So if you see how this section of the webpage, this first section, is exactly the same as this here, and then you add the text to check, which will end up on the end here. Now I've got test there, and so if I refresh this page test is not a profanity, so it will come up false. But if I put in a profanity word, so let's go here, refresh the page. Oh, I did it wrong. Enter. True, right? So the word shit is a profanity, so it returns true. And now you can use your wild imagination here and type in any Oh, you can see what I've tried here in my history. There we go. Damn is not a profanity according to this app. So what can we use a profanity checker for? Well, if you're sending an email and you're sending it to someone sensitive, imagine if we had this text file and we accidentally typed that rather than the actual quote. The actual movie title, that. So this won't pick up in a text, in an auto spelling correct, because that word is spelled correctly and the O and the I keys are right next to each other, so that could easily happen. So if we've got a shot in the dark, it could easily be a shit in the dark. We don't want that to happen, so this is why we're making this checker. So where were we? We've got— we've defined the variable connection, we've opened, and it's going to open up this website here. Now this is where urllib comes into, into play. So remember at the start we imported urllib? Importing urllib allows us to use this function here, urllib.urlopen. So urllib.urlopen is an instance that's a part of the class urllib. But we'll get on to more about classes and, and functions and whatnot down the track. I'm just going to literally run through what this code does step by step. So urllib will open this web page and pass in the contents of the file. So it's going to pass in literally every word that's in this text onto the end of this URL here, and then we define another variable here output equals connection.read. So what does this mean? So that means before we use the read, read function up here to read the quotes, and so output will make it a lot easier in our shell. Rather than it outputting a whole bunch of just true or false, we want it to, to output something, something a bit more text-friendly. So as if— let me just give you an example. This is the output we're talking about here. So connection is going to give us the result of true or false, and output will read this connection. So false.read. So now output will equal false in this case, but if we type in a swear word, profanity, the output will equal true, and then we close connection because we don't need it anymore. So it's not actually going to close the web page, but we just close it because we're telling Python we don't actually need this in working memory anymore because we have our output variable saved as true or false depending on whether or not there's a profanity in these words. And so what's happening next? We've got connection.close, we've gone over that, and then if true in output, print profanity alert. So it's saying if this value is true, it's going to print profanity alert to the, to the console as an output. But if it's false, print this document has no curse words, else print could not scan the document properly. So we're doing an if statement here. So if true print this. If else if false, print this. Else print this, right? So now read this text. There's no swear words in there, spoiler alert. So now we're going to run this and see what happens. This document has no curse words, just as we expected. So what's happening behind the scenes? Well, let's go from the top. So quotes, the variable, is opening this file, and then contents of file is reading quotes, the variable. It's saving it to contents of file, and then we're running check profanity, which is this function here, on contents of file, which is what's in here. And then we're checking the text in here via this web page. So Behind the scenes, this is— what this is doing here is actually going to this webpage, typing in all of this text, and putting it at the end of this URL. So our browser is smart enough to do that for us, to put all these weird symbols here. Like a space is actually equal to %20. '%20', and so false because there's no swear word in there. And then the output is reading— output variable will get given a true or false value depending on if there's a profanity in here. And then if true, we're printing 'profanity alert', but if false, we're printing— where'd it go? 'This document has no curse words.' Now let's change this One letter. It's the moment of truth. Let's see if this works. So now there's definitely a profanity in this piece of text here. So what should it do? I'll give you a second to think about that. So if a profanity is present, that means this will be true, right? Because if we completely delete everything off here, And we go back to type in this, it's going to give true. So if true, profanity alert. And that word's in there, so hopefully this works because it's going to pass all this text onto the end of that URL, and then the output is going to define what gets printed in the Python shell. So, moment of truth. Save this file, save this file, run. Profanity alert! Excellent. So that has essentially alerted us that we've got a profanity in our text and we shouldn't be really emailing that to our boss if we had to. Oh, and what's this little thing at the bottom here? Well, this is read_text. It's calling the function that we defined up here. And remember, this is a two-step problem. We have to read the text from here and then decide whether there's a profanity in there and print something to the, to the console. And then we call read_text. We only have to call one of the functions because check_profanity is being called in the— within the read_text function. And what do I mean by called? Well, called is just another word for being run. So, well, that's how I define it. But that is essentially how the Profanity Editor works from the Udacity Python Foundations course. And now, don't get me wrong, I'm no expert on Python, but I'm just sharing with you what I've learned. And take everything that I've said with a grain of salt. If you if you're finding that what I said wasn't making sense, I would highly recommend signing up to the Udacity Programming Foundations with Python and running through the, the examples yourself, because they do a lot better job at explaining it than I do. But I'm having fun learning some Python. I'm going to continue making these videos every time I learn something new, so hopefully you're getting some value out of them. If not, I'm getting a lot of value out of them because cementing my knowledge. I, I couldn't program anything in Python about a week ago until I started, started this course. So we'll see how it goes. I'm signed up to a deep learning course on Udacity as well, so I'll be making videos about that too. But whatever you're doing, I hope you're having a great day, and I'll— if you've got any questions, leave it in the comments. I'll try and help you out as best I can, but I'll catch you later.
More from Daniel

Small Language Models (SLMs) Are the Future: Fine-Tuning AI That Runs on Your iPhone

Introducing Sunny - A MedGemma-Powered Skin Health Tracking App (Kaggle Competition Entry)

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 4 - Comparing the results

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 3 - Running LLMs on the RTX 4090

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 2 - Formalizing the tests

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 1 - Creating a series of tests

Local Multimodal RAG on the NVIDIA DGX Spark | Part 4 - Publishing our demo to Hugging Face

Local Multimodal RAG on the NVIDIA DGX Spark | Part 3 - Putting it all together


