You are on page 1of 17

KRISHNA ENGINEERING COLLEGE

(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


INDEX
Student Roll Number:2101611520039 Student name:Rudra Tyagi
Subject name: Artificial Intelligence Lab Subject code: KAI-551

Impleme-
Practical Output Viva Total
Practical name Date ntation Signature
No. (5 MM) (5 MM) (20 MM)
(10 MM)
Write a python program to
1 implement Breadth First
Search Traversal?
Write a python program to
2 implement Water Jug
Problem?
Write a python program to
3 remove punctuations from the
given string?
Write a python program to sort
4 the sentence in alphabetical
order?
Write a program to implement
5 Hangman game using python.
Write a program to implement
6 Tic-Tac-Toe game using
python.
Write a python program to
remove stop words for a given
7 passage from a text file using
NLTK?
Write a python program to
implement stemming for a
8 given sentence using NLTK?

Write a python program to


POS (Parts of Speech)
9 tagging for the give sentence
using NLTK?
Write a python program to
10 implement Lemmatization
using NLTK?
Write a python program to for
11 Text Classification for the give
sentence using NLTK.

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 1:
Name of the practical :- WAP to implement Breadth First Search Traversal.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

graph = {'A': set(['F', 'C', 'B']),


'B': set(['A', 'C', 'G']),
'C': set(['A', 'B', 'D', 'E', 'F' ,'G']),
'D': set(['C', 'F', 'E', 'J']),
'E': set(['C', 'D', 'G', 'J', 'K']),
'F': set(['A', 'C', 'D']),
'G': set(['B', 'C', 'E', 'K']),
'J': set(['D', 'E', 'K']),
'K': set(['E', 'G', 'J'])}
#print(graph)
visited=[] # initialize empty list for visited node
queue=[] #initialize empty list for QUEUE node
def bfs(visited,graph,node):
visited.append(node)
queue.append(node)
while queue:
s=queue.pop(0)
print(s,end=" ")
for neighbour in graph[s]: #Graph[s] stores pop nodes of queue
if neighbour not in visited:
visited.append(neighbour) #if neighbour not in visited THEN add in visited list
queue.append(neighbour) #if neighbour not in visited THEN add in QUEUE list
print("Following is the Breadth-First Search")
bfs(visited,graph,'B') #CALL function bfs

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 2:
Name of the practical :- WAP to implement Water Jug Problem.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

from collections import defaultdict


jug1, jug2, aim = 4, 3, 2
visited = defaultdict(lambda: False)
def waterJugSolver(amt1, amt2):
if (amt1 == aim and amt2 == 0) or (amt2 == aim and amt1 == 0):
print(amt1, amt2)
return True
if visited[(amt1, amt2)] == False:
print(amt1, amt2)

visited[(amt1, amt2)] = True


return (waterJugSolver(0, amt2) or
waterJugSolver(amt1, 0) or
waterJugSolver(jug1, amt2) or
waterJugSolver(amt1, jug2) or
waterJugSolver(amt1 + min(amt2, (jug1-amt1)),
amt2 - min(amt2, (jug1-amt1))) or
waterJugSolver(amt1 - min(amt1, (jug2-amt2)),
amt2 + min(amt1, (jug2-amt2))))
else:
return False
print("Steps: ")
waterJugSolver(0, 0)

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 3:
Name of the practical :- WAP to remove punctuations from the given string.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

punctuations = '''./?@#$%^-[]{}&*_~!();:'"\,<>''' #PUNCTUATION LIST


str=input("Enter String") #INPUT STRING BY USER
no_punctuation="" #EMPTY STRING
for char in str: #FOR EACH CHARACTER IN INPUT STR IF THERE IS PUNCTUATION
MARK OR NOT
if char not in punctuations:
no_punctuation=no_punctuation+char
print( no_punctuation)

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 4:
Name of the practical :- WAP to sort the sentence in alphabetical order.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

sentence=input(" Enter a Sentence that is to be sorted in alphabetical order ")


words = [word.upper() for word in sentence.split()] #change sentence into upper
case
words.sort() #sort()
for word in words:

print(word)

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 5:
Name of the practical :- WAP to implement Hangman game using python.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

import random
import time

WORDS = ("python", "jumble", "easy","tree","stocks","internet","bharat", "difficult",


"answer", "xylophone")
word = random.choice(WORDS)
attempts=5
guesses =" "
Player=input("Enter your Name Here : ")
print("Hello "+Player,", Ready to play Hangman")
time.sleep(1)
print("Start Guessing....")
time.sleep(3)
while attempts>0:
failattempt=0
for char in word:
if char in guesses:
print(char)
else:
print("_")
failattempt+=1

if failattempt==0:
print("Fetching Your Results...")
time.sleep(5)
print("HURRAH! You Won the Game")
break
guess=input("Enter Character Here : ")
guesses+=guess
#if attempts==2:
# print("You are not doing well guessess. we providing a list of words that makes you
easy to guess. ")
#print(WORDS)
if guess not in word:
attempts-=1
print("Wrong Guess")
print(Player+","+"You have Only",+attempts,"Attempts to Win or Lose the Game")
if attempts==0:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


print("Fetching Your Results...")
time.sleep(5)
print("OHH! You Lose the Game")

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 6:
Name of the practical :- WAP to implement Tic-Tac-Toe game using python.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

import numpy as np
import random
from time import sleep
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))

