Spaces:
Runtime error
Runtime error
import matplotlib.pyplot as plt | |
import re | |
import numpy as np | |
from collections import defaultdict | |
def createGraph(resultPath): | |
# Initialize lists to store emotions sequentially | |
emotions_list = [] | |
emotion_counts = defaultdict(int) | |
emotion_scores = defaultdict(float) | |
# Define a regular expression pattern to match the emotion and score | |
pattern = re.compile(r"'emotion': '(\w+)', 'score': ([\d\.]+)") | |
# Read and parse the file | |
with open(resultPath, 'r') as file: | |
for line in file: | |
match = pattern.search(line) | |
if match: | |
emotion = match.group(1) | |
score = float(match.group(2)) | |
emotions_list.append(emotion) | |
emotion_counts[emotion] += 1 | |
emotion_scores[emotion] += score | |
else: | |
print(f"Skipping malformed line: {line.strip()}") | |
# Group emotions into positive, neutral, and negative categories | |
emotion_group_map = { | |
'happy': 'Positive', | |
'surprise': 'Positive', # Positive emotions | |
'neutral': 'Neutral', # Neutral emotions | |
'sad': 'Negative', | |
'angry': 'Negative', | |
'fear': 'Negative', | |
'disgust': 'Negative' # Negative emotions | |
} | |
# Aggregate counts for positive, neutral, and negative categories | |
grouped_counts = defaultdict(int) | |
for emotion in emotions_list: | |
grouped_counts[emotion_group_map[emotion]] += 1 | |
# Define the categories and their counts | |
categories = ['Positive', 'Neutral', 'Negative'] | |
counts = [grouped_counts[category] for category in categories] | |
# **Figure 1: Bar Chart for Positive, Neutral, and Negative Emotions** | |
plt.figure(figsize=(8, 6)) | |
plt.bar(categories, counts, color=['green', 'grey', 'red'], alpha=0.7) | |
plt.xlabel('Emotion Group') | |
plt.ylabel('Count') | |
plt.title('Distribution of Positive, Neutral, and Negative Emotions') | |
plt.grid(True) | |
# Save the bar plot | |
plt.savefig('data/output/emotion_bar_plot.png') | |
# **Figure 2: Stem Plot (unchanged from previous version)** | |
emotion_values = [1 if emotion_group_map[e] == 'Positive' else | |
0 if emotion_group_map[e] == 'Neutral' else | |
-1 for e in emotions_list] | |
# Prepare x-axis values (line numbers) | |
line_numbers = np.arange(1, len(emotion_values) + 1) | |
plt.figure(figsize=(12, 6)) | |
plt.plot(line_numbers, emotion_values, color='blue', linewidth=2, linestyle='-', marker='o', alpha=0.7) | |
for i, emotion in enumerate(emotions_list): | |
if emotion_group_map[emotion] == 'Positive': | |
plt.axvspan(i + 0.5, i + 1.5, color='green', alpha=0.3) | |
elif emotion_group_map[emotion] == 'Neutral': | |
plt.axvspan(i + 0.5, i + 1.5, color='grey', alpha=0.3) | |
elif emotion_group_map[emotion] == 'Negative': | |
plt.axvspan(i + 0.5, i + 1.5, color='red', alpha=0.3) | |
plt.xlabel('Line Number') | |
plt.ylabel('Emotion Group') | |
plt.title('Emotion Type Across the File (Stem Plot)') | |
plt.ylim([-2, 2]) | |
plt.grid(True) | |
plt.yticks([-1, 0, 1], ['Negative', 'Neutral', 'Positive']) | |
# Save the stem plot | |
plt.savefig('data/output/emotion_stem_plot.png') | |
# **Figure 3: Combination Bar and Line Chart for Counts and Scores (unchanged)** | |
emotions = list(emotion_counts.keys()) | |
counts = [emotion_counts[e] for e in emotions] | |
average_scores = [emotion_scores[e] / emotion_counts[e] for e in emotions] | |
fig, ax1 = plt.subplots() | |
ax1.bar(emotions, counts, color='b', alpha=0.7) | |
ax1.set_xlabel('Emotion') | |
ax1.set_ylabel('Count', color='b') | |
ax1.tick_params(axis='y', labelcolor='b') | |
ax2 = ax1.twinx() | |
ax2.plot(emotions, average_scores, color='r', marker='o') | |
ax2.set_ylabel('Average Score', color='r') | |
ax2.tick_params(axis='y', labelcolor='r') | |
plt.title('Emotion Distribution and Average Scores') | |
plt.savefig('data/output/emotionAVG.png') | |