pythonremove-method

Difficulty with analyzer.py, returns KeyError: ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n'


Here's how my code looks like:

import nltk

class Analyzer():

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

        file = open(positives, "r")
        for line in file:
            self.positives.add(line.strip("\n"))
            if line.startswith(";"):
                self.positives.remove(line)
        file.close()

        file1 = open(negatives, "r")
        for line in file1:
            self.negatives.add(line.strip("\n"))
            if line.startswith(";"):
                self.negatives.remove(line)
        file1.close()

    def analyze(self, text):
        with open("text") as texts:
            for lines in texts:
                # Get a list of words from the lines in text.
                tokens = [self.tokenizer.tokenize(lines)]
                # All the words in postive-words and negative-words are lowercased.
                if tokens.lower() in self.positives:
                    return 1
                elif tokens.lower() in self.negatives:
                    return -1
                else:
                    return 0

Unfortunately, this seem to not work and no matter how I shift around the lines of codes, I kept getting:

Traceback (most recent call last):
  File "./smile", line 32, in <module>
    main()
  File "./smile", line 20, in main
    analyzer = Analyzer(positives, negatives)
  File "/home/ubuntu/workspace/pset6/sentiments/analyzer.py", line 13, in __init__
    self.positives.remove(line)
KeyError: ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n'

May I get a hint of what am I doing wrong? Would really appreciate some hints! Thank you!


Solution

  • The issue is that you are trying to remove an item from the set that isn't present. You are adding line.strip("\n") to the set, but then attempting to remove line from that same set. To ensure that you are always removing something actually present in the set you could do something like the following:

    entry = line.strip("\n")
    self.positives.add(entry)
    if line.startswith(";"):
        self.positives.remove(entry)
    

    In the above code you can never accidentally try to remove something not present in the dictionary. You will have to make similar changes when dealing with self.negatives as well, but that shouldn't be too hard.

    Alternatively you could simply not add an entry to the set if you would then be removing it by rearranging to something like the following:

    if not line.startswith(";"):
      self.positives.add(line.strip("\n"))