Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

count_words: (str, dict of {str: int}) -> None The first parameter is tweet text

ID: 3777544 • Letter: C

Question

count_words:
(str, dict of {str: int}) -> None The first parameter is tweet text, and the second is a dictionary containing lowercase words as keys and integer counts as values. The function should update the counts of words in the dictionary. If a word is not the dictionary yet, it should be added. For the purposes of this function, words are defined by whitespace: every string that occurs between two pieces of whitespace (or between a piece of whitespace and either the beginning or end of the tweet) could be a word. Numeric characters are treated the same way as alphabetic characters. Hashtags, mentions, and URLs are not considered words. The empty string is not considered a word. Words don't contain punctuation, so punctuation should be removed from any candidate words. For example, if we are analyzing the tweet "@utmandrew Don't you wish you could vote? #MakeAmericaGreatAgain", we would increment the count for the word "you" by 2 and the counts for words "dont", "wish", "could", and "vote" by 1. count_words:
(str, dict of {str: int}) -> None The first parameter is tweet text, and the second is a dictionary containing lowercase words as keys and integer counts as values. The function should update the counts of words in the dictionary. If a word is not the dictionary yet, it should be added. For the purposes of this function, words are defined by whitespace: every string that occurs between two pieces of whitespace (or between a piece of whitespace and either the beginning or end of the tweet) could be a word. Numeric characters are treated the same way as alphabetic characters. Hashtags, mentions, and URLs are not considered words. The empty string is not considered a word. Words don't contain punctuation, so punctuation should be removed from any candidate words. For example, if we are analyzing the tweet "@utmandrew Don't you wish you could vote? #MakeAmericaGreatAgain", we would increment the count for the word "you" by 2 and the counts for words "dont", "wish", "could", and "vote" by 1.

Explanation / Answer

import re

def count_words(string, wordsdict):
    wordList = re.sub("[^w]", " ", string).split()
    for word in wordList:
        word = word.lower()
        if word in wordsdict.keys():
            wordsdict[word] += 1
        else:
            wordsdict[word] = 1
    return wordsdict

wordsdict = {}
string = "@utmandrew Don't you wish you could vote? #MakeAmericaGreatAgain"
print count_words(string, wordsdict)