Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Question - Struggling with writing a function in Python that will go through a list of documents and count the frequency of appearance of

Python Question - Struggling with writing a function in Python that will go through a list of documents and count the frequency of appearance of a particular keyword and its synonyms.

For reference, an "Entry" in the code refers to a class of objects with the attributes "word" and "synonyms". A Thesaurus is a list of Entry objects and a Corpus is a list of Documents, and each Document is a list of strings.

The first six lines involve the creation of a list, All_words, that contains the keyword and the keyword's synonyms.

From lines 7-12, the algorithm can only get the function to output the FIRST Search_word in All_words and its number of occurrences.

#####

def search(keyword) : #user inputs keyword All_words = [keyword] for Entry in Thesaurus: if Entry.word == keyword: for Word in Entry.synonyms: All_words.append(Word) for Search_word in All_words: count = 0 for Document in Corpus: for Word in Document: if Search_word == Word: count = count + 1 return Search_word, count

input = "happy" output = search(input) print(output)

#####

For reference, if the keyword is "sad", All_words should be: ['sad', 'upset', 'depressed', 'unhappy']

So far if we input "sad" the results are: ('sad',4) instead of returning multiple tupules like: (('sad',4),('upset',2),('depressed',2),('unhappy',0))

And if the input is: "happy"

The results are just: ('joyous', 1)

Need help to create a list that contains each synonym and corresponding count before the final for loop, and to append the data to the list inside the for loop.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions