brooklyn

py

School

Yale University *

*We aren’t endorsed by this school

Course

351B

Subject

Computer Science

Date

Apr 3, 2024

Type

py

Pages

2

Uploaded by CountIron11913

Report
import csv import matplotlib.pyplot as plt # Problem 1: count the words spoken by favorite characters def count_words(filename, favorite_characters, stopwords): word_counts = {} # create a dictionary to store word counts for characters with open(filename, 'r') as file: reader = csv.reader(file) for line in reader: character, dialogue = line[0], line[1] character = character.upper() # convert character name to uppercase if character in favorite_characters: words = dialogue.split() for word in words: word = word.lower() # convert the word to lowercase if word not in stopwords: if character not in word_counts: word_counts[character] = {} # create a sub-dictionary for the character if it doesn't exist if word not in word_counts[character]: word_counts[character][word] = 0 # initialize word count to 0 if the word is not in the sub-dictionary word_counts[character][word] += 1 return word_counts # Problem 2: print the top-n words for a character def print_top_words(character, n, word_counts): if character in word_counts: words = word_counts[character] sorted_words = sorted(words.items(), key=get_word_count, reverse=True) print(f"{character} top-{n} words:") for i in range(min(n, len(sorted_words))): word, count = sorted_words[i] print(f"{word}: {count}") else: print(f"{character} is not one of your favorite characters.") # helper function to get word count from an item (used for sorting) def get_word_count(item): return item[1] # Problem 3: plot the top-n words for a character def plot_top_words(character, n, word_counts): if character in word_counts: words = word_counts[character] sorted_words = sorted(words.items(), key=get_word_count, reverse=True) top_words = [word[0] for word in sorted_words] word_counts = [word[1] for word in sorted_words] # create a bar chart to visualize the top words and their counts plt.bar(top_words[:n], word_counts[:n]) plt.xlabel("Words") plt.ylabel("Word Counts") plt.title(f"Top {n} Words for {character}") plt.xticks(rotation=45) plt.show()
else: print(f"{character} is not one of your favorite characters.") def main(): filename = 'b99_lines.csv' favorite_characters = ['ROSA', 'HOLT', 'GINA'] stopwords = ["a", "an", "the", "i", "im", "he", "she", "they", "and", "if", "of", "out", "do", "you", "to", "it", "is", "was", "in", "we", "that", "my", "your", "be", "on", "this", "me", "are", "what", "with", "but", "youre", "so", "oh", "for", "like", "him", "have", "not", "one", "too", "just", "dont", "well", "yeah", "all", "let", "about", "go", "can", "at", "hes", "thats", "am", "its", "as", "were", ""] # process the dialogues and get word counts for the favorite characters word_counts = count_words(filename, favorite_characters, stopwords) # print the top words for each character print_top_words('ROSA', 10, word_counts) print_top_words('HOLT', 10, word_counts) print_top_words('GINA', 10, word_counts) # prompt user to enter a character and plot their top words character_name = input("Enter one of your favorite characters: ") n = 10 plot_top_words(character_name.upper(), n, word_counts) if __name__ == '__main__': main()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help