Sentiment

illustration of hand drawing a heart

Description

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.

Quickstart

  1. // Create a new Sentiment method
  2. const sentiment = ml5.sentiment('movieReviews', modelReady);
  3. // When the model is loaded
  4. function modelReady() {
  5. // model is ready
  6. console.log('Model Loaded!');
  7. }
  8. // make the prediction
  9. const prediction = sentiment.predict(text);
  10. console.log(prediction);

Usage

Initialize

  1. const magic = ml5.Sentiment(model, ?callback);

Parameters

  • model: REQUIRED. Defaults to ‘moviereviews’. You can also use a path to a manifest.json file via a relative or absolute path.
  • 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()

Given a number, will make magicSparkles

  1. sentiment.predict(text);

📥 Inputs

  • text: Required. String. A string of text to predict

📤 Outputs

  • Object: Scores the sentiment of given text with a value between 0 (“negative”) and 1 (“positive”).

Examples

p5.js

p5 web editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

No tutorials yet - contribute one today!

Acknowledgements

Contributors:

  • Itay Niv

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code

/src/Sentiment/