AI Lab file pdf
AI Lab file pdf
Experiments Signature
Write a python program to for Text Classification for the give sentence using
05
NLTK.
Experiment No. 01
Write a python program to implement Water Jug Problem.
About: The Water Jug Problem involves two jugs with fixed capacities and the goal is to
measure a specific quantity of water using the jugs. This problem is a classic example of
problem-solving in Artificial Intelligence.
Code:
while queue:
a, b = queue.popleft()
if (a, b) in visited:
continue
visited.add((a, b))
print(f"Jug1: {a}, Jug2: {b}")
if a == target or b == target:
print("Target achieved!")
return True
queue.extend([
(jug1, b), (a, jug2), (0, b), (a, 0),
(min(a + b, jug1), max(0, b - (jug1 - a))),
(max(0, a - (jug2 - b)), min(a + b, jug2))
])
water_jug_solver(4, 3, 2)
Output:
Jug1: 0, Jug2: 0
Jug1: 4, Jug2: 0
Jug1: 0, Jug2: 3
Jug1: 3, Jug2: 0
...
Target achieved!
Conclusion:
The Water Jug Problem demonstrates how a simple problem can be solved using
BFS, ensuring all states are explored efficiently.
Experiment No. 02
Write a program to implement Hangman game using python.
About: Hangman is a classic word-guessing game. The user should guess the word
correctly by entering alphabets of the user choice. The program will get input as single
alphabet from the user and it will matchmaking with the alphabets in the original word. If
the user given alphabet matches with the alphabet from the original word, then user must
guess the remaining alphabets. Once the user successfully found the word he will be
declared as winner otherwise user lose the game.
Code:
import random
def hangman():
words = ["python", "java", "hangman", "programming"]
word = random.choice(words)
guessed_word = ["_" for _ in word]
attempts = 6
print("Welcome to Hangman!")
while attempts > 0:
print("Word: ", " ".join(guessed_word))
guess = input("Guess a letter: ").lower()
if guess in word:
for i, letter in enumerate(word):
if letter == guess:
guessed_word[i] = guess
Welcome to Hangman!
Word: _ _ _ _ _ _ _
Guess a letter: p
Conclusion: The Hangman game is a fun way to understand loops and conditions in
Python while handling user inputs interactively.
Experiment No. 03
Write a python program to implement stemming for a given sentence using
NLTK.
About: Stemming is the process of reducing a word to its root form. It helps in text
preprocessing for Natural Language Processing tasks.
Code:
words = word_tokenize(sentence)
Output:
Conclusion:
Stemming reduces words to their base forms, which is useful in reducing vocabulary
size for text analysis.
Experiment No. 04
Write a python program to implement Lemmatization using NLTK.
About: Lemmatization is the process of reducing words to their base or dictionary form,
preserving meaning.
Code:
words = word_tokenize(sentence)
Output:
Conclusion:
Lemmatization provides the base form of words while preserving their context,
making it crucial for semantic analysis.
Experiment No. 05
Write a python program to for Text Classification for the give sentence
using NLTK.
About: Text classification categorizes text into predefined labels using machine learning
or rule-based approaches.
Code:
training_data = [
("I love this product", "Positive"),
("This is an amazing place", "Positive"),
("I hate this thing", "Negative"),
("This is not good", "Negative")
]
train_set = [
({word: True for word in word_tokenize(text)}, label)
for text, label in training_data
]
classifier = NaiveBayesClassifier.train(train_set)
print("Sentence:", sentence)
print("Classification:", classifier.classify(features))
Output:
Sentence: I love this place
Classification: Positive
Conclusion:
Text classification using NLTK provides a simple way to build and test
classifiers for analyzing sentiments or categories of text.