如何POST查询结果到云端特定地址的Watson音调分析器?
How to POST a query result to the Watson tone analyser hosted on the cloud at a specific address?
我有一个 SQL table 向上,其中有艺术家、歌曲和歌词列。
|id | artist | song | text|
-------------------------
|1 | ABBA | Blah | blah|
-------------------------
|...| ... | ... | ... |
我还准备了一个云托管的 Watson Tone 应用程序。我想POST每一行的歌词到这个地址的云托管Watson音调分析器:http://watson-row-analyser.eu-gb.cf.appdomain.cloud/#
然后我想用结果JSON将每首歌的情感分数添加到一个新的table:
|id | artist | song | score |
----------------------------
|1 | ABBA | Blah | joy .93|
----------------------------
|...| ... | ... | ... |
我该怎么做?如果有人可以帮助我将我编写的代码实际连接到 IBM 云地址的初始部分,以便我可以试用一些 POSTs,那将是一个很大的帮助!
我已经尝试过这里的代码:
const https = require('https')
const data = JSON.stringify({
todo: 'Buy the milk'
})
const options = {
hostname: 'flaviocopes.com',
port: 443,
path: '/todos',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
但在我的例子中,我不确定主机名是什么,也不确定端口是什么(因为它不是在本地托管,而是在 IBM 的云上)。所以我一直无法建立所需的连接。这是在正确的轨道上吗?
下面是我到目前为止的代码——我可以成功地逐行查询 table ,只需从每行中获取歌词即可。
var mysql = require('mysql');
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '<password>',
database: 'songdata'
//insecureAuth : true
});
con.connect(function(err) {
if (err) throw err;
for (var i = 0; i <= 1; i++) {
con.query(`SELECT text FROM songdata2ID WHERE id = ${i}
`, function(err, result, fields) {
if (err) throw err;
console.log(result);
});
}
});
已解决:此处不需要 POST。这是遵循 IBM Watson Tone Analyzer 的 api 端点规则的案例 https://cloud.ibm.com/apidocs/tone-analyzer
const ToneAnalyzerV3 = 要求('ibm-watson/tone-analyzer/v3');
const toneAnalyzer = new ToneAnalyzerV3({
version: '{version}',
iam_apikey: '{apikey}',
url: 'https://gateway-wdc.watsonplatform.net/tone-analyzer/api'
});
确保您首先在 IBM 上将其正确设置为应用程序。然后您将获得连接到端点所需的 url、api-密钥。
我有一个 SQL table 向上,其中有艺术家、歌曲和歌词列。
|id | artist | song | text|
-------------------------
|1 | ABBA | Blah | blah|
-------------------------
|...| ... | ... | ... |
我还准备了一个云托管的 Watson Tone 应用程序。我想POST每一行的歌词到这个地址的云托管Watson音调分析器:http://watson-row-analyser.eu-gb.cf.appdomain.cloud/#
然后我想用结果JSON将每首歌的情感分数添加到一个新的table:
|id | artist | song | score |
----------------------------
|1 | ABBA | Blah | joy .93|
----------------------------
|...| ... | ... | ... |
我该怎么做?如果有人可以帮助我将我编写的代码实际连接到 IBM 云地址的初始部分,以便我可以试用一些 POSTs,那将是一个很大的帮助!
我已经尝试过这里的代码:
const https = require('https')
const data = JSON.stringify({
todo: 'Buy the milk'
})
const options = {
hostname: 'flaviocopes.com',
port: 443,
path: '/todos',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
但在我的例子中,我不确定主机名是什么,也不确定端口是什么(因为它不是在本地托管,而是在 IBM 的云上)。所以我一直无法建立所需的连接。这是在正确的轨道上吗? 下面是我到目前为止的代码——我可以成功地逐行查询 table ,只需从每行中获取歌词即可。
var mysql = require('mysql');
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '<password>',
database: 'songdata'
//insecureAuth : true
});
con.connect(function(err) {
if (err) throw err;
for (var i = 0; i <= 1; i++) {
con.query(`SELECT text FROM songdata2ID WHERE id = ${i}
`, function(err, result, fields) {
if (err) throw err;
console.log(result);
});
}
});
已解决:此处不需要 POST。这是遵循 IBM Watson Tone Analyzer 的 api 端点规则的案例 https://cloud.ibm.com/apidocs/tone-analyzer
const ToneAnalyzerV3 = 要求('ibm-watson/tone-analyzer/v3');
const toneAnalyzer = new ToneAnalyzerV3({
version: '{version}',
iam_apikey: '{apikey}',
url: 'https://gateway-wdc.watsonplatform.net/tone-analyzer/api'
});
确保您首先在 IBM 上将其正确设置为应用程序。然后您将获得连接到端点所需的 url、api-密钥。