Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use faster profanity checker #1095

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 34 additions & 43 deletions Node-1st-gen/text-moderation/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,73 +13,64 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
"use strict";

const functions = require('firebase-functions/v1');
const capitalizeSentence = require('capitalize-sentence');
const Filter = require('bad-words');
const badWordsFilter = new Filter();
const functions = require("firebase-functions/v1");
const capitalizeSentence = require("capitalize-sentence");
const { isProfane, replaceProfanities } = import("no-profanity");

// Moderates messages by lowering all uppercase messages and removing swearwords.
exports.moderator = functions.database.ref('/messages/{messageId}').onWrite((change) => {
const message = change.after.val();
exports.moderator = functions.database.ref("/messages/{messageId}").onWrite((change) => {
const message = change.after.val();

if (message && !message.sanitized) {
// Retrieved the message values.
functions.logger.log('Retrieved message content: ', message);
if (message && !message.sanitized) {
// Retrieved the message values.
functions.logger.log("Retrieved message content: ", message);

// Run moderation checks on on the message and moderate if needed.
const moderatedMessage = moderateMessage(message.text);
// Run moderation checks on on the message and moderate if needed.
const moderatedMessage = moderateMessage(message.text);

// Update the Firebase DB with checked message.
functions.logger.log(
'Message has been moderated. Saving to DB: ',
moderatedMessage
);
return change.after.ref.update({
text: moderatedMessage,
sanitized: true,
moderated: message.text !== moderatedMessage,
});
}
return null;
// Update the Firebase DB with checked message.
functions.logger.log("Message has been moderated. Saving to DB: ", moderatedMessage);
return change.after.ref.update({
text: moderatedMessage,
sanitized: true,
moderated: message.text !== moderatedMessage,
});
}
return null;
});

// Moderates the given message if appropriate.
function moderateMessage(message) {
// Re-capitalize if the user is Shouting.
if (isShouting(message)) {
functions.logger.log('User is shouting. Fixing sentence case...');
message = stopShouting(message);
}
// Re-capitalize if the user is Shouting.
if (isShouting(message)) {
functions.logger.log("User is shouting. Fixing sentence case...");
message = stopShouting(message);
}

// Moderate if the user uses SwearWords.
if (containsSwearwords(message)) {
functions.logger.log('User is swearing. moderating...');
message = moderateSwearwords(message);
}
// Moderate if the user uses SwearWords.
if (isProfane(message)) {
functions.logger.log("User is swearing. moderating...");
message = moderateSwearwords(message);
}

return message;
}

// Returns true if the string contains swearwords.
function containsSwearwords(message) {
return message !== badWordsFilter.clean(message);
return message;
}

// Hide all swearwords. e.g: Crap => ****.
function moderateSwearwords(message) {
return badWordsFilter.clean(message);
return replaceProfanities(message);
}

// Detect if the current message is shouting. i.e. there are too many Uppercase
// characters or exclamation points.
function isShouting(message) {
return message.replace(/[^A-Z]/g, '').length > message.length / 2 || message.replace(/[^!]/g, '').length >= 3;
return message.replace(/[^A-Z]/g, "").length > message.length / 2 || message.replace(/[^!]/g, "").length >= 3;
}

// Correctly capitalize the string as a sentence (e.g. uppercase after dots)
// and remove exclamation points.
function stopShouting(message) {
return capitalizeSentence(message.toLowerCase()).replace(/!+/g, '.');
return capitalizeSentence(message.toLowerCase()).replace(/!+/g, ".");
}
48 changes: 24 additions & 24 deletions Node-1st-gen/text-moderation/functions/package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"name": "text-moderation-functions",
"description": "Moderate text using Firebase Functions",
"dependencies": {
"bad-words": "^3.0.4",
"capitalize-sentence": "^0.1.5",
"firebase-admin": "^11.9.0",
"firebase-functions": "^4.4.1"
},
"devDependencies": {
"eslint": "^8.40.0"
},
"scripts": {
"lint": "./node_modules/.bin/eslint --max-warnings=0 .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
},
"engines": {
"node": "18"
},
"private": true
"name": "text-moderation-functions",
"description": "Moderate text using Firebase Functions",
"dependencies": {
"capitalize-sentence": "^0.1.5",
"firebase-admin": "^11.9.0",
"firebase-functions": "^4.4.1",
"no-profanity": "^1.4.2"
},
"devDependencies": {
"eslint": "^8.40.0"
},
"scripts": {
"lint": "./node_modules/.bin/eslint --max-warnings=0 .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
},
"engines": {
"node": "18"
},
"private": true
}
56 changes: 28 additions & 28 deletions Node/quickstarts/callable-functions/functions/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"lintfix": "eslint . --fix",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"compile": "cp ../../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
},
"engines": {
"node": "18"
},
"main": "index.js",
"dependencies": {
"bad-words": "^3.0.4",
"capitalize-sentence": "^0.1.5",
"firebase-admin": "^11.9.0",
"firebase-functions": "^4.4.1"
},
"devDependencies": {
"eslint": "^8.40.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^3.1.0"
},
"private": true
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"lintfix": "eslint . --fix",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"compile": "cp ../../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
},
"engines": {
"node": "18"
},
"main": "index.js",
"dependencies": {
"capitalize-sentence": "^0.1.5",
"firebase-admin": "^11.9.0",
"firebase-functions": "^4.4.1",
"no-profanity": "^1.4.2"
},
"devDependencies": {
"eslint": "^8.40.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^3.1.0"
},
"private": true
}
19 changes: 4 additions & 15 deletions Node/quickstarts/callable-functions/functions/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"use strict";

const capitalizeSentence = require("capitalize-sentence");
const Filter = require("bad-words");
const badWordsFilter = new Filter();
const {isProfane, replaceProfanities} = import("no-profanity");

// Sanitizes the given text if needed by replacing bad words with '*'.
exports.sanitizeText = (text) => {
Expand All @@ -28,30 +27,21 @@ exports.sanitizeText = (text) => {
}

// Moderate if the user uses SwearWords.
if (containsSwearwords(text)) {
if (isProfane(text)) {
console.log("User is swearing. moderating...");
text = replaceSwearwords(text);
}

return text;
};

/**
* Returns true if the string contains swearwords.
* @param {string} message
* @return {boolean}
*/
function containsSwearwords(message) {
return message !== badWordsFilter.clean(message);
}

/**
* Hide all swearwords. e.g: Crap => ****.
* @param {string} message
* @return {string}
*/
function replaceSwearwords(message) {
return badWordsFilter.clean(message);
return replaceProfanities(message);
}

/**
Expand All @@ -61,8 +51,7 @@ function replaceSwearwords(message) {
* @return {boolean}
*/
function isShouting(message) {
return message.replace(/[^A-Z]/g, "").length > message.length / 2 ||
message.replace(/[^!]/g, "").length >= 3;
return message.replace(/[^A-Z]/g, "").length > message.length / 2 || message.replace(/[^!]/g, "").length >= 3;
}

/**
Expand Down