Welcome back.
A set contains only unique values.
Just like a map.
In this lecture you're going to learn how to use a map as a set.
You're going to build a program that can answer whether a given word exists in a file or not.
All right let's get started
first of all let me show you the input file that you're going to use.
This is a passage from Romeo and Juliet from Shakespeare.
It contains 70 unique words and 99 words in total.
So the map that you're going to create will store only 70 words because map keys are unique.
OK now I'm going to create a new scanner that uses the standard input.
How can you split the input by words? You can use strings dot split the function for each line.
Right.
However there is a more natural way you can configure the scanner to scan only for the words instead
of lines.
All you need to do is calling the split method and telling it to scan for the words like so.
Let me show you how it works.
First I need to scan for the words like so each time I call the scan method.
It will scan for a word not a line.
Let me show you.
I'm going to print the scanned word by calling the text method like so
as you can see it scans for the words instead of lines all right.
Since you have a program that can scan for the words it is time to index the words using a map to do
that.
I'm going to create a new map like so the key type is a string and the element type is a bool.
The keys will be the unique words the corresponding elements will tell you whether the word is in the
map or not.
So the user will be able to find out whether a word exists in the input or not.
Now I'm going to remove the println and I'm going to get the word using the text method like so.
Let me also change the word to lower case letters because a String map keys are case sensitive.
I also don't want to index tiny words.
So I'm going to index a word only if it has three letters at least.
OK.
Now it's time to index the words in the map like so but why do you need to set it to true.
It's because this is what makes the map a set.
I'll explain it in a minute.
No worries.
Let's say the user asks for the sun word.
Let's check the word using the comma-ok.
idiom like so if the word is in the map the ok variable will be true.
So I'm going to say the input contains the word and I'm going to terminate the program using a return
statement.
Otherwise I'm gonna say sorry the input does not contain the word
since there is a sun word in the input.
It says the input contains sun.
Let's see whether Shakespeare talks about gophers clearly it does not.
I'm not surprised at this at all.
However this code is not right.
For example let me show you what the map returns when I ask for sun and Tesla as you can see sun returns
true whereas Tesla returns false.
It's because when the scanner returns the sun word I set it to true in the map.
That's why it returns true here.
However the map doesn't contain the Tesla word so it returns false because the zero value for bool is
false.
So I don't need to use the comma okay idiom here.
I can directly check whether a word exists in the map or not like so. Let me also remove this code. As
you can see it still works.
Let's also try it with the sun word it works.
Now it's time to get the word from the user.
Now I'm going to fast forward it because you already know how it works.
I'm going to remove the query variable from here.
I'm going to get it from the user.
All right let's try it as you can see it works although there are 99 words in the input file here.
You only need to store 70 words because the map keys are unique and that's efficient by the way.
You don't have to pipe a file.
You can pipe anything but for now let's directly enter the input.
I'm going to look for the beautiful word.
Now I'm going to say today is a beautiful day as you can see it says that the input contains the beautiful
word. Cool!
You can even check whether a website contains a word that you are looking for.
To do that you can pipe the curl command to your program.
The Curl tool can retrieve the contents of a website.
For example I can get a text document from the web like so "-s" argument here tells the curl that
only to fetch the content.
Normally
It also shows us the download progress the s flag prevents that as you can see it retrieves the source
code of the article.
Now I can search for a word in it using our program.
First I need to type the same command again.
Then I need to pipe the content to our program.
Like so I'm going to search for the outside word.
Like so.
As you can see the article contains the word.
Let me show you the indexed words.
I'm going to loop over the keys of the map then I'm going to print the word as you can see the program
not only indexes the words it also indexes the numbers and punctuation characters or sometimes a word
ends with a dot or a comma.
So if you search for these words it won't be able to find them for example let me search for the inclusive
word even though the document contains the inclusive word.
The program couldn't find it.
It's because the map is doing an exact match and the word answer with a dot.
For example if I say inclusive that it can find the word.
Now it works.
But this is not good.
So how can you remove the numbers and punctuation characters.
Well there are a lot of ways but the simplest one for now is using a regular expression.
This lecture is about maps and scanners as you know.
So I'm not going to talk about the regular expressions in full detail.
They're out of scope but put simply a regular expression allows us to find a pattern within a text
to create a regular expression.
You can use the regexp package.
I'm going to create a pattern that matches any characters but letters.
I've called the must compile function out of the loop because it's a costly operation.
You only need to compile the regular expression pattern only once then you can use it as many times
as you want.
Compiling a pattern allows the matcher to work faster.
Okay.
Now let's see how it works.
The must compile function takes a string and returns a matcher.
So if we give it an incorrect pattern it will crash your program any function that starts with a must
word does that it must work or it will crash.
I know that my pattern is fine.
So there is no harm in calling this function here.
The square braces mean match to a character.
The caret symbol means do not match. a to z means any character from a to z.
And lastly the plus sign means match for the same pattern.
One or more times.
So in summary this pattern means find any character except letters one or more times the must compile
function returns me a pattern matcher so I can use it to remove anything except letters.
Let's use it down here.
I'm going to call another function to replace anything but letters like so. You give it a string and
you say search the pattern inside the word.
And if you can find it replace it with an empty string.
So this will remove anything but the letters.
Let me show you.
As you can see now there are only words without any numbers or punctuation characters.
Awesome.
For example the inclusive word doesn't contain dot anymore because I have removed it by using the regular
expression.
So now the program says that the document contains the inclusive word.
Cool.
These were a few examples to show you that using the buf io scanner you can get input from anywhere
and you can use a map as a set.
All right.
That's how you can use a map as a set.
That's all for now.
See you in the next lecture.