while implementing extract_features function, I tried to get the value for the key = (“word”,1) and compare if is not None in freqs dict. and if so update x[0,1] and x[0,2]. What am I doing wrong here?
The easiest way to achieve what you want is with dictionary’s get()
method. You can find more details in this thread.
Basically, what the function does is:
- in the start
x
is set to an array of three values: [[0, 0, 0]] - next “bias” is set to 1;
x[0,0] = 1
results inx
→ [[1, 0, 0]] - next you loop over the words:
- in each loop you need to update (accumulate with
+=
) the “positive” part, which is atx[0,1]
; to get that, you use theget()
method onfreqs
dictionary where keyname is(word, 1.0)
and value for not found is 0. For example, if (word, 1.0) has 5 positive counts, then after the iterationx
→ [[1,5,0]] - in each loop you also need to update (accumulate with
+=
) the “negative” part, which is atx[0,2]
(or third “column”); to get that, you also use theget()
method onfreqs
dictionary where keyname is(word, 0.0)
and value for not found is 0. For example, if (word, 1.0) has 2 negative counts, then after the iterationx
→ [[1,5,2]]
- in each loop you need to update (accumulate with
- after the
for
loop is finished the example of thex
could be [[1, 313, 61]] - the tweets words accumulated to be positive. If none of the words were in the dictionary (like the example of ‘blorb bleeeeb bloooob’), thenx
with [[1, 0, 0]] will be returned.
Good luck!