Skip to content

Instantly share code, notes, and snippets.

@stopachka
Created September 21, 2017 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stopachka/d79a8b8d5971f03b616a10c16495c59d to your computer and use it in GitHub Desktop.
Save stopachka/d79a8b8d5971f03b616a10c16495c59d to your computer and use it in GitHub Desktop.
import-samples

2. Import all entities and samples on a new app

Here is a quick script, that gives all the samples, can go ahead and create the entities needed, alongside the samples on a new app:

const _ = require('lodash');
const fetch = require('node-fetch');
const samples = require('./samples');
const TOKEN = 'CHANGE_ME';

const headers = {
  Authorization: `Bearer ${TOKEN}`,
  'Content-Type': 'application/json',
};

function postEntity(id) {
  return fetch(
    'https://api.wit.ai/entities?v=20170307',
    {
      method: 'POST',
      headers,
      body: JSON.stringify({id}),
    }
  );
}

function postSamples(samples) {
  return fetch(
    'https://api.wit.ai/samples?v=20170307',
    {
      method: 'POST',
      headers,
      body: JSON.stringify(samples),
    }
  );
}

function addEntities() {
  const ids = new Set(
    _.chain(samples).flatMap(s => s.entities).map(e => e.entity).value()
  );
  [...ids].reduce(
    (p, id) => p.then(() => postEntity(id)),
    Promise.resolve()
  ).then(_ => console.log('done!'));
}

function addSamples() {
  postSamples(samples).then(
    r => console.log('yes!', r),
    r => console.log('no!', r)
  );
}

// run addEntities() first, to add all entities
// run addSamples() second, to add all samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment