彼得,
故宮命令應該已經創建了一個包含沃森包裝的API目錄nodes_module。 您仍然需要編寫一個主要的Node.js文件來調用API。 下面的例子是使用語言翻譯沃森服務Node.js的文件:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var watson = require('watson-developer-cloud');
var language_translation = watson.language_translation({
username: '<username>',
password: '<password>',
version: 'v2'
});
language_translation.translate({
text: 'A sentence must have a verb', source : 'en', target: 'es' },
function (err, translation) {
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(translation, null, 2));
});
language_translation.identify({
text: 'The language translation service takes text input and identifies the language used.' },
function (err, language) {
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(language, null, 2));
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
它需要快遞,所以你必須先安裝快車和運行Node.js應用程式(假設你保存上述文件作爲app.js,還需要進入屈臣氏的服務用戶名和密碼在文章中解釋你是以下):
npm install express
node app.js
輸出與此類似:
Example app listening at http://:::3000
{
"translations": [
{
"translation": "Una sentencia debe tener un verbo"
}
],
"word_count": 6,
"character_count": 27
}
{
"languages": [
{
"language": "en",
"confidence": 0.913647
},
{
"language": "id",
"confidence": 0.0323458
},
{
"language": "sv",
"confidence": 0.0156442
},
{
"language": "fi",
"confidence": 0.011028
},
{
"language": "is",
"confidence": 0.00709899
},
{
"language": "nn",
"confidence": 0.00553699
},
{
"language": "ht",
"confidence": 0.00408621
},
{
"language": "et",
"confidence": 0.00119016
},
{
"language": "lv",
"confidence": 0.00115617
},
{
"language": "de",
"confidence": 0.00114441
},
{
"language": "tr",
"confidence": 0.00111117
},
{
"language": "ro",
"confidence": 0.000802137
},
{
"language": "lt",
"confidence": 0.000801177
},
{
"language": "cs",
"confidence": 0.000613459
},
{
"language": "sq",
"confidence": 0.000474419
},
{
"language": "ku",
"confidence": 0.000468358
},
{
"language": "bs",
"confidence": 0.000452145
},
{
"language": "sk",
"confidence": 0.000396557
},
{
"language": "nb",
"confidence": 0.000324082
},
{
"language": "hu",
"confidence": 0.000292984
},
{
"language": "fr",
"confidence": 0.000289142
},
{
"language": "da",
"confidence": 0.000274609
},
{
"language": "eo",
"confidence": 0.000194707
},
{
"language": "eu",
"confidence": 0.000167031
},
{
"language": "vi",
"confidence": 0.000147763
},
{
"language": "az",
"confidence": 0.000121994
},
{
"language": "af",
"confidence": 0.000111069
},
{
"language": "nl",
"confidence": 0.0000278278
},
{
"language": "pl",
"confidence": 0.0000243381
},
{
"language": "pt",
"confidence": 0.0000127483
},
{
"language": "es",
"confidence": 0.0000112067
},
{
"language": "ja",
"confidence": 0.0000018683
},
{
"language": "it",
"confidence": 5.87651e-7
},
{
"language": "ko",
"confidence": 4.27166e-7
},
{
"language": "zh",
"confidence": 1.33964e-7
},
{
"language": "zh-TW",
"confidence": 6.36584e-8
},
{
"language": "el",
"confidence": 3.10395e-8
},
{
"language": "hi",
"confidence": 1.61209e-8
},
{
"language": "ur",
"confidence": 3.72468e-9
},
{
"language": "mn",
"confidence": 3.26788e-9
},
{
"language": "ru",
"confidence": 2.58127e-9
},
{
"language": "he",
"confidence": 1.91823e-9
},
{
"language": "km",
"confidence": 1.60638e-9
},
{
"language": "ar",
"confidence": 1.18306e-9
},
{
"language": "pa",
"confidence": 1.16591e-9
},
{
"language": "bn",
"confidence": 9.64752e-10
},
{
"language": "so",
"confidence": 9.41827e-10
},
{
"language": "ka",
"confidence": 7.88174e-10
},
{
"language": "uk",
"confidence": 5.64222e-10
},
{
"language": "ps",
"confidence": 3.55452e-10
},
{
"language": "ky",
"confidence": 3.32569e-10
},
{
"language": "fa",
"confidence": 2.74777e-10
},
{
"language": "ta",
"confidence": 2.7293e-10
},
{
"language": "be",
"confidence": 2.71734e-10
},
{
"language": "bg",
"confidence": 2.33392e-10
},
{
"language": "te",
"confidence": 2.0307e-10
},
{
"language": "ml",
"confidence": 1.92155e-10
},
{
"language": "kk",
"confidence": 1.67796e-10
},
{
"language": "hy",
"confidence": 1.42515e-10
},
{
"language": "gu",
"confidence": 1.21276e-10
},
{
"language": "ba",
"confidence": 6.74764e-11
},
{
"language": "cv",
"confidence": 4.22054e-11
}
]
}
請參閱我的答案以獲取完整的解決方案 –