AttributeError: 'list' object has no attribute 'copy'

NLTK classifiers work with feature sets; these are always given as dictionaries with feature names mapping to a value. You are passing in a list instead, so you are not producing features as per the NLTK documentation. The code simply expects a Python dictionary, and Python dictionaries have a .copy() method.

See the NLTK tutorial chapter on Learning to Classify Text:

The returned dictionary, known as a feature set, maps from feature names to their values. Feature names are case-sensitive strings that typically provide a short human-readable description of the feature, as in the example 'last_letter'. Feature values are values with simple types, such as booleans, numbers, and strings.

Also see the Featuresets section of the NLTK Classify API documentation:

The features describing a token are encoded using a “featureset”, which is a dictionary that maps from “feature names” to “feature values”. Feature names are unique strings that indicate what aspect of the token is encoded by the feature.

You haven't shared what kind of objects the train_data list contains; if those are feature set dictionaries, you want to use classify_many() instead:

results = classifier.classify_many(test_data)

That method does take a list, but each element must still be a valid feature set.


The list.copy method does not work both in python 2.x and python 3.x, I wonder why it is still in the documentation. To achieve the results of copying a list, user the list keyword:

fruits = ['banana', 'cucumber', 'apple', 'water mellon']
my_fruits = list(fruits)

Optionally, you can copy a list by slicing it:

my_fruits_copy = fruits[:]

Tags:

Python

List

Nltk