我写了这段代码。怎么了?错误是(语法无效)

时间:2020-10-23 12:57:03

标签: python classification naivebayes

我的代码是朴素的贝叶斯分类器,我想计算肯定和否定的句子

   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)

3 个答案:

答案 0 :(得分:1)

classifier.classify(dict([token, True] for token in custom_tokens移至单个变量。

在比较操作中将=替换为==if-elif-else块)。以:结束条件。

答案 1 :(得分:1)

您必须将'='替换为'=='。并在if语句后加':'。与elif相同。

if (classifier.classify(dict([token, True] for token in custom_tokens) == "Positive":

答案 2 :(得分:0)

错误出在dict([token, True] for token in custom_tokens)中,您可以使用dict理解,而应该在if语句中使用==

我不确定remove_noise或classifier.clasify函数是做什么的,所以我猜测您正在尝试这样的事情:

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)
相关问题