mirror of
https://github.com/wassname/talk.git
synced 2026-09-12 13:01:11 +08:00
First draft of toxic-comments
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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,
|
||||
};
|
||||
Reference in New Issue
Block a user