Getting Started with Natural Language Processing in Python

article
#1

Originally published at: https://www.sitepoint.com/natural-language-processing-python/

A significant portion of the data that is generated today is unstructured. Unstructured data includes social media comments, browsing history and customer feedback. Have you found yourself in a situation with a bunch of textual data to analyse, and no idea how to proceed?

The objective of this tutorial is to enable you to analyze textual data in Python through the concepts of Natural Language Processing (NLP). You will first learn how to tokenize your text into smaller chunks, normalize words to their root forms, and then, remove any noise in your documents to prepare them for further analysis.

Let's get started!

Prerequisites

In this tutorial, we will use Python's nltk library to perform all NLP operations on the text. At the time of writing this tutorial, we used version 3.4 of nltk. To install the library, you can use the pip command on the terminal:

pip install nltk==3.4

To check which version of nltk you have in the system, you can import the library into the Python interpreter and check the version:

import nltk
print(nltk.__version__)

To perform certain actions within nltk in this tutorial, you may have to download specific resources. We will describe each resource as and when required.

However, if you would like to avoid downloading individual resources later in the tutorial and grab them now in one go, run the following command:

python -m nltk.downloader all

Step 1: Convert into Tokens

A computer system can not find meaning in natural language by itself. The first step in processing natural language is to convert the original text into tokens. A token is a combination of continuous characters, with some meaning. It is up to you to decide how to break a sentence into tokens. For instance, an easy method is to split a sentence by whitespace to break it into individual words.

0 Likes

#2

Step 1 describes syntactical recognition, right?

Does Step 2 apply to all words or just to verbs? For verbs you are describing verb tense, right?

For Step 3, punctuation is sometimes useful, correct? I suppose it depends on the extent to which we want to understand the words. Some words might be just noise or might be important. The word “the” might be considered noise but there is a difference between “a suspect” (usually a general comment) and “the suspect” (usually specific).

0 Likes

#3

Hi Samuel,

For step 1, I would say that syntactical recognition can be achieved through tokenization.

In step 2, the example definitely works around verbs and nouns, though you can modify it to get the base form of any word.

In step 3, you should consider what is the objective of your analysis - you may want to retain stop words and punctuation depending on the use case. Further, as you have pointed out, you may want to group words together too - “a suspect” could be a separate entity from “the suspect”!

0 Likes

#4

I think that if someone is attempting to parse words grammatically to understand what is said as best as possible then verbs can be really complicated. Verbs can have a tense (time), number (singular or plural) and a type of the subject.

0 Likes

#5

True, it does pose a challenge. While writing this article, what I had in mind was something like social media comments where the idea is to parse the text and try to assess its meaning.

0 Likes

#6

Good intro but it would’ve been nice if the example showed how you can use NLP to determine predictive intent(i.e. what does the customer want ? balance status, order status, make a payment etc) when they call up and don’t want to deal with the typical press 1 if you want x press 2 if you want y press 3 if you want z till they drill down to an option that can help them. People most of the time just press 0 to connect with a human and that slows down the entire call center.

0 Likes

#7

Hi,

Covering advanced topics would have made this article difficult to comprehend for beginners in NLP. The intent from text can be predicted as a next step by analyzing the word associations in language.

0 Likes

#8

It is not a matter of advanced, it is more of a matter of complex. Natural Language is complicated. People do not communicate using simple combinations of letters and words. NLP is by definition not simple. This article is actually not about NLP; more specifically, this article has very minimal or no recognition of grammar. Recognition of the fundamentals of grammar is not advanced NLP.

0 Likes