【问题标题】:I have wrote this code .what the wrong?? error is (invalid syntax)我已经写了这段代码。怎么了??错误是(无效的语法)
【发布时间】:2021-02-06 13:07:21
【问题描述】:

我的代码是朴素贝叶斯分类器,我想计算正面和负面的句子

   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)

【问题讨论】:

  • 请用您的问题发布整个回溯。这将帮助其他人查明您代码中的问题

标签: python classification naivebayes


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您必须将“=”替换为“==”。并以 ':' 结束 if 语句。与 elif 相同。

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

    【讨论】:

      【解决方案3】:

      错误出现在 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2018-06-02
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        • 2019-12-18
        • 1970-01-01
        相关资源
        最近更新 更多