How to generate N-grams in Python

N-grams are all possible combinations of “N” words from the text. When two words are combined at a time, they are known as Bigrams, when three words are combined at a time, they are known as Trigrams, so on and so forth. They are very useful when we are trying to do NLP because combinations of words are more meaningful as compared to individual words.

There are two ways to generate N-grams, either by writing the logic yourself or by using the nltk library function.

Sample Output

Generating N-grams in Python
Generating N-grams in Python


Sample Output

Generate N-grams using nltk in Python
Generate N-grams using nltk in Python

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *