Hey, in this video, I'll show you my solution to the tasks I gave you in the previous video.
So this is what I did.
I loaded the book here.
Then to extract the paragraphs were love was used.
I used this code.
So let me explain you the code here, what it does.
So basically, you need to understand what a paragraph is.
So if you if you look at this.
Text file.
Where the book is, you'll see that this is a paragraph and two paragraphs are split by break lines.
And break lines in Python are represented as backslash ends.
So this represents a paragraph.
This pattern matches a paragraph which contains love.
So basically we're saying, give me any string which has love and it should not have backslash.
N So this is a negation, this symbol here.
So it should not contain backslash n one or more times.
In other words, this will match any letter, any character but break lines.
So break lines will not be counted.
And same goes after the words love.
So anything will be matched.
Space is commerce, other characters, but not break lines.
Now that should give you the findings list.
So I've just printed out just the first two paragraphs here.
So a list of two paragraphs, But there are more.
Of course, I just didn't want to show them in here.
Extra of the chapter titles.
This can be done in two ways, and this is very important because you'll learn a new concept about regex
regular expressions.
So the first method, which is more primitive, is this.
So we're looking at capturing these pieces of text.
Write everything Precious.
That's a title.
So the secret to match this from the text is that you should see what makes this special, this text
special from the text.
And what I found out is that the text is always in letters.
It doesn't have any periods.
It might have commas though, so you could have a comma here, so you could add a space and a comma
because the titles of the chapter titles can contain spaces, but sometimes maybe they can contain a
comma as well, but they don't contain periods.
So I'm not adding a period here.
So if I execute this, I get this list of chapter titles and then they are.
Followed by two break lines.
So two break lines after this.
One and two here.
So that pattern will match actually these, but together with the backslash ends.
So this line here, which if I remove that, I'm going to get this together with this backslash ends.
So you might ask, well, we don't need to capture these.
We only need to capture the titles without the backslash ends.
And the other method does a better job on this.
But if you don't use the other method, which I'll explain in just a bit, you'd have to strip out the
title.
So the findings list of strings you need to to strip it out of these backslash ends using a list comprehension.
And then the new findings will be those without the backslash ends.
However, there is a better way to do this.
Again, we import our E and the pattern is the same as this, but it does have a very crucial difference.
I have placed this port around parentheses.
You see an open parentheses here and I closing parenthesis here and backslash ends are outside of those
parentheses.
That means that tells the compile methods to return only the parts inside the parentheses.
So in other words, the round parentheses tell Python to only get these.
In the returned findings.
So these two are just to indicate what is after that expression, but these are not included.
So everything that is outside the parentheses serves as a pattern, but it's not included in the results.
So that's the better way to do this.
Of course, I didn't expect you to do this this way, but this was a great opportunity to learn this
method as well.
Great.
Then a function which finds the occurrences of any words.
Basically, this consisted of putting together the codes you learned in the previous videos, putting
together that code inside the function.
So that's a function.
It gets one argument.
W then we construct a pattern here.
So this represents a new world.
Any bunch of letters in a row and a bunch of letters one after the other.
And so then we get the findings.
Then we calculate.
So we count how many words we have, how many occurrences we have for each word, and return the dictionary
in here.
But we only return the dictionary if the word is found.
Otherwise we return the string.
So on F string where we we inject the words that the user entered here.
So it's basically the same codes that we use before, plus this try accept.
BLOCK And then I'm calling the function here and we get this result.
So that's the complete code for the exercise.
And I hope this makes sense to you.
If you have questions, drop them in the Q&A.
Thanks.
See you.