-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIdentifyLanguage.js
More file actions
37 lines (33 loc) · 1009 Bytes
/
IdentifyLanguage.js
File metadata and controls
37 lines (33 loc) · 1009 Bytes
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
let rp = require("request-promise");
let _ = require("lodash");
function main(params) {
console.log(JSON.stringify(params))
if (params.payload.input.text !== '') {
const options = { method: 'POST',
url: 'https://api.us-south.language-translator.watson.cloud.ibm.com/instances/<instance>/v3/identify?version=2018-05-01',
auth: {
'username': 'apikey',
'password': ''
},
headers: {
"Content-Type":"text/plain"
},
body: [
params.payload.input.text
],
json: true,
};
return rp(options)
.then(res => {
const confidence = _.get(res, 'languages[0].confidence' );
const language = confidence > 0.5 ? _.get(res, 'languages[0].language') : 'en';
_.set(params, 'payload.context.skills["main skill"].user_defined["language"]', language);
console.log(JSON.stringify(params))
return params;
})
}
else {
_.set(params, 'payload.context.skills["main skill"].user_defined["language"]', 'none' )
return params
}
};