css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.
 https://opensource.org/licenses/MIT
-->

<!DOCTYPE html>
<html>

<head>
  <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="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/addons/p5.dom.min.js"></script>
  <script src="https://unpkg.com/ml5@0.1.1/dist/ml5.min.js" type="text/javascript"></script>
  <script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>

</head>

<body>
  <h1>Word2Vec Suggestor</h1>
  <p id='status'>Loading Model...</p>
  <div>
    <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>
    </div>
    
  </div>

</body>

</html>
            
          
!
            
              .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.
// https://opensource.org/licenses/MIT

/* ===
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() {
  noLoop();
  noCanvas();

  // Create the Word2Vec model with pre-trained file of 10,000 words
  word2Vec = ml5.word2vec('https://rawcdn.githack.com/ml5js/ml5-data-and-models/e77bfb7484bb37babeaba4d1fbf8ea119f14cee6/models/wordvecs/common-english/wordvecs10000.json', modelLoaded);

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

  story.elt.addEventListener('keyup',(e)=>{
    //console.log(e.keyCode);
    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';
        }
        nearResults.html(output);
        $('.suggestion').on('click',(e)=>{
          const lastIndex = sentence.lastIndexOf(lastWord);
          sentence = sentence.substr(0, lastIndex);
          sentence = sentence += `${$(e.target).text()} `;
          story.value(sentence);
          $('#story').focus();
        })
      });
    }
  });
}

            
          
!
999px
🕑 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.
Loading ..................

Console