classificationnltknaivebayespython-3.12

nltk.NaiveBayesClassifier.classify() input parameter


I have the following trained classifier:

classifier = nltk.NaiveBayesClassifier.train(features[:train_count])

When I try to use it to classify():

result = classifier.classify(feature)

and feature is ({'mean_compound': 1.0, 'mean_positive': 0.0, 'positives': 0}, ''), I hit the following error:

Traceback (most recent call last):
  File "/usr/src/Python/pAIthon/Nltk.py", line 240, in <module>
    CustomizeSentimentAnalysis()
  File "/usr/src/Python/pAIthon/Nltk.py", line 186, in CustomizeSentimentAnalysis
    result = classifier.classify(feature)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/khteh/.local/lib/python3.12/site-packages/nltk/classify/naivebayes.py", line 89, in classify
    return self.prob_classify(featureset).max()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/khteh/.local/lib/python3.12/site-packages/nltk/classify/naivebayes.py", line 95, in prob_classify
    featureset = featureset.copy()
                 ^^^^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'copy'

What should be the input parameter to classify()?


Solution

  • Use the feature without the label: {'mean_compound': 1.0, 'mean_positive': 0.0, 'positives': 0}