The loop creates a nested dictionary, and sets all values to 1, presumably to then just use the keys as a way to weed out duplicate values.
You could use sets instead and avoid the = 1
value:
sentimentDict = {}
def loadSentiment():
with open('Sentiment/positive_words.txt', 'r') as f:
sentimentDict['positive'] = {line.strip() for line in f}
with open('Sentiment/negative_words.txt', 'r') as f:
sentimentDict['negative'] = {line.strip() for line in f}
Note that you don't even need to create the initial dictionaries; you can create the whole set with one statement, a set comprehension.
If other code does rely on dictionaries with the values being set to 1
(perhaps to update counts at a later stage), it'd be more performant to use the dict.fromkeys()
class method instead:
sentimentDict = {}
def loadSentiment():
with open('Sentiment/positive_words.txt', 'r') as f:
sentimentDict['positive'] = dict.fromkeys((line.strip() for line in f), 1)
with open('Sentiment/negative_words.txt', 'r') as f:
sentimentDict['negative'] = dict.fromkeys((line.strip() for line in f), 1)
Looking at your source blog article however shows that the dictionaries are only used to do membership testing against the keys, so using sets here is much better and transparent to the rest of the code to boot.