I'm almost there, I think, but I'm stuck. In this problem, there are built-in variables, Thesaurus and Corpus, that I don't have to define. I'm having a couple issues:
Right now, I'm testing with this made-up Thesaurus and Corpus:
class Entry : #testing your own Thesaurus/ Corpus
def __init__(self, word, synonyms) :
self.word = word
self.synonyms = synonyms
e1 = Entry("savory", ["umami", "meat", "main course", "dinner"])
e2 = Entry("sweet", ["dessert", "candy", "tart", "sugar"])
Thesaurus = [e1, e2]
doc1 = ["My", "main course", "is", "a", "meat", "dish", "with", "lots", "of", "umami"]
doc2 = ["yum", "I", "love", "sweet", "dessert"]
doc3 = ["this", "is", "yet", "another", "savory", "dish"]
Corpus = [doc1, doc2, doc3]
Any help is appreciated. Thanks.
def search(keyword) :
all_words = [keyword]
for entry in Thesaurus:
if entry.word == keyword:
for word in entry.synonyms:
all_words.append(word)
store = []
for search_word in all_words:
count = 0
for document in Corpus:
for word in document:
if search_word == word:
count = count + 1
store.append(search_word)
store.append(count)
return store
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!
I think the best solution for you is using the dictionary as the representation of the Entity if you don't have to use class, which look like e1 = {'savory':["umami", "meat", "main course", "dinner"]}.
You have two ways to print class:
2.1 write a print method in the class and call it to print the class.
2.2 try to visit __ dict __ like: print(e1.__ dict __) (no blankspace)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.