Question: I'm coding with Python and trying to yield the result [('happy', 16), ('glad', 2), ('pleased', 2), ('delighted', 2), ('joyus', 1)] but keep getting [('happy', 1),
I'm coding with Python and trying to yield the result [('happy', 16), ('glad', 2), ('pleased', 2), ('delighted', 2), ('joyus', 1)] but keep getting [('happy', 1), ('happy', 2), ('happy', 3), etc.]. I think something is wrong with my count or .append in the last loop but I can't figure it out (code below). Any help is greatly appreciated!!
def search(keyword): All_words = [keyword] for Entry in Thesaurus: #for word in Entry: if keyword == Entry.word: #print(keyword) for word in Entry.synonyms: #print(word) All_words.append(word) #print(All_words) #return All_words # modify to return a list of tuples for Search_word in All_words: #print(Search_word) Count = 0 #match = [] tup_list = [] #print(Count) for Document in Corpus: for Word in Document: #print(Word) if Search_word == Word: #print(Search_word) Count = Count + 1 #print(word) #print(Count) tup = (Search_word, Count) #print(Count) tup_list.append(tup) #print(tup_list) return tup_list input = "happy" output = search(input) # invoke the method using a test input print(output) # prints the output of the function # do not remove this line!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
