-
Notifications
You must be signed in to change notification settings - Fork 2
/
toxicity.js
51 lines (48 loc) · 1.77 KB
/
toxicity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const tf = require('@tensorflow/tfjs');
const toxicity = require('@tensorflow-models/toxicity');
const encoder = require('@tensorflow-models/universal-sentence-encoder');
const threshold = 0.5;
const labels = ['toxicity'];
const Build = require('newspaperjs').Build;
const Article = require('newspaperjs').Article;
function getToxicity(sentence)
{
return new Promise((resolve) => {
toxicity.load(threshold, labels).then(model => {
model.classify(sentence).then(predictions=> {
toxic = predictions[0]['results'][0]['match'];
resolve(toxic);
})
})
});
}
module.exports.test = (textURL) =>
{
console.log(textURL)
return new Promise((resolve, reject) => {
Article(textURL).then(result1 => {
var text = result1['text'].split(/\r?\n/);
text = text.sort(function (a, b) { return b.length - a.length; })[0];
let documents = text.replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");
sentenceEmbeddings = null;
toxicityResult = false;
toxicity.load(threshold, labels).then(modelToxicity => {
sentencetox = new Array(documents.length);
for (i = 0; i < documents.length; i++)
{
sentencetox[i] = getToxicity(documents[i]);
}
Promise.all(sentencetox).then((values) => {
if (values.includes(true)) toxicityResult = true;
resolve(toxicityResult)
}).catch(reason=>{
console.log(reason);
})
}).catch(reason=>{
console.log(reason);
})
}).catch(reason=>{
console.log(reason);
})
})
}