''' DS2000 Fall 2024 Starter code for class on 11/8/24 The starter code reads in chatGPT responses from a csv file (responses only, ignoring questions/timestamps) In class we'll fill in together the two function stubs below: - clean_response - given a chatGPT response (string), remove punctuation and turn all letters lowercase - sentiment_score - given a list of words (strings) and a dictionary of word : score, compute and return a sentiment score from -1 to +1 ''' import matplotlib.pyplot as plt import csv TXTFILE = "chatgpt.csv" RESPONSE_COL = 0 SENTS = {"good": .6, "great" : .8, "celebrated": 1, "happier": 1, "love": .5, "like": .5, "happy" : .5, "dislike": -.5, "fight": -.5, "sad": -1, "divisive": -.25, "conflict" : -.25, "angry" : -.3, "argument" : -.2, "hate" : -1} def read_text(filename, resp_col): ''' parameters: string for filename, int for col where reponse it returns: list of strings (sentences/comments) does: opens the given csv file and turns one column into a list of strings ''' responses = [] with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter = ",") next(csvfile) for row in csvfile: responses.append(row[resp_col]) return responses def clean_response(response): ''' Parameters: a single response from chatGPT (a string) Returns: a string (copy/cleaned up version of input string) Does: creates a new string that matches the original, with punctuation removed and all lowercase ''' clean_str = "" for letter in response: if letter.isalpha() or letter == " ": clean_str = clean_str + letter.lower() return clean_str def sentiment_score(words, sentiments): ''' parameters: a single response from chatGPT (a list of strings), and a dictionary of word (str) : sentiment score (fl) Returns: float Does: Computes a sentiment score for the given response, based on values from the given dictionary ''' score = 0 for word in words: if word in sentiments: score += sentiments[word] score = score / len(words) return score def main(): # Step one: gather data by reading in the chatGPT text file and creating a list of responses sentences = read_text(TXTFILE, RESPONSE_COL) # Step two: clean up the strings to remove punctuation and turn lowercase for i in range(len(sentences)): sentences[i] = clean_response(sentences[i]) # Step two: computation! For each response, compute its sentiment score (-1 to +1) scores = [] for sentence in sentences: score = sentiment_score(sentence.split(), SENTS) # just for funsies, what was that one super-negative score we saw? if score < -0.01: print("That one negative response:", sentence) scores.append(score) # Step three: communication. Print or plot the scores. plt.hist(scores, color = "magenta") plt.title("Sentiment Scores for ChatGPT Responses") plt.xlabel("Score Range") plt.ylabel("Number of Responses") plt.show() main()