Что я хочу сделать: отфильтровать определенный столбец данных, разделить эти твиты на 2 как положительные и отрицательные, сохранить их в словаре как положительные и отрицательные, подсчитать каждое слово в положительном или отрицательном подмножестве и написать число в значении, соответствующем этому слову в словаре. имейте в виду, что это моя домашняя работа, и я могу изменить код только между #. Я, вероятно, ошибаюсь после «за», но не могу понять почему.
def get_counts(data):
counts the number of times a word appears in negative or positive tweets
Parameters:
data: Pandas dataframe of tweets
Returns:
counts: Dictionary of counts, which includes the dictionaries 'pos' and 'neg'
counts = {"pos":{}, "neg":{}}
# Your code goes here
poscol=sentiment_data.loc[sentiment_data['a']>2,['f']] #here I filter positive tweets
negcol=sentiment_data.loc[sentiment_data['a']<2,['f']] #here I filter negative tweets
possen=''.join(poscol['f']) #I merge these tweets into one string
negsen=''.join(negcol['f'])
pos1 = word_tokenize(possen) #I tokenize these words so that I can make each of them keys of the dict
neg1 = word_tokenize(negsen)
pos = dict.fromkeys(pos1, [0]) #I create dicts with keys and absent values.
neg = dict.fromkeys(neg1, [0])
for a in counts:
if a in pos1:
counts['pos'][a] == pos1.count('a')
if a in neg1:
counts['neg'][a] += neg1.count('a')
return counts
# Do not change
counts = get_counts(sentiment_data)
print(counts["pos"]["happy"]) # should print 124
print(counts["neg"]["hate"]) # should print 99
KeyError Traceback (most recent call last)
<ipython-input-16-e732b4029bae> in <module>
33 # Do not change
34 counts = get_counts(sentiment_data)
---> 35 print(counts["pos"]["happy"]) # should print 124
36 print(counts["neg"]["hate"]) # should print 99
KeyError: 'happy'