Lab File Complete
Lab File Complete
1|Page
PROGRAM 1
if __name__ == '__main__':
2|Page
PROGRAM 2
import re
# initializing string
test_str = "KCNIT, is best : for ! students ;"
# printing result
print("the string after punctuation filter : " + res)
Output-
The original string is : KCNIT, is best : for ! students ;
The string after punctuation filter : KCNIT is best for students
3|Page
PROGRAM 3
4|Page
PROGRAM 4
# this function is used to initialize the dictionary elements with a #default value.
from collections import defaultdict
# jug1 and jug2 contain the value for max capacity in respective jugs
# and aim is the amount of water to be measured.
jug1, jug2, aim = 4, 3, 2
# initialize dictionary with default value as false.
visited = defaultdict(lambda: false)
# recursive function which prints the intermediate steps to reach the final solution and return boolean
# value (true if solution is# possible, otherwise false). amt1 and amt2 are the amount of
# water present in both jugs at a certain point of time.
5|Page
PROGRAM 5
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)
# converts the words in word_tokens to lower case and then checks whether
#they are present in stop_words or not
filtered_sentence = [w for w in word_tokens if not w.lower() in stop_words]
#with no lower case conversion
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)
Output:
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing',
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
6|Page
PROGRAM 6
Output:
[(KCNIT, 'NNP'),
('is', 'VBZ'),
('a', 'DT'),
('Computer', 'NNP'),
('Science', 'NNP'),
('platform', 'NN'),
('.', '.')]
7|Page
PROGRAM 7
# sample text
text = "nltk is a powerful library for natural language processing."
Output:
Original Text:
NLTK is a powerful library for natural language processing.
PoS Tagging Result:
NLTK: NNP
is: VBZ
a: DT
powerful: JJ
library: NN
for: IN
natural: JJ
language: NN
processing: NN
.: .
8|Page
PROGRAM 8
import random
import words
def hangman():
# randomly chose a word form the list of words.
chosen_word = get_word()
# keep an empty list that is used to display the letters
# and empty spaces in a word that user must guess.
display = []
for _ in chosen_word:
display += "_"
9|Page
if c == guess:
display[index] = guess
index += 1
# check if he has run out of lives, then he has lost the game.
if lives == 0:
print("You Lose")
print(f"The word was: {chosen_word}")
end_of_loop = True
# The loop that will keep calling the game play function again and again unless the user does not want
#to play it anymore.
end_of_game = False
while not end_of_game:
# ask user if he wants to play the game
ask = input("Do you want to play Hangman? (y/n): ").lower()
# if he insert yes, then call the function for playing the game.
if ask == 'y' or ask == 'yes':
hangman()
# if the answer is no, quit the loop and end the program.
elif ask == 'n' or ask == 'no':
print("Program Exit Successful")
end_of_game = True
# if the user entered something else, ask again.
else:
print("Your given input could not be processed.")
print("Please enter a correct input.")
10 | P a g e