mirror of
https://github.com/wassname/talk.git
synced 2026-07-05 15:36:35 +08:00
38 lines
842 B
JavaScript
38 lines
842 B
JavaScript
const fetch = require('node-fetch');
|
|
|
|
const API_ENPOINT = 'https://commentanalyzer.googleapis.com/v1alpha1';
|
|
|
|
async function getScores(apiKey, text) {
|
|
const response = await fetch(`${API_ENPOINT}/comments:analyze?key=${apiKey}`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
comment: {
|
|
text,
|
|
},
|
|
|
|
// TODO: support other languages.
|
|
languages: ['en'],
|
|
requestedAttributes: {
|
|
TOXICITY: {},
|
|
SEVERE_TOXICITY: {},
|
|
}
|
|
}),
|
|
});
|
|
const data = await response.json();
|
|
return {
|
|
TOXICITY: {
|
|
summaryScore: data.attributeScores.TOXICITY.summaryScore.value
|
|
},
|
|
SEVERE_TOXICITY: {
|
|
summaryScore: data.attributeScores.SEVERE_TOXICITY.summaryScore.value
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
getScores,
|
|
};
|