-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (55 loc) · 2.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, render_template, request
import pandas as pd
import re
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import random
import json
app = Flask(__name__)
# Load the JSON file
json_file_path = 'AnimeQuotes.json' # Update this with your actual file path
with open(json_file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Convert the JSON data to a DataFrame
df = pd.DataFrame(data)
# Preprocess the text data
def preprocess_text(text):
text = text.lower() # Convert to lowercase
text = re.sub(r'[^\w\s]', '', text) # Remove punctuation
return text
df['processed_quotes'] = df['Quote'].apply(preprocess_text)
# Initialize the VADER sentiment analyzer
analyzer = SentimentIntensityAnalyzer()
# Function to get sentiment scores
def get_sentiment(text):
return analyzer.polarity_scores(text)
# Apply sentiment analysis
df['sentiment'] = df['processed_quotes'].apply(get_sentiment)
# Extract compound score for simplicity
df['compound'] = df['sentiment'].apply(lambda x: x['compound'])
# Classify sentiment
def classify_sentiment(compound):
if compound >= 0.05:
return 'positive'
elif compound <= -0.05:
return 'negative'
else:
return 'neutral'
df['sentiment_class'] = df['compound'].apply(classify_sentiment)
# Function to get quotes based on mood
def get_quotes_by_mood(mood, num_quotes=1): # Adjusted to return 1 quote by default
if mood not in ['positive', 'negative', 'neutral']:
raise ValueError("Mood must be 'positive', 'negative', or 'neutral'.")
filtered_df = df[df['sentiment_class'] == mood]
selected_quote = filtered_df.sample(n=num_quotes)
# Returning the quote, character, and anime
return selected_quote[['Quote', 'Character', 'Anime']].to_dict(orient='records')
@app.route('/', methods=['GET', 'POST'])
def index():
mood = None
quotes = []
if request.method == 'POST':
mood = request.form.get('mood')
quotes = get_quotes_by_mood(mood)
return render_template('index.html', mood=mood, quotes=quotes)
if __name__ == '__main__':
app.run(debug=True)