import matplotlib.pyplot as plt
from pythonosc import udp_client
# Paths to the emotion mapping file and input text file
mapping_file_path = 'C:/Users/Chris/PycharmProjects/SentimentAnalysis_Test/emotions.txt'
input_text_file_path = 'C:/Users/Chris/PycharmProjects/SentimentAnalysis_Test/read.txt'
categories = ['angel', 'hero', 'feminist', 'rebel']
# Color mapping for categories
color_mapping = {'angel': 'yellow', 'hero': 'darkblue', 'feminist': 'green', 'rebel': 'red'}
osc_client = udp_client.SimpleUDPClient("127.0.0.1", 9000)
# Function to load emotion mappings from a file
def load_emotion_mapping(file_path):
with open(file_path, 'r') as f:
parts = line.strip().split(':')
mapping[word.strip().lower()] = category.strip().lower()
# Function to load the input text from a file
def load_input_text(file_path):
with open(file_path, 'r') as f:
# Function to process the text and count emotions per category
def count_emotions(text, mapping, categories):
# Remove punctuation from text
text = text.translate(str.maketrans('', '', string.punctuation))
text_words = text.lower().split()
counts = {category: 0 for category in categories}
matched_words = {category: [] for category in categories}
matched_words[category].append(word)
return counts, matched, matched_words
# Random distribution generator
def generate_random_distribution(categories):
random_distribution = np.random.dirichlet(np.ones(len(categories)), size=1)[0]
return {categories[i]: random_distribution[i] * 100 for i in range(len(categories))}
# Plotting function with adjusted annotation position
def plot_emotion_distribution(counts, categories, no_matches, matched_words):
plt.clf() # Clear the previous plot
values = [counts.get(category, 0) for category in categories]
colors = [color_mapping[category] for category in categories] # Assigning colors based on category
plt.bar(categories, values, color=colors) # Using assigned colors
plt.xlabel('Emotion Category')
plt.title('Emotion Distribution')
plt.ylim(0, max(values) + 20 + 2 * max(len(words) for words in matched_words.values())) # Provide extra vertical space above bars
# Add the annotation for no matches if randomly generated
plt.text(len(categories) / 2, max(values) + 10, '0% Match. Generating Random Distribution.', fontsize=10,
color='red', ha='center')
# Add matched words above each column
for i, category in enumerate(categories):
words = matched_words[category]
for j, word in enumerate(words):
plt.text(i, values[i] + 2 + j * 2, word, fontsize=8, color='black', ha='center') # Increased spacing
plt.pause(0.1) # Pause to update the plot
# Function to send the emotion distribution and matched words via OSC
def send_osc(counts, matched_words):
highest_category = max(counts, key=counts.get) # Get the category with the highest value
for category, value in counts.items():
osc_client.send_message(f"/{category}", value)
words_message = ', '.join(matched_words[category])
osc_client.send_message(f"/{category}_words", words_message if words_message else " ")
osc_client.send_message("/highest_category", highest_category) # Send the highest category word
print(f"Word response: {highest_category}") # Print the word response
# Main loop to send random OSC values every 10 seconds
# Generate a random distribution
random_distribution = generate_random_distribution(categories)
matched_words = {category: [] for category in categories} # Empty matched words since it's random
# Plot and send the random distribution
plot_emotion_distribution(random_distribution, categories, True, matched_words)
send_osc(random_distribution, matched_words)
print("Emotion Distribution (Random Generation):")
for category, value in random_distribution.items():
print(f"{category}: {value:.2f}%")
time.sleep(10) # Wait for 10 seconds before repeating