def possibilities(board):
l = []

for i in range(len(board)):
for j in range(len(board)):

if board[i][j] == 0:
l.append((i, j))
return(l)

def random_place(board, player):


selection = possibilities(board)
current_loc = random.choice(selection)
board[current_loc] = player
return(board)

def row_win(board, player):


for x in range(len(board)):
win = True

for y in range(len(board)):
if board[x, y] != player:
win = False
continue

if win == True:
return(win)
return(win)
def col_win(board, player):
for x in range(len(board)):
win = True

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


for y in range(len(board)):
if board[y][x] != player:
win = False
continue

if win == True:
return(win)
return(win)

def diag_win(board, player):


win = True
y = 0
for x in range(len(board)):
if board[x, x] != player:
win = False
if win:
return win
win = True
if win:
for x in range(len(board)):
y = len(board) - 1 - x
if board[x, y] != player:
win = False
return win

def evaluate(board):
winner = 0

for player in [1, 2]:


if (row_win(board, player) or
col_win(board, player) or
diag_win(board, player)):

winner = player

if np.all(board != 0) and winner == 0:


winner = -1
return winner

# Main function
def play_game():
board, winner, counter = create_board(), 0, 1
print(board)
sleep(2)

while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
Faculty Remarks & Signature
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)
print("Winner is: " + str(play_game()))

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 7:
Name of the practical :- WAP to remove stop words for a given passage from a text file using NLTK.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

from nltk.corpus import stopwords


