Question: Hello, I need help creating 2 functions. stopwords set is as follows: {'other', it's, 'them', he's, 'will', won't, 'her', 'only', 'with', 'no', 'few', they're, 'was',
Hello, I need help creating 2 functions.
stopwords set is as follows: {'other', "it's", 'them', "he's", 'will', "won't", 'her', 'only', 'with', 'no', 'few', "they're", 'was', 'so', 'at', 'off', 'just', 'on', "they've", 'ourselves', 'both', "he'll", "that's", 'this', 'where', 'by', 'for', "weren't", "here's", "shouldn't", "he'd", "how's", 'theirs', "isn't", 'does', "wouldn't", 'each', 'then', 'well', 'am', 'every', 'an', 'good', "haven't", 'our', 'yours', 'has', "who's", "let's", 'to', 'it', 'do', 'such', "ain't", "i'd", 'i', 'be', 'below', 'any', 'ours', "i've", "there's", 'would', 'how', 'are', 'against', 'their', 'again', 'until', 'themselves', 'there', 'than', "she'll", "i'm", "wasn't", 'further', 'they', 'those', 'before', 'hers', 'never', 'because', "hasn't", 'under', 'always', 'his', 'nothing', "mustn't", "shan't", "we're", 'is', 'which', 'he', 'should', 'into', 'could', 'these', 'everyone', 'himself', "couldn't", "they'd", 'when', 'who', "she'd", 'him', 'your', 'my', 'through', 'ought', 'its', 'over', "don't", 'ever', "aren't", 'more', 'once', "can't", 'too', "doesn't", 'having', 'in', 'some', 'still', 'around', 'about', 'myself', 'aint', 'between', 'yourself', 'have', "she's", 'she', "you've", 'up', "they'll", 'out', 'us', "hadn't", "you're", 'from', 'nor', "where's", 'of', 'same', 'me', 'during', 'if', 'that', 'being', 'above', 'doing', 'and', 'herself', 'did', 'own', 'had', "we'd", 'now', 'already', 'what', 'or', 'as', "when's", "we've", 'not', 'we', 'while', 'most', "you'll", 'down', "what's", 'you', 'very', 'here', 'hey', 'a', 'oh', 'were', 'anything', 'cannot', 'the', 'all', "i'll", 'can', "we'll", "why's", 'been', 'why', 'but', "didn't", 'itself', 'whom', 'yourselves', "you'd", 'after'}
the first is
validate_word(word, stopwords) -> Boolean
This function receives a string and a set of words as parameters . If the given word is in the stop word set or it has any digit or punctuation, the function returns False. Otherwise, it returns True. Hint: the string method isalpha() may prove useful here.
Function Test validate_word
After storing the stop words in stopwords
for w in ["you", "love", "peace!", "face2face"]: print(w, ":" , validate_word(w, stopwords))
Instructor values:
you : False love : True
peace! : False
face2face : False
process_lyrics(lyrics, stopwords) -> set
This function receives a string and a set of stop words as parameters. The string contains the lyrics. The function splits the lyrics by space. Each word is then made lower case and stripped of whitespace and then punctuation. After that it validates each word by using the validate_word function. If the word is valid, it adds that word to a set which will be returned after all words are processed. Note: Do not forget to convert the words to lowercase and strip punctuation from the end of words (if there is something like a hyphen in the middle of a string, we will not strip it). Hint: string.punctuation is useful.
Function Test process_lyrics
After storing the stop words in stopwords
Testing string: lyrics = """Yes, how many times must a man look up Before he can really see the sky? Yes, how many ears must one man have Before he can hear people cry? Yes, how many deaths will it take till he knows That too many people have died? The answer my friend is blowin' in the wind The answer is blowin' in the wind."""
Instructor value for process_lyrics(lyrics, stopwords): {'one', 'sky', 'answer', 'deaths', 'ears', 'man', 'knows', 'cry', 'till', 'wind', 'blowin', 'yes', 'times', 'must', 'many', 'friend', 'take', 'hear', 'really', 'look', 'died', 'people', 'see'}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
