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