import nltk from nltk.util import ngrams from nltk.tokenize import word_tokenize # Download required NLTK data files nltk.download('punkt') def generate_ngrams(text, n): # Tokenize the text into words tokens = word_tokenize(text) # Generate n-grams n_grams = ngrams(tokens, n) return list(n_grams) # Example text text = "Natural language processing with Python is fun and educational." # Generate bigrams (n=2) bigrams = generate_ngrams(text, 2) print("Bigrams:", bigrams)