Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions lib/api/natural-language-understanding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

'use strict';

var NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');
var natural_language_understanding = new NaturalLanguageUnderstandingV1({
username: process.env.NATURAL_LANGUAGE_UNDERSTANDING_USERNAME,
password: process.env.NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD,
version_date: '2017-02-27'
});
var debug = require('debug')('bot:api:natural-language-understanding');

/**
* Returns true if the entity.type is a city
* @param {Object} entity NLU entity
* @return {Boolean} True if entity.type is a city
*/
function isCity(entity) {
return entity.type === 'Location' && 'disambiguation' in entity && entity.disambiguation.subtype.indexOf("City") != -1;
}

/**
* Returns only the name property
* @param {Object} entity NLU entity
* @return {Object} Only the name property
*/
function onlyName(entity) {
return { name: entity.text };
}

module.exports = {
/**
* Extract the city mentioned in the input text
* @param {Object} params.text The text
* @param {Function} callback The callback
* @return {void}
*/
extractCity: function(params, callback) {
params.language = 'en';
params.features={'entities':{}};
natural_language_understanding.analyze(params, function(err, response) {
debug('text: %s, entities: %s', params.text, JSON.stringify(response.entities));
if (err) {
callback(err);
}
else {
var cities = response.entities.filter(isCity).map(onlyName);
callback(null, cities.length > 0 ? cities[0]: null);
}
})
}
};
5 changes: 3 additions & 2 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ var extend = require('extend');
var Promise = require('bluebird');
var conversation = require('./api/conversation');
var weather = require('./api/weather');
var alchemyLanguage = require('./api/alchemy-language');
var NaturalLanguageUnderstanding = require('./api/natural-language-understanding');

var cloudant = require('./api/cloudant');
var format = require('string-template');
var pick = require('object.pick');

var sendMessageToConversation = Promise.promisify(conversation.message.bind(conversation));
var getUser = Promise.promisify(cloudant.get.bind(cloudant));
var saveUser = Promise.promisify(cloudant.put.bind(cloudant));
var extractCity = Promise.promisify(alchemyLanguage.extractCity.bind(alchemyLanguage));
var extractCity = Promise.promisify(NaturalLanguageUnderstanding.extractCity.bind(NaturalLanguageUnderstanding));
var getForecast = Promise.promisify(weather.forecastByGeoLocation.bind(weather));
var getGeoLocation = Promise.promisify(weather.geoLocation.bind(weather));

Expand Down
50 changes: 50 additions & 0 deletions test/integration/test.apis.natural-language-understanding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

'use strict';
require('dotenv').config({ silent: true });

var naturalLanguage = require('../../lib/api/natural-language-understanding');

describe('natural-language-understanding.js', function () {
this.timeout(3000);
this.slow(1000);
it('should return Miami if is detected', function (done) {
var params = {
text: 'I live in Miami'
};
naturalLanguage.extractCity(params, function (err, city) {
if (city.name === 'Miami') {
done();
} else {
done(JSON.stringify(city));
}
});
});

it('should return empty if no city is mentioned', function (done) {
var params = {
text: 'We don\'t have cities here'
};
naturalLanguage.extractCity(params, function (err, city) {
if (!city) {
done();
} else {
done(JSON.stringify(city));
}
});
});
});