[go: up one dir, main page]

0% found this document useful (0 votes)
37 views11 pages

AI Lab Manual Aktu

Uploaded by

cluster1007
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)
37 views11 pages

AI Lab Manual Aktu

Uploaded by

cluster1007
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/ 11

Program 7

Object:- Write a python program to remove stop words for a given passage from a text file
using NLTK?
Source Code:-
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

# Ensure required NLTK packages are downloaded


nltk.download('punkt')
nltk.download('stopwords')

def remove_stopwords(file_path):
# Read the passage from the text file
try:
with open(file_path, 'r') as file:
text = file.read()
except FileNotFoundError:
print("The specified file does not exist.")
return

# Tokenize the text into words


words = word_tokenize(text)

# Get the list of stop words for English


stop_words = set(stopwords.words('english'))

# Remove stop words


filtered_words = [word for word in words if word.lower() not in stop_words]

14
# Join the filtered words into a cleaned passage
cleaned_text = ' '.join(filtered_words)

print("Original Passage:")
print(text)
print("\nCleaned Passage:")
print(cleaned_text)

# Specify the path to the input text file


file_path = 'input.txt' # Replace 'input.txt' with your file name
remove_stopwords(file_path)

Output:-

15
Program 8
Object:- Write a python program to implement stemming for a given sentence using NLTK?
Source Code:-
import nltk
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize

# Ensure required NLTK packages are downloaded


nltk.download('punkt')

def perform_stemming(sentence):
# Initialize the PorterStemmer
stemmer = PorterStemmer()

# Tokenize the sentence into words


words = word_tokenize(sentence)

# Apply stemming to each word


stemmed_words = [stemmer.stem(word) for word in words]

# Join the stemmed words into a sentence


stemmed_sentence = ' '.join(stemmed_words)

print("Original Sentence:")
print(sentence)
print("\nStemmed Sentence:")
print(stemmed_sentence)
# Input sentence
sentence = "The cats are running faster than the dogs, and they will be jumping again soon."
perform_stemming(sentence)

16
Output:-

17
Program 9
Object:- Write a python program to POS (Parts of Speech) tagging for the give sentence
using NLTK?
Source Code:-
import nltk

# Ensure required NLTK packages are downloaded


nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

def pos_tagging(sentence):
# Tokenize the sentence into words
words = nltk.word_tokenize(sentence)

# Perform POS tagging


pos_tags = nltk.pos_tag(words)

print("Parts of Speech Tags:")


for word, tag in pos_tags:
print(f"{word} -> {tag}")

# Input sentence
sentence = "The quick brown fox jumps over the lazy dog."
pos_tagging(sentence)

18
Output:-

19
Program 10
Object:- Write a python program to implement Lemmatization using NLTK?
Source Code:-
import nltk
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize

# Ensure required NLTK packages are downloaded


nltk.download('punkt')
nltk.download('wordnet')
nltk.download('omw-1.4')

def perform_lemmatization(sentence):
# Initialize the WordNetLemmatizer
lemmatizer = WordNetLemmatizer()

# Tokenize the sentence into words


words = word_tokenize(sentence)

# Apply lemmatization to each word


lemmatized_words = [lemmatizer.lemmatize(word) for word in words]

# Join the lemmatized words into a sentence


lemmatized_sentence = ' '.join(lemmatized_words)

print("Original Sentence:")
print(sentence)
print("\nLemmatized Sentence:")
print(lemmatized_sentence)

20
# Input sentence
sentence = "The leaves are falling from the trees and children are playing in the park."
perform_lemmatization(sentence)

Output:-

21
Program 11
Object:- Write a python program to for Text Classification for the give sentence using NLTK
Source Code:-
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import movie_reviews
from nltk.classify import NaiveBayesClassifier
from nltk.classify.util import accuracy
from nltk.corpus import stopwords

# Ensure required NLTK packages are downloaded


nltk.download('punkt')
nltk.download('stopwords')
nltk.download('movie_reviews')

# Feature extraction function


def extract_features(words):
stop_words = set(stopwords.words('english'))
return {word: True for word in words if word.lower() not in stop_words}

# Load the movie reviews dataset


def load_dataset():
positive_features = [(extract_features(movie_reviews.words(fileid)), 'Positive') for fileid in
movie_reviews.fileids('pos')]
negative_features = [(extract_features(movie_reviews.words(fileid)), 'Negative') for fileid
in movie_reviews.fileids('neg')]
return positive_features, negative_features

# Train the Naive Bayes Classifier


def train_classifier():
positive_features, negative_features = load_dataset()

22
train_data = positive_features[:800] + negative_features[:800]
test_data = positive_features[800:] + negative_features[800:]
classifier = NaiveBayesClassifier.train(train_data)
return classifier, test_data

# Classify a given sentence


def classify_sentence(classifier, sentence):
words = word_tokenize(sentence)
features = extract_features(words)
return classifier.classify(features)

# Main function
def main():
classifier, test_data = train_classifier()

# Test the classifier accuracy


print(f"Classifier Accuracy: {accuracy(classifier, test_data) * 100:.2f}%")

# Classify a sample sentence


sample_sentence = "The plot was dull and the acting was terrible."
classification = classify_sentence(classifier, sample_sentence)
print(f"Input Sentence: \"{sample_sentence}\"")
print(f"Classification: {classification}")

# Show top features


print("\nMost Informative Features:")
classifier.show_most_informative_features(10)

if __name__ == "__main__":
main()

23
Output:-

24

You might also like