-
Notifications
You must be signed in to change notification settings - Fork 1
/
tone-analyzer.js
112 lines (93 loc) · 3.2 KB
/
tone-analyzer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Copyright (c) 2016 Ali Lokhandwala <[email protected]>. 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.
*/
/** @namespace tone-analyzer */
/** @module watson/tone-analyzer */
'use strict';
const ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
const debug = require('debug')('servicekit:tone-analyzer');
const noop = () => 0;
exports = module.exports = create;
exports.newAnalyzer = _newTA;
//--
/**
* Helper creates a new instance of the watson service.
* @private
*/
function _newTA(config) {
return new ToneAnalyzerV3({
username: config.username,
password: config.password,
version: 'v3',
version_date: config.version_date
});
}
/**
* The tone analyzer service factory.
* @param {Object} config Configuration for the service.
* @param {string} config.username
* @param {string} config.password
* @param {string} [config.version_date] Defaults to 2017-09-21.
* @param {boolean} [config.sentences] Enable or disable sentence level analysis. Default true.
* @param {string} [config.content_type] Input content type and charset. Default 'text/plain;charset=utf-8'
* @return {Function} The tone analyzer service
*/
function create(config) {
config.version_date = config.version_date || '2017-09-21';
config.content_type = config.content_type || 'text/plain;charset=utf-8';
var tone_analyzer = exports.newAnalyzer(config);
return tone;
//--
/**
* Analyse general tone for sentences or documents.
* @memberOf tone-analyzer
* @async
* @param {string} text The content to analyse
* @param {boolean} [sentences] Returns an analysis of each individual sentence if true
* @param {string} [content_language] Content language code, can be 'en' or 'fr'. Default 'en'
* @param {function} cb callback(err, result)
*
* @example
* tone('This is great', cb)
*/
function tone(text, sentences, content_language, cb) {
if (typeof (content_language) === 'function') {
cb = content_language;
content_language = 'en';
}
if (typeof (sentences) === 'function') {
cb = sentences;
sentences = config.sentences;
content_language = 'en';
}
if (typeof (sentences) === 'string') {
content_language = sentences;
sentences = config.sentences;
}
if (!cb) cb = noop;
var parameters = {
tone_input: text,
sentences: sentences === false ? false : true,
content_type: config.content_type,
content_language: content_language
};
debug('tone analyzer parameters', parameters);
tone_analyzer.tone(parameters, function (err, tone) {
if (err) return cb(err);
debug('tone analyzer result', tone);
cb(null, tone);
});
}
}