You can download this code by clicking the button below.
This code is now available for download.
This function extracts n-grams of a specified length from a given text. N-grams are commonly used as features in natural language processing.
Technology Stack : Python
Code Type : Text processing
Code Difficulty : Intermediate
def extract_n_grams(text, n):
"""
Extracts n-grams from a given text.
Args:
text (str): The text from which to extract n-grams.
n (int): The size of the n-grams to extract.
Returns:
list: A list of n-grams extracted from the text.
"""
words = text.split()
n_grams = [' '.join(words[i:i+n]) for i in range(len(words) - n + 1)]
return n_grams