简体   繁体   中英

How do you see built-in variables to check if your answer is correct? Python

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:

  1. I'm not getting the right keywords and/or occurrences from my code
  2. I can't figure out how to correctly print the Thesaurus (in order to check). The Corpus prints nicely with just a simple print statement, however the same print statement will return items in the Thesaurus as their locations instead of the words. I don't know the variable attributes, as I didn't define the class, so I feel like I'm shooting in the dark.
  3. When I try to give store.append two arguments, it won't accept both, even though I see other people have done it this way. I can only get the words and number of occurrences to print when I have two store.append statements. (I'm trying to get the output to be a tuple and not a list)

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!
  1. 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"]}.

  2. 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)

  1. append two args by using a.apend([b1, b2]) is a possible way.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM