In order to install NLTK, we must have Python installed on our computers. You can go to the link www.python.org/downloads and select the latest version for your OS i.e. Windows, Mac and Linux/Unix. For basic tutorial on Python you can refer to the link www.howcodex.com/python3/index.htm.
Now, once you have Python installed on your computer system, let us understand how we can install NLTK.
We can install NLTK on various OS as follows −
In order to install NLTK on Windows OS, follow the below steps −
First, open the Windows command prompt and navigate to the location of the pip folder.
Next, enter the following command to install NLTK −
pip3 install nltk
Now, open the PythonShell from Windows Start Menu and type the following command in order to verify NLTK’s installation −
Import nltk
If you get no error, you have successfully installed NLTK on your Windows OS having Python3.
In order to install NLTK on Mac/Linux OS, write the following command −
sudo pip install -U nltk
If you don’t have pip installed on your computer, then follow the instruction given below to first install pip −
First, update the package index by following using following command −
sudo apt update
Now, type the following command to install pip for python 3 −
sudo apt install python3-pip
In order to install NLTK through Anaconda, follow the below steps −
First, to install Anaconda, go to the link www.anaconda.com/distribution/#download-section and then select the version of Python you need to install.
Once you have Anaconda on your computer system, go to its command prompt and write the following command −
conda install -c anaconda nltk
You need to review the output and enter ‘yes’. NLTK will be downloaded and installed in your Anaconda package.
Now we have NLTK installed on our computers but in order to use it we need to download the datasets (corpus) available in it. Some of the important datasets available are stpwords, guntenberg, framenet_v15 and so on.
With the help of following commands, we can download all the NLTK datasets −
import nltk nltk.download()
You will get the following NLTK downloaded window.
Now, click on the download button to download the datasets.
Following is the example in which we are implementing Porter Stemmer algorithm by using PorterStemmer nltk class. with this example you would be able to understand how to run NLTK script.
First, we need to import the natural language toolkit(nltk).
import nltk
Now, import the PorterStemmer class to implement the Porter Stemmer algorithm.
from nltk.stem import PorterStemmer
Next, create an instance of Porter Stemmer class as follows −
word_stemmer = PorterStemmer()
Now, input the word you want to stem. −
word_stemmer.stem('writing')
'write'
word_stemmer.stem('eating')
'eat'