Implement N-Grams using Python NLTK – A Step-By-Step Guide

Feautured Img Ngrams

In this tutorial, we will discuss what we mean by n-grams and how to implement n-grams in the Python programming language.

Also read: BLEU score in Python – Beginners Overview


Understanding N-grams

Text n-grams are commonly utilized in natural language processing and text mining. It’s essentially a string of words that appear in the same window at the same time.

When computing n-grams, you normally advance one word (although in more complex scenarios you can move n-words). N-grams are used for a variety of purposes.

N Grams Demonstration
N Grams Demonstration

For example, while creating language models, n-grams are utilized not only to create unigram models but also bigrams and trigrams.

Google and Microsoft have created web-scale grammar models that may be used for a variety of activities such as spelling correction, hyphenation, and text summarization.


Implementing n-grams in Python

In order to implement n-grams, ngrams function present in nltk is used which will perform all the n-gram operation.

from nltk import ngrams
sentence = input("Enter the sentence: ")
n = int(input("Enter the value of n: "))
n_grams = ngrams(sentence.split(), n)
for grams in n_grams:
    print(grams)

Sample Output

Enter the sentence: Let's test the n-grams implementation with this sample sentence! Yay!
Enter the value of n: 3
("Let's", 'test', 'the')
('test', 'the', 'n-grams')
('the', 'n-grams', 'implementation')
('n-grams', 'implementation', 'with')
('implementation', 'with', 'this')
('with', 'this', 'sample')
('this', 'sample', 'sentence!')
('sample', 'sentence!', 'Yay!')

See how amazing the results are! You can try out the same code for a number of sentences. Happy coding! 😇


Also Read:

  1. Stemming and Lemmatization in Python
  2. Creating Bag of Words Model from Scratch in python
  3. How to remove Stop Words in Python using NLTK?
  4. Word Cloud using Python