Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

 Copyright (c) 2018 ml5
 This software is released under the MIT License.

<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Word2Vec example with p5.js. Using a pre-trained model on common English words.</title>

  <script src=""></script>
  <script src=""></script>
  <script src="" type="text/javascript"></script>


  <h1>Word2Vec Suggestor</h1>
  <p id='status'>Loading Model...</p>
    <p>Begin writing. Each time you hit space ml5 will suggest alternatives for the last word you typed.</p>
    <div class="row">
      <textarea id="story"></textarea>
      <p id="results"></p>


              .row {
    margin-top: 10px;
    padding: 20px;
    outline: 2px solid #ccc;
    outline-offset: -10px;
    -moz-outline-radius: 10px;
    -webkit-outline-radius: 10px;
  #story {
    width: 100%;
    border: 0;
    min-height: 150px;

  #status {
    display: none;

  .suggestion {
    display: block;
  body {
    font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif
textarea {
  font-size: 30px;
              // Copyright (c) 2018 ml5
// This software is released under the MIT License.

/* ===
ml5 Example
Word2Vec example with p5.js. Using a pre-trained model on common English words.
=== */

let word2Vec;

function modelLoaded() {
  select('#status').html('Model Loaded');

function setup() {

  // Create the Word2Vec model with pre-trained file of 10,000 words
  word2Vec = ml5.word2vec('', modelLoaded);

  // Select all the DOM elements
  let story = select('#story');
  let nearResults = select('#results');

    if (e.keyCode === 32){ //spacebar
      let sentence = story.value(); 
      const words = sentence.split(" ");
      const lastWord = words[words.length -2];
      word2Vec.nearest(lastWord, (err, result) => {
        let output = '';
        if (result) {
          for (let i = 0; i < result.length; i++) {
            output += '<span class="suggestion">' + result[i].word + '</span>';
        } else {
          output = 'No word vector found';
          const lastIndex = sentence.lastIndexOf(lastWord);
          sentence = sentence.substr(0, lastIndex);
          sentence = sentence += `${$(} `;

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.