diff --git a/lib/api/natural-language-understanding.js b/lib/api/natural-language-understanding.js new file mode 100755 index 0000000..04da0d8 --- /dev/null +++ b/lib/api/natural-language-understanding.js @@ -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); + } + }) + } +}; diff --git a/lib/controller.js b/lib/controller.js index 8670f76..db7483a 100755 --- a/lib/controller.js +++ b/lib/controller.js @@ -21,7 +21,8 @@ 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'); @@ -29,7 +30,7 @@ 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)); diff --git a/test/integration/test.apis.natural-language-understanding.js b/test/integration/test.apis.natural-language-understanding.js new file mode 100755 index 0000000..c8a4f76 --- /dev/null +++ b/test/integration/test.apis.natural-language-understanding.js @@ -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)); + } + }); + }); +});