我正在关注'Watson Quickstart for Node.js'找到here,但我在使用他们提供的npm命令安装Watson Developer Cloud Node.js包装程序包时遇到了问题。
这是输出: cmd output after npm command
之后,运行命令的目录中不会出现任何文件。有没有人对如何安装Watson Developer Cloud包装器以通过Bluemix开始使用Node.js开发Watson应用程序有任何建议?
答案 0 :(得分:3)
彼得,
您所指的页面版本已在三个多月前完全重写。重写应解决您遇到的问题。它似乎在同一个网址上提供:
我不确定为什么你会看到那个旧版本的那个页面。也许它是你缓存中的陈旧版本?请尝试刷新页面,看看是否有帮助。
答案 1 :(得分:1)
好的,因此文档页面存在许多问题。我将它们与解决方案一起列在下面。在解决了以下缺点之后,我能够得到这个例子。
示例中的源代码使用完全过时的请求参数 - 请参阅下面更新的源代码。
var watson = require(' watson-developer-cloud');
var machine_translation = watson.language_translation({
username: 'user_name',
password: 'password',
version: 'v2'
});
//the attributes from and to in the example provided are actually replaced by source and target
machine_translation.translate({
text: 'A sentence must have a verb', source : 'enus', target: 'eses' },
function (err, response) {
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(response, null, 2));
});
希望这可以解决您的问题。当然,示例中显示的代码只是一个入门代码 - 如果您想开发一个复杂的应用程序,那么您需要引入其他依赖项,如expressjs,jade等,然后明确地实现它们。
如果您想参考此服务的Github存储库示例,请访问this链接。
答案 2 :(得分:0)
彼得,
npm命令应该在该目录中创建了一个包含Watson包装器API的nodes_module。 您仍然需要编写一个主Node.js文件来调用API。 下面的示例是使用Language Translation Watson服务的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);
});
它需要express,所以你必须先安装express并运行Node.js应用程序(假设你将上面的文件保存为app.js,你也需要输入Watson服务用户名和密码,如文章中所述)正在跟随):
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
}
]
}