0% found this document useful (0 votes)
2 views3 pages

SL-Week0607 - Generative AI - Python

The document outlines four problems related to text processing in Python: sentiment analysis using the TextBlob library, text generation with the transformers library, text summarization also using transformers, and creating a simple chatbot with predefined responses. Each problem includes installation instructions, code snippets, and examples demonstrating how to implement the solutions. The document serves as a practical guide for utilizing Python libraries for various text-related tasks.

Uploaded by

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

SL-Week0607 - Generative AI - Python

The document outlines four problems related to text processing in Python: sentiment analysis using the TextBlob library, text generation with the transformers library, text summarization also using transformers, and creating a simple chatbot with predefined responses. Each problem includes installation instructions, code snippets, and examples demonstrating how to implement the solutions. The document serves as a practical guide for utilizing Python libraries for various text-related tasks.

Uploaded by

ahmedabad19553
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Problem #1: Text sentiment

• You can determine the sentiment of a text in Python using the TextBlob library,
which provides a simple way to analyze text sentiment as "positive," "neutral,"
or "negative.”

!pip install textblob


from textblob import TextBlob
def analyze_sentiment(text):
# Create a TextBlob object
blob = TextBlob(text)
# Get the sentiment polarity (-1 to 1)
polarity = [Link]
# Determine the sentiment
if polarity > 0:
return "Positive"
elif polarity == 0:
return "Neutral"
else:
return "Negative"
# Test the function
text = "I love this product! It's fantastic and works well."
sentiment = analyze_sentiment(text)
print(f"The sentiment of the text is: {sentiment}")

Problem #2: Text generation


Pip install transformers
from transformers import pipeline
# Load the text generation pipeline
text_generator = pipeline("text-generation")
# Provide a prompt
prompt = "Artificial intelligence is transforming the way we"
# Generate text
generated_text = text_generator(prompt, max_length=50)
# Print the generated text
print("Generated Text:")
print(generated_text)

problem 3: Text Summarization


Pip install transformers
from transformers import pipeline
# Load the summarization pipeline
summarizer = pipeline("summarization")
# Input text (example: a long paragraph or article)
text = """
Artificial intelligence is a branch of computer science that aims to create systems or
machines capable of performing tasks that typically require human intelligence.
These tasks include learning, reasoning, problem-solving, understanding language,
and even perceiving the environment. In recent years, AI has made significant
strides, particularly in areas like natural language processing, computer vision, and
robotics.
"""
# Generate a summary
summary = summarizer(text, max_length=60)
# Print the summary
print(summary)
problem 4: simple chatbot

import random

# Predefined responses
responses = {
"hello": ["Hi there!", "Hello!", "Hey! How can I help you?"],
"how are you": ["I'm just a program, but I'm doing fine! How about you?", "I'm
good! What about you?"],
"bye": ["Goodbye!", "See you later!", "Take care!"],
"default": ["I'm sorry, I don't understand that. Can you rephrase?"]
}

def chatbot_response(user_input):
user_input = user_input.lower()
for key in responses:
if key in user_input:
return [Link](responses[key])
return [Link](responses["default"])
# Chat loop
print("Chatbot: Hi! I'm your chatbot. Type 'bye' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print("Chatbot: Goodbye!")
break
response = chatbot_response(user_input)
print("Chatbot:", {response})

You might also like