I'm hoping you can help me - I'm rather struggling with the jump from C to Python in CS50 2017 edition - lots of new syntax and concepts to cover in one week! I'm currently at the very first stage - getting Smile to work.

Based on the lecture and Zamyla's walkthroughs, I've written the following code to initialize analyzer and load positive-words.txt and negative-words.txt into memory. NB the smile file defines the absolute paths to our lists of positive and negative words as:

positives = os.path.join(sys.path[0], "positive-words.txt")

negatives = os.path.join(sys.path[0], "negative-words.txt")

so my understanding is that I can just refer to these files as 'positives' and 'negatives', respectively. My code:

import nltk

class Analyzer():
"""Implements sentiment analysis."""

"""Initialize Analyzer."""

def __init__(self, positives, negatives):
    self.positives = set()
    self.negatives = set()

def load(self, positives, negatives):

    file = open(positives, "r")
    print("Printing positives_0") # Test to see if this code is read and executed
    for line in file:
        if not str.startswith(";"):
    print("Printing positives_1:")
    print(self.positives) # Test to see if the words are loaded into memory

    file = open(negatives, "r")
    print("Printing negatives_0") # Test to see if this code is read and executed
    for line in file:
        if not str.startswith(";"):
    print("Printing negatives_1:")
    print(self.negatives) # Test to see if the words are loaded into memory

    return True


As far as I can tell, this is a direct adaptation of what Prof. Malan shows in week 8's lecture as the way to load a the dictionary for a spell-checker, in Python. I've added in some print statements as a way of testing whether the code is read, and executed (it isn't!).

The IDE flags up an error pre-interpretation that says the following:

"Instance of 'set' has no 'adds' member" - this is on lines 18 and 28 - the "self.positives.adds(..." lines.

I don't understand this error - since this is the code Prof. Malan used, I had understood that set objects would have a the adds() method included in them. I've tried googling the definition of sets in Python, but can't find a complete reference.

If I run "./smile love", I just get the output:| - when I was expecting the print statements from analyzer to execute!

Thanks in advance for your help.

  • OK. I noticed I hadn't actually called the load() method. I've added it into the code above. When I run ./smile love I now get the following error: Traceback (most recent call last): File "./smile", line 6, in <module> from analyzer import Analyzer File "/home/ubuntu/workspace/pset6/sentiments/analyzer.py", line 3, in <module> class Analyzer(): File "/home/ubuntu/workspace/pset6/sentiments/analyzer.py", line 36, in Analyzer load() TypeError: load() missing 3 required positional arguments: 'self', 'positives', and 'negatives' – Matt Friend May 2 '17 at 21:12
  • Thanks both Vikrant Pradan and @DinoCoderSaurus - your explanations helped me adapt my code to make it work. More importantly (to me!) they gave me a huge "aha moment" about what it means to use the __init__() function to define what will happen when the code instantiates a specific instance of the class. I hadn't realised I could define the class Analyzer in such a way as to load 'positives' and 'negatives' into memory inside/as part of the instance of the object. Mind blown! – Matt Friend May 5 '17 at 10:15

"Instance of 'set' has no 'adds' member" - this is on lines 18 and 28 - the "self.positives.adds(..." lines.

The set operator is the singular add. Notice Prof is (presumably) having the same error:

enter image description here

Here's python doc on sets.

The load error mentioned in your comment is because the load function is defined to take 3 arguments, the call load() is sending 0 arguments.

But let's back up a little. Adding a load function to an Analyzer object is much more complicated than it need be. Analyzer is an object (Class), inside of which is data. The data you want it to contain is two lists. It is more analogous to the structs1.py in the lecture. Build those lists inside the __init__ function, and they will always be available to an Analyzer object. The analyze function/method will have access to these sets, so when it is called from smile, it can evaluate the word.

And remember you want to strip newlines from your input. From the python doc "If omitted or None, the chars argument defaults to removing whitespace."


The function for adding elements to set in python is add() not adds(). Moreover in your last line of code you have called the load() function but you have not passed the required parameters. i.e the file paths for negative and positive words.

Also one more thing, you have to call the load function inside the init() function so for that you need to write load() function before the init() or you can simply load the sets inside the init() function without the use of a load() function

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .