Hello and welcome to this art tutorial.
And mostly.
Welcome to part seven Natural Language Processing.
So what is natural language Processing all about?
Well, it's about analyzing texts.
These texts can be books, reviews, some HTML
web pages that you extract from web scraping, all sorts of text
and therefore natural language processing is a branch of machine learning
where we do some predictive analysis on text mostly.
So in this part these texts are going to be reviews,
you know, written reviews of restaurants.
And so we will make some machine learning models
that will predict if the review is positive or negative.
So this is a simple example of a application
of natural language processing.
But the algorithm we'll make in
this part will be very well applicable to other kinds of texts.
You know, you'll be able
to apply this on books, for example, to predict the genre of a book.
You know, whether a book is a thriller, a comedy or a romance.
You'll be also able to use it on HTML web pages
to do whatever kind of analysis you want to do on these pages.
You can also apply it on newspapers, you know, to predict in which category
an article belongs to.
Well, you'll see that we'll make a general model
that you'll be able to apply on most of the texts.
And of course, if you need to apply this on a more complicated text,
you can ask me some questions in the Q&A and I'll tell you what
to add to make this code work properly for your problem.
Your text.
Okay, so let's start this.
Let's start our algorithm.
And before starting with the first code section, let's do the basic step.
Let's set the right folder as working directory.
So we'll go to our Machine learning A-Z folder.
Then part seven Natural Language Processing.
So congratulations for reaching the part.
You are now entering into a very useful and exciting branch of machine learning.
So let's go in there.
And now we have to be the section Natural Language Processing.
So here we go.
That's the folder we want to set as a working directory.
So now we'll click on this more button here and then set as working directory.
All good.
And now as you can notice in this folder that we just set it as working directory.
We have two data files.
We have a restaurant reviews dot CSV file
and a restaurant reviews dot TSV file.
So I have these two data files on purpose because there is an important thing
to understand when we prepare text data sets that I would like to highlight
and show you right now.
So I'm going to go to my folder on my computer
and we'll have a look at these two files from there.
So right now I'm going to my folder.
Here it is.
And let's open the two restaurant review CSV.
So open with text edit.
Here we go.
And restaurant reviews dot CSV.
So the same open with text edit.
So that's the CSV and that's the CSV.
Let's first have a look at the CSV.
As you can see, the first line here is the title
of the future columns we're going to have in RStudio.
The first column is review and the second column is light.
And I can see that because these two terms here are separated by a comma.
And this is a CSV file, meaning that all the columns are separated by a comma.
So that's the first line containing the titles of the columns.
And then we have all our observations.
So one line corresponds to one observation.
And as you can see in each of these lines we first have the review.
So this is of course a review of a restaurant.
So wow love this place.
Meaning of course that the review is positive
and therefore in the second column the liked column,
we have a one here, meaning that the review is indeed positive.
So the variable for this liked column can take two values, 1 or 0.
And one means that it's a positive review
and zero means that it's a negative review.
So indeed, as you can see in the second review, crust is not good.
Well, of course that's a negative review.
And therefore there is a zero here.
All right.
So that's actually the kind of file we are used to.
Because since the beginning of this course we've only been using some CSV file
where the columns are separated by a comma.
But here we have something different.
We can see that we have the same columns.
First column is review, second column is liked and we have the same reviews.
So these are exactly the same data sets with the same data.
But there is one major difference.
And as you might have guessed this difference is the delimiter.
And this file. Here the delimiter is a comma.
So that's the delimiter separating the two columns.
And in this file the delimiter is a tab.
And that's why we call it tsv tab.
Separated values.
These csv comma separated values.
And so now according to you, which one
should we choose for our future algorithm?
You know, we'll have a machine learning algorithm analyzing all the reviews here.
And then the goal of this algorithm will be to predict
whether the review is positive or negative.
All right.
But now the question is
do we need a data set where the columns are separated by a comma or by a tab.
Well, the answer is, as you might have guessed by a tab.
And why is that?
It's because we already have some commas in the reviews itself.
Well, for example, this one, this review is the food comma.
Amazing.
So if we use our CSV file where the delimiter is a comma,
well we'll have a problem for this review here because for this
particular observation the first column will contain the food here.
So R will think it's a review.
The food and the second column will not be one here.
But amazing because there is this comma here that is taken for the delimiter.
And therefore it will separate the food.
And amazing.
And therefore what will happen to one, it will go to the next observation,
and therefore one will be taken for a new review.
So that will not make any sense.
And this will mess up with the whole algorithm.
And that's why it's way better to take tabs here, because,
you know, when people write reviews, they do not put tabs in the review.
Well that would be very rare.
They would put comma very easily, as we can see for this particular review.
And we can find other reviews with commas.
I'm sure of it. Yes, indeed. We have another one here.
This place is not worth your time, comma, let alone Vegas.
So it's very natural to put some commas in the reviews,
but not much natural to put some tabs in the reviews.
And besides, if you press on tab
when you're writing a review, well this will go to the next
you know button to like submit a review or something else.
But by pressing the Tab button when you were writing your review,
you would get out of the review and you would not be able to continue to write it.
So we will never find a tab in the review.
And that's why we will never have this problem of getting these
anomalies due to duplicate delimiters in one specific review.
So I really recommend to prepare
your text data sets this way with a tab separator,
because you will never have that kind of problem.
One other solution, if you really want to use a CSV,
would be to include some double quotes here
one of the left of the review and one on the right,
but you would still get some problems in case you have some double quotes
in the reviews itself, I'm sure we can find one.
Let's have a look.
Let's press command F to find a double quote here we are.
And that's exactly what I'm talking about.
For example, let's have a look at this review here.
The description said yum yum sauce.
Well that's because this person here is quoting a description found somewhere.
And so since it's quoting it's using some double quotes here yum yum sauce.
And another one here.
So even if you put some double quotes to separate your reviews from the result,
that is 1 or 0.
Well you would still have this kind of problem.
Whereas if you separate your review in the light variable by a tab,
you will never get this kind of problem
because no one will press tab by writing a review.
So definitely that's the one we'll go for.
Restaurant underscore reviews dot TSV tab separated values.
And by the way this is a data set taken from a paper from group
to individual labels using deep features by coziest et al.
So we will use this data set.
And this contains 1000 reviews.
And for each of the review we have the real result 0 or 1.
So let's do it.
Let's start implementing our algorithm.
And we will start with the first step that is to import this data set.
Restaurant reviews dot CSV into to.
So let's do it.
Let's close this and let's go back to RStudio.
All right. So now let's import the data set.
So as usual we're going to call our data set data set this way.
And then equals.
And then that's where we use a function to import the data set.
However so far we've been using the readcsv function
to import our data sets because simply our data sets were CSV files.
But as we just understood this time we're not dealing with the CSV file.
We are dealing with a CSV file.
So of course things might be different now, but
we will still type read dot
csv here and then some parentheses.
And then let's press F1 here to get some info about this.
Read that CSV function.
So what do we see?
First we see that we don't only have one import function.
Indeed, we can see here that we have the read
that table function which we haven't used yet.
The recursive function,
which is the function we've been using since the beginning of this course.
Then we also have the read dot csv two function, which is the same
as this one, with the only difference that the default separator.
You know, the delimiter that is separating your columns is a semicolon
instead of a comma as the default parameter for the readcsv function.
So that's the main difference between the two.
But that's not what we are interested in right now,
because we would like to use a function where the default parameter
for the separator is a tab and not a semicolon.
We could still use this readcsv function and change the set parameter.
But you know, let's use another function for once
to import the data set with the right default parameter.
And speaking of the default parameter, which should be the tab separator.
Well, that's actually the next import function,
which is the read that the line function.
Indeed, you can see here that the default parameter for the separator is a tab.
Empty here means tab actually.
So that's exactly the function we want.
That's the best function to use for data set right now,
because our data set contains columns separated by a tab.
So that's the function we'll use.
So I'm going to remove here readcsv
and replace it by red dot the limb.
Here we go.
And now we input the parameters.
So it's the same principle as for readcsv.
We of course need to input first the data set in quotes.
And the data set is called restaurant.
Reviews dot csv.
So we need to specify this because indeed
in our working directory for that we have the two files CSV and CSV.
So we need to specify here csv.
That's the first parameter.
And then we have some other parameters like this header parameter here
which is by default equals to true, meaning that it considers
the first line of our data set
as the titles of the columns, which is the case for our data set.
Because remember the first line is review tab liked and review is the title
of the first column, and liked is the title of the second column.
So we're good with this header parameter.
So we don't need to input this.
And same for this next parameter set,
because by default the parameter for the separator is a tab.
And that's exactly what we need right now.
And then we have this parameter quotes.
And that's a very useful parameter to input for natural language processing.
Because most of the time you'll find some quotes,
most of the time double quotes in your text.
We checked that we had some in our reviews,
and so we need to ignore these quotes because we don't want to have some kind
of misinterpretation when our reading function reads all the reviews.
So in general, in natural language processing, it's better to ignore
any kind of quotes.
We did exactly the same in Python and everything went well,
so we'll do the same here.
And to do this we add this quote parameter
and we set it equals to actually nothing in quotes.
You know by putting nothing in this quotes here,
that means that it's ignoring any kind of quotes in the text.
So that's good.
And now we'll add a last parameter that is not specified here
and which is the strings as vectors parameter.
And what is this parameter used for.
Well you know the first column of our data set contains the written reviews.
And you know, in R when we're making some classification models
which will be what we'll be doing here in natural language processing,
because basically
we'll be classifying your reviews and tell whether they are positive or negative.
So that's classification.
And you know,
when we're doing
some classification models and working with some categorical variables,
well remember we use this factor function
to specify the categorical variables as factors.
And you know right now we have some reviews.
And since in some way it's not a numeric variable,
you know, taking some continuous real values, well in some way it can
be considered as a categorical variable having some different factors.
But in natural language processing we must not identify
the reviews as factors in R and that's because we will analyze
the inside of the reviews, because we'll be analyzing
the different words of the review to understand the correlations
between the presence of the words
and the result, whether the review is positive or negative.
So since we'll drill into the review and analyze its content well,
we must not specify the review as factors as if it was a single entity,
because that's what a factor would be, a single entity having a single meaning,
regardless of the different meanings of the different words of the review.
And so to prevent from identifying those reviews as factors,
well, what we need to do
is add this other parameter, which is the string as factors parameter.
Here it is. I just need to press enter.
And now we just need to input false like this.
Not in quotes.
And that's will not identify the reviews as factors.
And that's all.
That's how we should import this text file.
You know using the read import function to import a TSV file by default.
And then add this quote parameter to ignore the quotes and then add
the string as factors parameter to prevent from identifying the reviews as vectors.
All right. So let's do it.
Let's select this line of code and execute.
All right I'll get our data set as well.
Imported.
As you can see it has 1000 observations.
That means that the cut between the review column
and the liked column was done very properly without any issue.
So now let's open our data set and let's have a look.
As you can see, all the reviews are very well separated
to their verdict, whether it's a positive or negative review.
And so everything here looks great.
And we need to make sure that we have our 1000 reviews.
Well we can see that here very easily.
But you know we have our 1000 reviews.
And when I scroll up we can see that all the reviews are
well in the review column and all the liked results.
0 or 1 or well in the light column here.
You see, if I scroll up, we don't have any review in the light column
or a 1 or 0 in the review column.
So everything looks great.
We are ready to move on to the next step, which will be to clean
the different reviews.
That is a compulsory step
in natural language processing, which consists of cleaning the text
to make it ready for our future machine learning algorithms.
So that's what we'll do in the next tutorial.
And until then, enjoy machine learning.