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(";"):
self.positives.adds(line.str.strip())
print("Printing positives_1:")
print(self.positives) # Test to see if the words are loaded into memory
print()
file.close()
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(";"):
self.negatives.adds(line.str.strip())
print("Printing negatives_1:")
print(self.negatives) # Test to see if the words are loaded into memory
print()
file.close()
return True
load()
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.