ESE begin 27 April 2026. View Timetable
Logo

Experiment 4

Implement N-Gram (Bigram) model.

Objective: To understand text preprocessing techniques including tokenization, stop word removal, and script validation using NLTK.


Prerequisites

Install NLTK

Open your terminal or command prompt and run: pip install nltk

Perform

  1. Open your text editor or IDE (IDLE, VS Code, etc.).
  2. Create a new file named exp2.py.
  3. Paste the code below.
  4. Run the script.

Code

from nltk.util import ngrams

sentence = input("Enter the sentence: ")
tokens = sentence.split()
n_grams = int(input("Enter 'n': "))

output = list(ngrams(tokens, n_grams))
print(output)
ColabOpen Colab

On this page