 Right, so hey guys and welcome back to another Python tutorial. So in this video I'm going to be showing you guys how to actually create a Keyword search algorithm using Python. So you might be wondering what this is. So this is just going to be a nested for loop, which is going to help you go through a string and then find out if there is any repeating keywords in there and Basically, it's going to print out the count of each word that you give to it So many search engines use these algorithms to kind of rank keywords up So this might sound a bit confusing right now, but as soon as I start coding you understand what I mean I'm going to go ahead and open visual studio code like always and then maximize that and Zoom in a bit so that you guys can see a lot more clearer Cool, so I'm going to create a new file now and then save this file as search engine.py Because it's a Python file, so we got to use the Python extension Save this up and now we're going to start so as I was saying before in this tutorial We're creating an algorithm, which means it's going to be like a method. That's going to help us do something So this algorithm what it's going to do is let's assume this was Google right and we typed in something like I don't know how to How to Create YouTube something like that. So what Google would go ahead and do is it would take this whole string Obviously, it has a bit more complex algorithm But let's just say Google would take this whole string and then convert this string into individual words And then these words would then be stored in an array. Now when it comes to shopping and stuff a Certain keyword can be ranked up or lower or even on YouTube's search algorithm certain keywords are ranked more than Other ones. So that's what we're going to be doing. So for example in this case our algorithm is going to return how equals one to equals one create equals one and And we also have another keyword in there, which is YouTube and that too equal one So in the case of which we actually go ahead and edit our search query and type in YouTube Again in there what should go ahead and happen is our algorithm should return YouTube to so basically what it's doing Is it's ranking the keywords are giving us a count of the keywords from our search? So without further ado, let's actually start coding this concept So first of all, we're going to be needing is a variable that's going to store our search criteria. So search equals Let's just say how To create a password Generator using Python, so let's assume this was our search query We've got to break this search into different words because right now it is just a whole string as one unit We've got to break it into different words. So so do so to do so we're going to type in a new variable called words equals that to search Split so what this will do is it's going to convert this search string into an array of different words So when I print for example words Zero It's going to print the word that is at the zero position, which is how if I print words one It's going to print two. Let me run this and show you So if I run it as you see right here, it says to now if I go ahead in here in the terminal and type in I don't know if I can Probably cannot but if I change this number to two Okay, for some reason visual studio code has frozen. So we're gonna have to Task manage it to the rescue and Then we're gonna have to find out why it just froze on this So I'm gonna close that off open visual studio code again. Hopefully it's going to be saved Okay, perfect. It is saved now. I'm gonna go ahead and access words two or let's say three So what it should return when I type in three is it should return zero one two three So it would return a actually let's go with four so that it returns password So now when I run it it returns password cool So as I said, it has been able to successfully convert this whole string into individual words now once that's done We're gonna need a dictionary Which is going to store the counts Against the words so it would be something like how it's going to have One then it would be two is going to have one and this is all going to be automated for now It's gonna be blank. We're gonna populate it in the loops and we're also gonna need another variable called count Which is gonna keep a track of how many words are being discovered. I'll come across this late We're gonna come across this latest. They'll make a bit more sense in a bit in a bit not in a minute So I'm gonna go ahead and create my first for loop now Now this first for loop is going to be a pretty simple one. So for a variable i in range From zero to length of words This basically means this loop is going to run from the number zero to whatever the length of words is So how many words are in here? So there are how many one two three four five six seven eight There's eight words in there. So this loop is gonna run eight times So pretty much it's gonna help us go through each word and search this string to make sure it doesn't have a repeating form of itself so we're gonna go ahead and then Assign a new variable. So I'm gonna type in first word equals words I So what this is gonna do, let me show you real quick This is gonna this loop right here is gonna go through each word in this array one by one. So let's run that And as I said right there it goes through each word one at a time So in the first iteration went through how second iteration went it through and then as on it just kept going on And if I change the string it will work as well because it's going based on the length of words. So it will Dynamically adjust itself when it needs to so now since we've got the first word What we're gonna be doing is we're gonna have a second loop that pretty much checks whether the first word matches any other words in the string or in this array So let's say the first word was how? the second word is going to be to then if it doesn't match with this the second word is gonna be create if it doesn't match It's gonna be a if it doesn't match It's going to be password if it doesn't match is going to be generator if it doesn't match It's going to be using and once again if it doesn't match, it's gonna be Python now if none of the words match count is going to be on zero and And then we're going to basically take the first word and then push it into our count's dictionary with a count of zero. Now if there was a case where there was another how in here, the second word will pretty much realize that it's matching the first word and then it would update the count to two and then it would be pushed as how comma two. Now this will make a bit more sense as soon as we start coding the second loop. So now the second loop is going to be nested in the first loop because we need the first word first and then the first loop is going to wait for the second loop to complete. So for i in range, pretty much the same thing we're doing again, zero to length of words. So we need to go through the entire array again because we need to check whether this first word matches any other words in that array. We need to do a variable called second word which is going to be equals to words i again because we need to pretty much go through all the words in that array. So second word is going to include every other word in that array. And then we need to do an if condition here. So if first word equals second word, then what we want to do is go ahead and update our count. So count equals count plus one. So if the first word matches the second word, then we update our count from zero to whatever how many words it's matched. Now we've also got to realize that once we're done with the first word, it's also going to move on to the next word. So when we move on to the next word, we don't want our count variable to be stuck on a specific number which is why we need to re-initialize it to zero. So before we do that, we actually need to go one and then back in the if statement and then as soon as we have our count for that word that we were talking about, we're going to be pushing the first word and then we're going to be giving it the value of count. So that's the first word is going to be the key and the count is going to be our value. And then once that's done, we re-initialize count to zero. Now lastly, after both these loops are done, it should just return the outcome to us. So if I go ahead and type in print counts, it should pretty much just print the counts of all the words to us. Let me run this quickly. And as you see right here, how zero, two, zero, create, zero, eight, zero, password, zero, generator, zero, Python. Okay, why is it saying zero actually? It should say one. Let's take a look. Oh, I see. So this counts needs to go another and then back. And the count equals zero needs to go another and then back as well because that needs to be happening after the second loop has ended. So let's run this again and it should hopefully work this time. Yep, it is working. So as you see, all the keywords that we have have a count of one, which is true. Let's go ahead and check if that's true. It is because we have all the keywords in here are only repeated once. Let's go ahead and add Python twice in here now. And then our outcome should be updated to be the key of Python to the value of two, which means Python has been repeated twice. Now quite rightfully, it does say Python two, which means our algorithm is working perfectly fine with even different sets of data. Let's go ahead and change this to using another time and then we'll do how another time and create another time as well. Cool. Let's run this now. And as you see, how has been updated to two, create has been updated to two, using has been updated to two and Python has been updated to two and the rest of the keywords are still on one. So as you see, this algorithm is working pretty well for us. You can use this algorithm for different programs where you might want to make a chatbot or something and want to weigh out which keyword is more, let's say more, what do you call it, pretty much makes more sense the other one. So more relevant than the other one. You could use this to give a keyword a score pretty much when you're using searches. So that was about it guys for this tutorial. Hope you actually understood how this algorithm works. I do understand it's a bit complex to understand in the first go, but if you did understand it, I would recommend going through it again and again and then practicing it so that you can actually understand how the simple looking code does a complex task in here to make our life a lot easier. Anyway, hope you've enjoyed today's tutorial because it's a lot different than what we usually do. If you'd like to donate to the channel or support the channel directly, you can do so by either being a Patreon and signing up using the link in the description. Or you can also use a Super Chat emoji or a highlighted message. I'm not forcing anyone to do so, but if you'd like to do so, go ahead. Also guys do consider joining the Discord channel, which is going to be stated in the description for a lot of fun. If you have any suggestions, drop them in the community tab under my posts. I will see your beautiful faces in the next tutorial. Peace.