How to use ibm-watson - 10 common examples

To help you get started, we’ve selected a few ibm-watson examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github watson-developer-cloud / node-sdk / examples / natural_language_understanding.v1.js View on Github external
'use strict';

var fs = require('fs');
var NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1.js');
require('dotenv').config({ silent: true }); //  optional

var nlu = new NaturalLanguageUnderstandingV1({
  // note: if unspecified here, credentials are pulled from environment properties:
  // NATURAL_LANGUAGE_UNDERSTANDING_USERNAME &  NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD
  // username: ''.
  // password: '',
  version: '2018-04-05',
  url: 'https://gateway.watsonplatform.net/natural-language-understanding/api/'
});

var filename = '../test/resources/natural_language_understanding/energy-policy.html';
fs.readFile(filename, 'utf-8', function(file_error, file_data) {
  if (file_error) {
    console.log(file_error);
  } else {
    var options = {
      html: file_data,
      features: {
github watson-developer-cloud / node-sdk / examples / browserify / server.js View on Github external
expressBrowserify('public/client.js', {
    watch: isDev,
    debug: isDev
  })
);

app.use(express.static('public/'));

// optional: load environment properties from a .env file
dotenv.load({ silent: true });

// For local development, specify the username and password or set env properties
var ltAuthService = new AuthorizationV1({
  username: process.env.TONE_ANALYZER_USERNAME || '',
  password: process.env.TONE_ANALYZER_PASSWORD || '',
  url: ToneAnalyzerV3.URL
});

app.get('/api/token/tone_analyzer', function(req, res) {
  ltAuthService.getToken(function(err, token) {
    if (err) {
      console.log('Error retrieving token: ', err);
      return res.status(500).send('Error retrieving token');
    }
    res.send(token);
  });
});

var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
app.listen(port, function() {
  console.log('Watson browserify example server running at http://localhost:%s/', port);
});
github watson-developer-cloud / node-sdk / examples / webpack / server.js View on Github external
app.use(
  webpackDevMiddleware(compiler, {
    publicPath: '/' // Same as `output.publicPath` in most cases.
  })
);

app.use(express.static('public/'));

// optional: load environment properties from a .env file
dotenv.load({ silent: true });

// For local development, specify the username and password or set env properties
var ltAuthService = new AuthorizationV1({
  username: process.env.TONE_ANALYZER_USERNAME || '',
  password: process.env.TONE_ANALYZER_PASSWORD || '',
  url: ToneAnalyzerV3.URL
});

app.get('/api/token/tone_analyzer', function(req, res) {
  ltAuthService.getToken(function(err, token) {
    if (err) {
      console.log('Error retrieving token: ', err);
      return res.status(500).send('Error retrieving token');
    }
    res.send(token);
  });
});

var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
app.listen(port, function() {
  console.log('Watson browserify example server running at http://localhost:%s/', port);
});
github watson-developer-cloud / speech-javascript-sdk / examples / server.js View on Github external
);

// set up webpack-dev-middleware to serve Webpack bundles for examples
const compiler = webpack(webpackConfig);
app.use(
  webpackDevMiddleware(compiler, {
    publicPath: '/' // Same as `output.publicPath` in most cases.
  })
);

const sttCredentials = Object.assign(
  {
    username: process.env.SPEECH_TO_TEXT_USERNAME, // or hard-code credentials here
    password: process.env.SPEECH_TO_TEXT_PASSWORD,
    iam_apikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY, // if using an RC service
    url: process.env.SPEECH_TO_TEXT_URL ? process.env.SPEECH_TO_TEXT_URL : SpeechToTextV1.URL
  },
  vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
);

// speech to text token endpoint
app.use('/api/speech-to-text/token', function(req, res) {
  const sttAuthService = new AuthorizationV1(sttCredentials);
  sttAuthService.getToken(function(err, response) {
    if (err) {
      console.log('Error retrieving token: ', err);
      res.status(500).send('Error retrieving token');
      return;
    }
    const token = response.token || response;
    if (process.env.SPEECH_TO_TEXT_IAM_APIKEY) {
      res.json({ accessToken: token, url: sttCredentials.url });
github watson-developer-cloud / speech-javascript-sdk / examples / server.js View on Github external
const token = response.token || response;
    if (process.env.SPEECH_TO_TEXT_IAM_APIKEY) {
      res.json({ accessToken: token, url: sttCredentials.url });
    } else {
      res.json({ token: token, url: sttCredentials.url });
    }
  });
});

// text to speech token endpoint
const ttsCredentials = Object.assign(
  {
    username: process.env.TEXT_TO_SPEECH_USERNAME, // or hard-code credentials here
    password: process.env.TEXT_TO_SPEECH_PASSWORD,
    iam_apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY, // if using an RC service
    url: process.env.TEXT_TO_SPEECH_URL ? process.env.TEXT_TO_SPEECH_URL : TextToSpeechV1.URL
  },
  vcapServices.getCredentials('text_to_speech') // pulls credentials from environment in bluemix, otherwise returns {}
);
app.use('/api/text-to-speech/token', function(req, res) {
  const ttsAuthService = new AuthorizationV1(ttsCredentials);
  ttsAuthService.getToken(function(err, response) {
    if (err) {
      console.log('Error retrieving token: ', err);
      res.status(500).send('Error retrieving token');
      return;
    }
    const token = response.token || response;
    if (process.env.TEXT_TO_SPEECH_IAM_APIKEY) {
      res.json({ accessToken: token, url: ttsCredentials.url });
    } else {
      res.json({ token: token, url: ttsCredentials.url });
github watson-developer-cloud / natural-language-classifier-nodejs / app.js View on Github external
*/

const express = require('express');

const app = express();
const NaturalLanguageClassifierV1 = require('ibm-watson/natural-language-classifier/v1');
const { IamAuthenticator } = require('ibm-watson/auth');

// Bootstrap application settings
require('./config/express')(app);

// Create the service wrapper

const classifier = new NaturalLanguageClassifierV1({
  version: '2018-04-05',
  authenticator: new IamAuthenticator({
    apikey: process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY || '',
  }),
  url: process.env.NATURAL_LANGUAGE_CLASSIFIER_URL,
});

app.get('/', (req, res) => {
  res.render('index', {
    showHeader: !(req.query.hide_header == 'true' || req.query.hide_header == '1'), // eslint-disable-line
  });
});

/**
 * Classify text
 */
app.post('/api/classify', (req, res, next) => {
  classifier.classify({
github watson-developer-cloud / natural-language-understanding-nodejs / app.js View on Github external
* Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const express = require('express');
const NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1.js');
const { IamAuthenticator } = require('ibm-watson/auth');

const app = express();
// Create the service wrapper
const nlu = new NaturalLanguageUnderstandingV1({
  version: '2018-04-05',
  authenticator: new IamAuthenticator({
    apikey: process.env.NATURAL_LANGUAGE_UNDERSTANDING_IAM_APIKEY || 'type-key-here',
  }),
  url: process.env.NATURAL_LANGUAGE_UNDERSTANDING_URL,
});

// setup body-parser
const bodyParser = require('body-parser');

app.use(bodyParser.json());

// Bootstrap application settings
require('./config/express')(app);

app.get('/', (req, res) => {
  res.render('index');
});
github watson-developer-cloud / assistant-intermediate / app.js View on Github external
var SearchDocs = require('./functions/searchDocs');
var searchDocs = new SearchDocs();

var BankFunctions = require('./functions/bankFunctions');
var bankFunctions = new BankFunctions();

var app = express();

// Bootstrap application settings
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json());

// Create the service wrapper
var assistant = new AssistantV2({
  version: '2019-02-28',
  authenticator: new IamAuthenticator({
    apikey: process.env.ASSISTANT_IAM_APIKEY
  }),
  url: process.env.ASSISTANT_IAM_URL,
});

var date = new Date();
date.setMonth(date.getMonth() + 1);
var initContext = {
  skills: {
    'main skill': {
      user_defined: {
        acc_minamt: 50,
        acc_currbal: 430,
        acc_paydue: date.getFullYear() + '-' + (date.getMonth() + 1) + '-26 12:00:00',
        accnames: [
          5624,
github watson-developer-cloud / text-to-speech-nodejs / app.js View on Github external
const express = require('express');

const app = express();
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1.js');
const { IamAuthenticator } = require('ibm-watson/auth');

const textToSpeech = new TextToSpeechV1({
  version: '2018-04-05',
  authenticator: new IamAuthenticator({
    apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY || 'type-key-here',
  }),
  url: process.env.TEXT_TO_SPEECH_URL,
});

// Bootstrap application settings
require('./config/express')(app);

const getFileExtension = (acceptQuery) => {
  const accept = acceptQuery || '';
  switch (accept) {
    case 'audio/ogg;codecs=opus':
    case 'audio/ogg;codecs=vorbis':
      return 'ogg';
    case 'audio/wav':
      return 'wav';
github watson-developer-cloud / discovery-nodejs / app.js View on Github external
const DISCOVERY_ENVIRONMENT_ID = 'system';
const DISCOVERY_COLLECTION_ID = 'news';

const DiscoveryV1 = require('ibm-watson/discovery/v1');
const { IamAuthenticator } = require('ibm-watson/auth');

// Create the service wrapper
const discovery = new DiscoveryV1({
  version: '2019-02-28',
  authenticator: new IamAuthenticator({
    apikey: process.env.DISCOVERY_IAM_APIKEY,
  }),
  url: process.env.DISCOVERY_URL,
});

// Bootstrap application settings
const express = require('express');
const path = require('path');
const queryBuilder = require('./src/query-builder');

const app = express();
require('./config/express')(app);

function getWidgetQuery(request) {
  const { widgetQueries } = request.query;