0

I'm working on the init function is sentiments and I'm struggling a bit on what to put in the for loop. This is what I have so far, can someone tell me if this is correct or how to fix it?

def __init__(self, positives, negatives):
"""Initialize Analyzer."""
self.positives = set()
file = open(positives, "r")
for line in file:
    with open('positive-words.txt', 'r') as f:
    pos_words = [line.strip() for line in f]
file.close()

def analyze(self, text):
    """Analyze text for sentiment, returning its score."""
0

Use either file = open(...); for line in file: ... file.close() or with open(..., not both. So either

self.positives = set()
file = open(positives, "r")
for line in file:
    self.positives.add(line.strip())
file.close()

or

with open(positives, "r") as file:
    self.positives = set([line.strip() for line in file])

or equivalent.

I also had some condition to ignore lines starting with ;, like

with open(positives, "r") as file:
    self.positives = set([line.strip() for line in file if not line.strip().startswith(";")])

You must log in to answer this question.

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