Can you suggest a python code for a simple sentiment project?
- User
Can you suggest a python code for a simple sentiment project?
Assistant
Here's a simple Python sentiment analysis project using the NLTK library:
import nltk from nltk.sentiment import SentimentIntensityAnalyzer import pandas as pd # Download necessary NLTK data nltk.download('vader_lexicon') def analyze_sentiment(text): sia = SentimentIntensityAnalyzer() score = sia.polarity_scores(text) # Determine sentiment based on compound score if score['compound'] >= 0.05: return 'Positive' elif score['compound'] <= -0.05: return 'Negative' else: return 'Neutral' # Example usage if __name__ == "__main__": # Sample texts texts = [ "I absolutely loved the movie! The acting was superb.", "The service at this restaurant was terrible and the food was cold.", "The weather is okay today, nothing special." ] # Analyze and display results results = [] for text in texts: sentiment = analyze_sentiment(text) score = SentimentIntensityAnalyzer().polarity_scores(text) results.append({ 'text': text, 'sentiment': sentiment, 'score': score['compound'] }) # Create a DataFrame for better visualization df = pd.DataFrame(results) print(df)Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.