Sentiment()

Sentiment is a model trained to predict the sentiment of any given text. The default model, currently 'moviereviews', is trained using IMDB reviews that have been truncated to a maximum of 200 words, only the 20000 most used words in the reviews are used.

This model, ported from tensorflowJS, scores the sentiment of text with a value between 0 ("negative") and 1 ("positive").

Example

// Create a new Sentiment method
const sentiment = ml5.sentiment('movieReviews', modelReady);
      
// When the model is loaded
function modelReady() {
  // model is ready
  console.log("Model Loaded!");
}

// make the prediction
const prediction = sentiment.predict(text);

Here is a complete example.

Constructor

Sentiment( 'moviereviews', callback )

currently the supported model name is 'moviereviews'. ml5 may support different models in the future.

Parameters

  • model - A string to the path of the JSON model.
  • callback - Optional. A callback function that is called once the model has loaded. If no callback is provided, it will return a promise that will be resolved once the model has loaded.

Properties

.ready

Boolean value that specifies if the model has loaded.

.model

The model being used.

Methods

.predict(text);

Scores the sentiment of given text with a value between 0 ("negative") and 1 ("positive").

text - string of text to predict

Source

/src/Sentiment/