1

I am getting module not trained error. However I have created training set in the sample code

from monkeylearn import MonkeyLearn

# Use the API key from your account
ml = MonkeyLearn('211893df48b')

# Create a new classifier
res = ml.classifiers.create('Test Classifier')

# Get the id of the new module
module_id = res.result['classifier']['hashed_id']

# Get the id of the root node
res = ml.classifiers.detail(module_id)
root_id = res.result['sandbox_categories'][0]['id']

# Create two new categories on the root node
res = ml.classifiers.categories.create(module_id, 'Negative', root_id)
negative_id = res.result['category']['id']
res = ml.classifiers.categories.create(module_id, 'Positive', root_id)
positive_id = res.result['category']['id']

# Now let's upload some samples
samples = [('The movie was terrible, I hated it.', negative_id), ('I love this movie, I want to watch it again!', positive_id)]
res = ml.classifiers.upload_samples(module_id, samples)

# Now let's train the module!
res = ml.classifiers.train(module_id)

# Classify some texts
res = ml.classifiers.classify(module_id, ['I love the movie', 'I hate the movie'], sandbox=True)
print res.result

Traceback (most recent call last): File "monkey_learn.py", line 30, in res = ml.classifiers.classify(module_id, ['I love the movie', 'I hate the movie'], sandbox=True) File "build/bdist.linux-x86_64/egg/monkeylearn/classification.py", line 67, in classify File "build/bdist.linux-x86_64/egg/monkeylearn/utils.py", line 101, in handle_errors monkeylearn.exceptions.MonkeyLearnException: Error: "The module is not trained. You have to train it before using the api."

Hariprasad
  • 1,611
  • 2
  • 14
  • 19

2 Answers2

0

There is a problem with module. To try beginning with MonkeyLearn we can use public modules. Public modules are pre-built modules created by MonkeyLearn community.

from monkeylearn import MonkeyLearn

ml = MonkeyLearn('211893df48b')
text_list = ["This is a text to test your classifier", "This is some more text"]
# English Tweets Sentiment Analysis
module_id = 'cl_qkjxv9Ly'
res = ml.classifiers.classify(module_id, text_list, sandbox=True)
print res.result
Hariprasad
  • 1,611
  • 2
  • 14
  • 19
0

The problem seems that you are trying to classify a new text, before the training of your model is finished. Try waiting a little bit (so you give enough time for the training to be completed) before using classify.

  • is there a chance that you'll be able to implement an appropriate callback for the `.train` method, which we can rely on? I saw that this action (https://monkeylearn.com/api/v3/?javascript#train-classifier) is *not required to be invoked manually*, but still, how we can identify that the model that have been just created with some dataset is ready to be asked with the `.classify` method? I don't want to make any `setInterval` functions, that will check the `last_trained` field for this purposes you know :) – Skyrocker Nov 08 '19 at 10:13