|
| 1 | +/** |
| 2 | + * Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var extend = require('extend'); |
| 20 | +var requestFactory = require('../../lib/requestwrapper'); |
| 21 | + |
| 22 | +function ToneAnalyzer(options) { |
| 23 | + // Default URL |
| 24 | + var defaultOptions = { |
| 25 | + url: 'https://gateway.watsonplatform.net/tone-analyzer-experimental/api' |
| 26 | + }; |
| 27 | + |
| 28 | + // Replace default options with user provided |
| 29 | + this._options = extend(defaultOptions, options); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Main API call. Returns the different tone dimensions of a text. |
| 34 | + * |
| 35 | + * @param params: An object with a string 'text' element. This is the |
| 36 | + * only field used. By this API call |
| 37 | + * |
| 38 | + * @return upon success, the callback function is called with an object |
| 39 | + * containing the different tones (emotion, writing and social), traits |
| 40 | + * and the evidence words found in the text. |
| 41 | + * |
| 42 | + * @see the API docs for a the full documentation. |
| 43 | + * |
| 44 | + */ |
| 45 | +ToneAnalyzer.prototype.tone = function(params, callback) { |
| 46 | + if (!params || !params.text){ |
| 47 | + callback(new Error('Missing required parameters: text')); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + var parameters = { |
| 52 | + options: { |
| 53 | + url: '/v2/tone', |
| 54 | + method: 'POST', |
| 55 | + body: params.text |
| 56 | + }, |
| 57 | + defaultOptions: extend(this._options, { |
| 58 | + headers: { |
| 59 | + 'accept':'application/json', |
| 60 | + 'content-type': 'text/plain' |
| 61 | + } |
| 62 | + }) |
| 63 | + }; |
| 64 | + |
| 65 | + return requestFactory(parameters, callback); |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * Returns related words for a given word (or set of words). |
| 70 | + * |
| 71 | + * @param params: An object to build the query string. It normally contains |
| 72 | + * a property "word", the term to look up. |
| 73 | + * Alternatively, one can specify a "context" (part of a phrase) and an |
| 74 | + * "index" (of the word to lookup, within "context"). |
| 75 | + * A 'limit' parameter is also accepted to limit the number of related |
| 76 | + * words suggested. |
| 77 | + * |
| 78 | + * @return upon success, the callback function is called with an object |
| 79 | + * containing related words to the ones given. Each word comes with |
| 80 | + * the semantic type and the meaning and sense of the root word, and |
| 81 | + * a weight associated to a trait. Positive weights would 'level up' |
| 82 | + * that particular tone, while negative weights would level it down. |
| 83 | + * |
| 84 | + * For example, if one wants to sound less "angry" on a message, the |
| 85 | + * suggestions with negative correlation with "Anger" (Emotion Tone) |
| 86 | + * could be replaced in the txt. |
| 87 | + * |
| 88 | + * @see the API docs for a the full documentation. |
| 89 | + * |
| 90 | + */ |
| 91 | +ToneAnalyzer.prototype.synonym = function(params, callback) { |
| 92 | + var parameters = { |
| 93 | + options: { |
| 94 | + url: '/v2/synonyms', |
| 95 | + method: 'GET', |
| 96 | + qs: params, |
| 97 | + json: true |
| 98 | + }, |
| 99 | + defaultOptions: extend(this._options, {}) |
| 100 | + }; |
| 101 | + |
| 102 | + return requestFactory(parameters, callback); |
| 103 | +}; |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +/** |
| 108 | + * Returns the different scorecards implemented by the service. |
| 109 | + * This is an array of objects with a "name" and "description" properties. |
| 110 | + * |
| 111 | + * As a first version, only a "business email" scorecard is implemented. |
| 112 | + * |
| 113 | + * @see the API docs for a the full documentation. |
| 114 | + * |
| 115 | + */ |
| 116 | +ToneAnalyzer.prototype.scorecards = function(params, callback) { |
| 117 | + var parameters = { |
| 118 | + options: { |
| 119 | + url: '/v2/scorecards', |
| 120 | + method: 'GET', |
| 121 | + json: true |
| 122 | + }, |
| 123 | + defaultOptions: extend(this._options, {}) |
| 124 | + }; |
| 125 | + |
| 126 | + return requestFactory(parameters, callback); |
| 127 | +}; |
| 128 | + |
| 129 | +module.exports = ToneAnalyzer; |
0 commit comments