-1

my code is naive bayes classifier and I want count positive & negative sentences

   pos_count=0
    neg_count=0
    file = open("a-samples.txt","r")
     
    for line in file:
      custom_tokens = remove_noise(word_tokenize(line))
      print('\n',line,'\n',classifier.classify(dict([token, True] for token in custom_tokens)))
      if (classifier.classify(dict([token, True] for token in custom_tokens) = "Positive"
        pos_count=pos_count+1                                           
      elif (classifier.classify(dict([token, True] for token in custom_tokens)="Negative"
        neg_count=neg_count+1
                                
    print ("pos =",pos_count,'\n',"neg= ",neg_count)
  • Please post the entire traceback with your question. that will help others to pinpoint the problem in your code – Sashaank Oct 27 '20 at 05:07

3 Answers3

1

Move classifier.classify(dict([token, True] for token in custom_tokens into single variable.

Replace = with == in the comparison operation (if-elif-else block). End conditions with :.

roddar92
  • 353
  • 1
  • 4
1

You have to replace '=' with '=='. And end if statement with ':'. Same thing with elif.

if (classifier.classify(dict([token, True] for token in custom_tokens) == "Positive":
Majo
  • 56
  • 4
0

The error was in dict([token, True] for token in custom_tokens) you could use dict comprehensions and you should use == in if statements.

I am not sure what remove_noise or classifier.clasify functions does so i am guessing that you are trying something like this:

pos_count = 0
neg_count = 0
file = open("a-samples.txt", "r")
    
for line in file:
    custom_tokens = remove_noise(word_tokenize(line))
    
    print('\n', line, '\n', classifier.classify({token: True for token in custom_tokens}))
    
    if (classifier.classify({token: True for token in custom_tokens})) == "Positive":
        pos_count = pos_count + 1
    
    elif (classifier.classify({token: True for token in custom_tokens})) == "Negative":
        neg_count = neg_count + 1
                            
print ("pos =", pos_count, '\n', "neg= ", neg_count)
TUNAPRO1234
  • 106
  • 5