''' DS2000 Spring 2023 Sample code from class - March 3rd We have the transript from the totally unhinged Bing AI chatbot conversation with a NY Times reporter. https://mashable.com/article/microsoft-bing-ai-chatbot-weird-scary-responses What was the word it used most frequently? ''' FILENAME = "chatbot.txt" def read_txt(filename): ''' Function: read_txt Parameter: filename, a string Returns: list of strings Does: iterate over each line of the file, split on spaces and append each word to a list ''' words = [] with open(filename, "r") as infile: for line in infile: words += line.split() return words def wordcount(lst): ''' FUnction: wordcount Parameter: list of strings, including repeats returns: a ditionary, where key = word and value - count Does: iterate over the list of words, updating the value in the dictionary as we go ''' wc = {} for word in lst: if word in wc: wc[word] += 1 else: wc[word] = 1 return wc def find_max(dct): ''' Function: find_max Parameters: dictionary where key is anythig, but value must be a number Return: key, value pair where value is higheset Does: iterate over given dictoinary, find highest value and return both the value and its key ''' max_val = -1 max_key = "" for key, value in dct.items(): if value > max_val: max_val = value max_key = key return max_key, max_val def main(): # Gather data - read in the text file to a 1d list words = read_txt(FILENAME) # Computation - turn the list of words into a dictionary dct = wordcount(words) # Computation - find the top 10 most common words # Keep a list of common words, and stop when we hit 10 most_common = [] while len(most_common) < 10: max_word, max_count = find_max(dct) most_common.append(max_word) del(dct[max_word]) # Communiation - print our list of top-10 common words print(most_common) main()