1

From previous question, I get this code

print("Array..............\n\n")
tagged=np.array(df['tagged_texts'])
temp = []
for x in tagged: 
    for y in x: 
        temp.append(y) 
tagged = temp
print(tagged)
pos=neg=obj=count=0
for word, tag in tagged:
    ss_set = []
    if 'NN' in tag and swn.senti_synsets(word):
        ss_set = list(swn.senti_synsets(word))
    elif 'VB' in tag and swn.senti_synsets(word):
        ss_set = list(swn.senti_synsets(word))[0]
    elif 'JJ' in tag and swn.senti_synsets(word):
         ss_set = list(swn.senti_synsets(word))[0]
    elif 'RB' in tag and swn.senti_synsets(word):
         ss_set = list(swn.senti_synsets(word))[0]
    if ss_set:
        pos=pos+synset.pos_score()
        neg=neg+synset.neg_score()
        obj=obj+synset.obj_score()
        count+=1
final_score=pos-neg
print(final_score)
df['final_score']=final_score

But then I get error message that name synset is not defined, anyone can explain to me why this happened? because I search the same question but I still didnt get to it

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-aa5a5112919f> in <module>
     11          ss_set = list(swn.senti_synsets(word))[0]
     12     if ss_set:
---> 13         pos=pos+synset.pos_score()
     14         neg=neg+synset.neg_score()
     15         obj=obj+synset.obj_score()

NameError: name 'synset' is not defined

​
Nadina
  • 185
  • 1
  • 1
  • 8
  • 2
    I see no `import` statements at the top of your code. Is that because you indeed have none? – Jongware May 05 '20 at 22:50
  • I used this ```from nltk.corpus import sentiwordnet as swn import pandas as pd import numpy as np import nltk ``` did I miss something?? – Nadina May 05 '20 at 22:58
  • Since this is _your_ code, you would know better what `synset` is supposed to refer to. – DYZ May 05 '20 at 23:34
  • I get this code and tried to figure whats wrong but still dont get it, I think the synset is to get the positive score, the code is tryin to calculate sentiment – Nadina May 05 '20 at 23:50

1 Answers1

0

from nltk.corpus import wordnet as wn

Importing this module may solve this problem.

  • This won't actually load the symbol `synset` yet. OP would need to change `synset` to `wn.synset`, or maybe use `from nltk.corpus.wordnet import synset` if they really want the `synset` name as is. – joanis May 16 '22 at 14:09