from nltk.tokenize import word_tokenize
example_sent = """This is a sample sentence,showing off the stop words
filtration."""
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)
filtered_sentence = [w for w in word_tokens if not w.lower() in stop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 8:
Name of the practical :- WAP to implement stemming for a given sentence using NLTK.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

# import these modules


from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize

ps = PorterStemmer()

# choose some words to be stemmed


words = ["program", "programs", "programmer", "programming", "programmers"]

for w in words:
print(w, " : ", ps.stem(w))

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 9:
Name of the practical :- WAP to implement Parts of Speech tagging for sentence using NLTK.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

import nltk
nltk.download('tagsets')
nltk.download('punkt')
text1 = "Python is a high-level, general-purpose programming language. Its des
print(text1)
words1 = nltk.word_tokenize(text1)
print(words1)
nltk.download('averaged_perceptron_tagger')
nltk.pos_tag(words1)
nltk.help.upenn_tagset("VBZ")

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 10:
Name of the practical :- WAP to implement Lemmatization using NLTK.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

from nltk.stem import WordNetLemmatizer


wnl=WordNetLemmatizer()
word="better"
print(wnl.lemmatize(word))
print(wnl.lemmatize(word,pos="n"))
print(wnl.lemmatize(word,pos="a"))
print(wnl.lemmatize(word,pos="v"))
print(wnl.lemmatize(word,pos="r"))

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


Program- 11:
Name of the practical :- WAP to implement for Text Classification for the sentence using NLTK.
Student Roll no :- 2101611520039
Student Name :- Rudra Tyagi
Semester/Batch :- 5th semester (2023-2024)

Code:

import nltk
import random
nltk.download('movie_reviews')
from nltk.corpus import movie_reviews
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
random.shuffle(documents)
print(documents[1])
all_words = []
for w in movie_reviews.words():
all_words.append(w.lower())
all_words = nltk.FreqDist(all_words)
print(all_words.most_common(15))
print(all_words["stupid"])

Output:

[nltk_data] Downloading package movie_reviews to /root/nltk_data...


[nltk_data] Package movie_reviews is already up-to-date!

<CategorizedPlaintextCorpusReader in '/root/nltk_data/corpora/movie_reviews'>
(['the', 'thirteenth', 'floor', ',', 'the', 'third', 'in', 'what', 'i', 'would',
'call', '"', 'the', 'reality', 'check', 'movie', 'series', '"', ',', 'is', 'very',
'similar', 'to', 'the', 'other', 'reality', 'check', 'movies', 'released', 'this',
'year', ',', 'the', 'matrix', 'and', 'existenz', '.', 'all', 'three', 'made',
'you', 'think', ',', 'made', 'you', 'wonder', 'what', 'is', 'real', ',', 'what',
'isn', "'", 't', ',', 'and', 'if', 'our', 'world', 'is', 'just', 'a', 'huge',
'game', '.', 'the', 'thirteenth', 'floor', 'doesn', "'", 't', 'reach', 'the',
'level', 'of', 'originality', ',', 'creativity', ',', 'and', 'curiosity',
'sparked', 'by', 'the', 'matrix', 'and', 'existenz', ',', 'but', 'it', 'certainly',
'gives', 'a', 'great', 'shot', 'at', 'it', '.', 'in', 'the', 'matrix', ',', 'we',
'were', 'told', 'that', 'the', 'humans', 'are', 'simply', 'a', 'virus', '.', 'in',
'existenz', ',', 'we', 'learned', 'that', 'our', 'life', 'could', 'be', 'just',
'a', 'game', '.', 'in', 'the', 'thirteenth', 'floor', ',', 'we', 'learn', 'that',
'we', 'are', 'just', 'electronic', 'devices', ',', 'living', 'under', 'another',
Faculty Remarks & Signature
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


'world', 'of', 'electronic', 'devices', '.', 'there', 'is', 'just', 'one', 'world',
'on', 'top', 'of', 'another', ',', 'and', 'everything', 'in', 'them', 'are',
'fake', 'and', 'electronically', 'generated', '.', 'the', 'thirteenth', 'floor',
'took', 'a', 'huge', ',', 'risky', 'turn', 'that', 'i', 'didn', "'", 't', 'expect',
'it', 'to', 'take', ',', 'and', 'i', "'", 'm', 'not', 'very', 'sure', 'it', 'was',
'such', 'a', 'great', 'turn', 'to', 'make', '.', 'rather', 'than', 'focusing',
'just', 'on', 'the', 'reality', 'parts', 'of', 'the', 'film', ',', 'the',
'thirteenth', 'floor', 'becomes', 'a', 'murder', 'mystery', 'that', 'ends', 'up',
'tying', 'in', 'with', 'the', 'different', 'worlds', '.', 'after', 'the',
'mysterious', 'murder', 'of', 'computer', 'program', 'designer', 'hammond',
'fuller', ',', 'played', 'by', 'armin', 'mueller', '-', 'stahl', ',', 'douglas',
'hall', ',', 'played', 'by', 'craig', 'bierko', ',', 'a', 'man', 'that', 'worked',
'under', 'fuller', 'for', 'many', 'years', ',', 'must', 'travel', 'through', 'an',
'electronic', 'computer', 'device', 'that', 'fuller', 'was', 'using', ',', 'to',
'the', 'year', '1937', ',', 'which', 'consists', 'of', 'computer', 'generated',
'characters', 'only', '.', 'the', 'simulation', 'of', '1937', 'is', 'just', 'like',
'it', 'was', 'back', 'in', 'the', 'olden', 'days', '.', 'all', 'of', 'the',
'people', 'involved', 'are', 'just', 'characters', ',', 'or', 'are', 'they', '?',
'douglas', 'strongly', 'believes', 'that', 'a', 'character', 'traveled', 'through',
'the', 'transport', 'from', 'their', 'world', 'to', 'ours', ',', 'and', 'killed',
'fuller', '.', 'along', 'the', 'way', ',', 'douglas', 'interacts', 'with', 'many',
'different', 'potential', 'suspects', ',', 'and', 'a', 'woman', 'who', 'claims',
'to', 'be', 'the', 'daughter', 'of', 'fuller', ',', 'jane', 'fuller', ',',
'played', 'wonderfully', 'by', 'a', 'talented', 'young', 'actress', ',',
'gretchen', 'mol', '.', 'the', 'thirteenth', 'floor', 'is', 'a', 'plot', 'driven',
'movie', 'from', 'the', 'time', 'the', 'film', 'gets', 'going', ',', 'and',
'doesn', "'", 't', 'use', 'spectacular', 'special', 'effects', 'and', 'big',
'sound', 'to', 'keep', 'the', 'viewer', "'", 's', 'interests', '.', 'i', 'was',
'worried', 'that', 'this', 'film', 'would', 'be', 'too', 'much', 'like', 'the',
'matrix', 'and', 'existenz', ',', 'and', 'i', 'wouldn', "'", 't', 'enjoy', 'it',
',', 'but', 'there', 'were', 'enough', 'twists', 'and', 'turns', 'to', 'keep',
'me', 'thinking', 'and', 'attentive', 'to', 'the', 'film', '.', 'unfortunately',
',', 'many', 'potential', 'would', 'have', 'been', 'great', 'scenes', 'were',
'thrown', 'away', 'with', 'predictable', 'content', ',', 'letting', 'the',
'viewer', 'know', 'the', 'outcome', 'of', 'the', 'scene', 'long', 'before', 'it',
'happened', '.', 'the', 'scenes', 'that', 'aren', "'", 't', 'original', 'or',
'interesting', 'seem', 'to', 'go', 'absolutely', 'nowhere', ',', 'leaving', 'you',
'wondering', ',', '"', 'what', 'is', 'the', 'point', 'of', 'this', '?', '"',
'sometimes', 'it', 'was', 'hard', 'to', 'believe', 'that', 'this', 'story', ',',
'with', 'terrible', 'dialogue', ',', 'some', 'bad', 'acting', ',', 'especially',
'from', 'craig', 'bierko', ',', 'and', 'scenes', 'that', 'go', 'absolutely',
'nowhere', ',', 'are', 'actually', 'true', '.', 'i', 'know', 'this', 'isn', "'",
't', 'a', 'true', 'story', 'or', 'anything', ',', 'but', 'a', 'film', 'should',
'be', 'able', 'to', 'make', 'you', 'think', 'that', 'it', 'is', '.', 'on', 'the',
'plus', 'side', 'of', 'acting', ',', 'we', 'get', 'to', 'see', 'the', 'missing',
'from', 'main', 'character', 'action', 'since', 'the', 'film', 'masterpiece',
'shine', 'in', '1995', 'actor', ',', 'armin', 'mueller', '-', 'stahl', '.', 'in',
'the', 'thirteenth', 'floor', ',', 'he', 'is', 'back', 'and', 'is', 'still',
'giving', 'believable', 'and', 'amazing', 'performances', '.', 'another', 'plus',
'is', 'gretchen', 'mol', "'", 's', 'realistic', 'performance', ',', 'creating',
'her', 'character', 'with', 'depth', ',', 'not', 'just', 'staying', 'in', 'the',
'one', '-', 'dimensional', 'phase', '.', 'don', "'", 't', 'expect', 'the',
'intensity', 'of', 'the', 'matrix', 'or', 'existenz', 'to', 'come', 'out', 'of',
'the', 'thirteenth', 'floor', ',', 'just', 'expect', 'another', 'film', 'that',
'messes', 'with', 'your', 'mind', 'for', 'awhile', ',', 'and', 'shuts', 'you',

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of CSE (Artificial Intelligence)


'down', 'like', 'any', 'other', 'movie', 'would', '.', 'thrills', ',', 'chills',
',', 'and', 'spills', 'aren', "'", 't', 'what', 'you', 'will', 'get', 'in', 'this',
'film', ',', 'but', 'you', 'will', 'just', 'get', 'another', 'trip', 'to', 'send',
'your', 'mind', 'on', ',', 'scrambling', 'it', 'until', 'you', "'", 've', 'had',
'enough', '.', 'the', 'bottom', 'line', '-', 'let', "'", 's', 'hope', 'this', 'is',
'the', 'last', 'of', 'the', '"', 'reality', 'check', '"', 'based', 'movies', 'for',
'awhile', '.'], 'pos')
[(',', 77717), ('the', 76529), ('.', 65876), ('a', 38106), ('and', 35576), ('of',
34123), ('to', 31937), ("'", 30585), ('is', 25195), ('in', 21822), ('s', 18513),
('"', 17612), ('it', 16107), ('that', 15924), ('-', 15595)]
253

Faculty Remarks & Signature

You might also like