I am trying to create a classifier using NLTK, however, I believe that I have a problem in the format of my data that I cannot get over.
My data looks like this:
data = [("TEXT 1", 'no'), ("TEXT 2", 'yes'), ("TEXT 3", 'no'), ("TEXT 4", 'no'), ("TEXT 5", 'yes')]
Then, I run the following code:
import nltk
from nltk.classify import maxent
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)
But, unfortunately I have the following error. What does this error consist of and how do I overcome it?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-93-e1b29adbeebb> in <module>
----> 1 classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)
/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls, train_toks, algorithm, trace, encoding, labels, gaussian_prior_sigma, **cutoffs)
324 algorithm = algorithm.lower()
325 if algorithm == "iis":
--> 326 return train_maxent_classifier_with_iis(
327 train_toks, trace, encoding, labels, **cutoffs
328 )
/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train_maxent_classifier_with_iis(train_toks, trace, encoding, labels, **cutoffs)
1175 # Construct an encoding from the training data.
1176 if encoding is None:
-> 1177 encoding = BinaryMaxentFeatureEncoding.train(train_toks, labels=labels)
1178
1179 # Count how many times each feature occurs in the training data.
/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls, train_toks, count_cutoff, labels, **options)
665
666 # Record each of the features.
--> 667 for (fname, fval) in tok.items():
668
669 # If a count cutoff is given, then only add a joint
AttributeError: 'str' object has no attribute 'items'
From the documentation:
train(train_toks, algorithm=None, trace=3, encoding=None, labels=None, gaussian_prior_sigma=0, **cutoffs)
Parameters train_toks (list) – Training data, represented as a list of pairs, the first member of which is a featureset, and the second of which is a classification label.
Your tuples need to have the first element be a dict
that "map[s] strings to either numbers, booleans or strings" then you need to have your second element be the classification label.
from nltk.classify import maxent
data = [({"TEXT 1": 'no'}, "Label")]
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)