0% found this document useful (0 votes)
7 views

AI Lab file pdf

notes

Uploaded by

Jatin Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AI Lab file pdf

notes

Uploaded by

Jatin Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Sr. No.

Experiments Signature

01 Write a python program to implement Water Jug Problem.

02 Write a program to implement Hangman game using python.

Write a python program to implement stemming for a given sentence using


03
NLTK.

04 Write a python program to implement Lemmatization using NLTK.

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.

Requirement: Python 3.x

Code:

from collections import deque

def water_jug_solver(jug1, jug2, target):


visited = set()
queue = deque([(0, 0)])

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))
])

print("Target not achievable.")


return False

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.

Requirement: Python 3.x

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

if "_" not in guessed_word:


print("You won! The word was:", word)
return
else:
attempts -= 1
print(f"Incorrect! Attempts left: {attempts}")

print("Game over! The word was:", word)


hangman()
Output:

Welcome to Hangman!

Word: _ _ _ _ _ _ _

Guess a letter: p

You won! The word was: python

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.

Requirement: Python 3.x, NLTK library

Code:

from nltk.stem import PorterStemmer


from nltk.tokenize import word_tokenize

sentence = "I am loving the natural language processing tasks"


ps = PorterStemmer()

words = word_tokenize(sentence)

print("Original Sentence:", sentence)


print("Stemmed Words:", [ps.stem(word) for word in words])

Output:

Original Sentence: I am loving the natural language processing tasks


Stemmed Words: ['I', 'am', 'love', 'the', 'natur', 'languag', 'process',
'task']

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.

Requirement: Python 3.x, NLTK library

Code:

from nltk.stem import WordNetLemmatizer


from nltk.tokenize import word_tokenize

sentence = "The leaves on the trees are falling"


lemmatizer = WordNetLemmatizer()

words = word_tokenize(sentence)

print("Original Sentence:", sentence)


print("Lemmatized Words:", [lemmatizer.lemmatize(word) for word
in words])

Output:

Original Sentence: The leaves on the trees are falling


Lemmatized Words: ['The', 'leaf', 'on', 'the', 'tree', 'are', 'falling']

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.

Requirement: Python 3.x, NLTK library

Code:

from nltk.tokenize import word_tokenize


from nltk.classify import NaiveBayesClassifier

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)

sentence = "I love this place"


features = {word: True for word in word_tokenize(sentence)}

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.

You might also like