Python activity, very confused--any help is appreciated!! PART 1 Write a functio
ID: 3825567 • Letter: P
Question
Python activity, very confused--any help is appreciated!!
PART 1 Write a function called wordCollector that takes two parameters as input: 1) a dictionary (possibly an empty dictionary) 2) a string representing a sentence. The function should iterate though the words in the sentence, and add them as keys to the dictionary, with the values representing the number of times the word has been seen. The function should first check to see if the word exists as a key in the dictionary. If the word is in the dictionary, then you should increment its count by one. If the word is not in the dictionary, then the function should add the word to the dictionary as a key, with a corresponding value 1, as an dictionary. Assume all the characters in the sentence are lowercase and there is no punctuation Hint for word in sentence. split will be helpful to iterate through the words Try the following code to test your implementation of wordCollector: empty dictionary word counts wordcounts wordCollector (wordcounts, this is a sentence") wordcounts word Collector (wordcounts this is like a group of words") wordcounts wordCollector (wordcounts lists of characters are this") for key in wordcounts: print key r wordcounts Ikey] If correctly implemented, you should see the following output: a 2 characters 1 group 1 like 1 sentence 1 this 3 of 2 is 2 lists 1 are words 1Explanation / Answer
#TASK-1
"""
def wordCollector(*args,**kwargs):
dict = {}
for word in args[0].split():
if(word not in dict):
dict[word] = 1;
elif(dict[word] >= 0):
dict[word] = dict[word] + 1;
for key, value in kwargs.iteritems():
flag = 0
for key2, value2 in dict.iteritems():
if(key2 == key):
dict[key] = value + value2;
flag = 1
if(flag == 0):
dict[key] = value
return dict
wordcounts = {}
wordcounts = wordCollector("this is a sentence",**wordcounts)
wordcounts = wordCollector("this is like a group of words",**wordcounts)
wordcounts = wordCollector("lists of characters are this",**wordcounts)
for key in wordcounts:
print key," : ",wordcounts[key]
"""
#TASK-2
class wordCounter():
def __init__(self):
self.wordcounts = {}
self.numSentencesCollected = 0
def wordCollector(self,*args):
self.numSentencesCollected += 1
dict = {}
for word in args[0].split():
if(word not in dict):
dict[word] = 1;
elif(dict[word] >= 0):
dict[word] = dict[word] + 1;
c = {x: dict.get(x, 0) + self.wordcounts.get(x, 0) for x in set(dict).union(self.wordcounts)}
self.wordcounts = c
def printNumCollected(self):
print "Counted %d sentences" % self.numSentencesCollected
def getMostCommonWord(self):
MostCommonWord = ''
TopOccurance = 0
for key, value in self.wordcounts.iteritems():
if(TopOccurance < value):
TopOccurance = value
MostCommonWord = key
return MostCommonWord
WC =wordCounter()
WC.wordCollector("this is a sentence")
WC.wordCollector("this is like a group of words")
WC.wordCollector("lists of characters are this")
WC.printNumCollected()
print "The most common word is: ",WC.getMostCommonWord()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.