Skip to content

Commit

Permalink
add variant words
Browse files Browse the repository at this point in the history
  • Loading branch information
JKS9 committed Jan 31, 2025
1 parent e43e6f0 commit b132efe
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
discussion.md
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 7 additions & 23 deletions src/profanityChecker.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { ProfanityConfig } from './profanityConfig.js';
import { parseString } from './utils/normalize.js';

export class ProfanityChecker extends ProfanityConfig {
/**
* Normalize the input sentence by removing unwanted characters and splitting it into an array of words.
* @param sentence - The sentence to normalize.
* @returns {string[]} - An array of normalized words.
*/
public static normalizeSentence(sentence: string): string[] {
return sentence
.replace(/[^a-zA-Z0-9àâäéèêëîïôöùûüç\s.,!?]/gu, ' ') // Ajout du modificateur 'u' pour Unicode et 'g' pour global
.trim() // Supprime les espaces en début/fin.
.toLowerCase() // Convertir en minuscule.
.split(/\s+/); // Divise en mots.
}

/**
* Check if the sentence contains any bad words.
* @param sentence - The sentence to check.
* @returns {boolean} - True if bad words are found, otherwise false.
*/
public static hasBadWords(sentence: string): boolean {
const words = this.normalizeSentence(sentence);
const words = parseString(sentence);

return words.some((word) => {
const cleanedWord = word.replace(/[.,!?]$/gu, '').toLowerCase();
const cleanedWord = word.replace(/[.,!?]/gu, '').toLowerCase();
return (
this.badWordsSet.has(cleanedWord) &&
!this.whiteListWordsSet.has(cleanedWord)
Expand All @@ -37,15 +25,11 @@ export class ProfanityChecker extends ProfanityConfig {
* @returns {string} - The censored sentence.
*/
public static censoredSentence(sentence: string): string {
const words = this.normalizeSentence(sentence);
const censoredWords = words.map((word) => {
const cleanedWord = word.replace(/[.,!?]$/gu, '').toLowerCase();
const words = parseString(sentence);

if (
this.badWordsSet.has(cleanedWord) &&
!this.whiteListWordsSet.has(cleanedWord)
) {
return this.censoreSet.repeat(cleanedWord.length) + word.slice(cleanedWord.length); // Replace with asterisks, keeping punctuation.
const censoredWords = words.map((word) => {
if (this.badWordsSet.has(word) && !this.whiteListWordsSet.has(word)) {
return this.censoreSet.repeat(word.length); // Replace with asterisks, keeping punctuation.
}
return word; // Keep the original word if not a bad word.
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/profanityCheckerFr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { test, expect, beforeAll } from 'vitest';
beforeAll(() => {
ProfanityConfig.changeCensoredWords('*');
ProfanityConfig.addBadWords([
'salope',
'Salope',
'putain de merde',
'con',
'sale pute',
Expand Down Expand Up @@ -139,13 +139,13 @@ test('censoredSentence censors bad phrase with multiple bad words', () => {

test('censoredSentence handles punctuation', () => {
expect(ProfanityChecker.censoredSentence('ceci est une phrase, conne!')).toBe(
'ceci est une phrase, *****!',
'ceci est une phrase , ***** !',
);
});

test('censoredSentence handles mixed case', () => {
expect(ProfanityChecker.censoredSentence('Ceci est une Salope')).toBe(
'ceci est une ******',
'Ceci est une ******',
);
});

Expand Down
11 changes: 11 additions & 0 deletions src/utils/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const parseString = (input: string): string[] => {
if (!input || input.length === 0) {
return [];
}

input = input.replace(/'/g, " ' ");

const words = input.match(/[\wÀ-ÿ]+|[.,!?;:]/g) || [];

return words.map((word) => word.trim()).filter((word) => word.length > 0);
};
92 changes: 92 additions & 0 deletions src/words/words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const badWords: string[] = [
'mûrde',
'mürde',
'myrde',
'putain',
'merda',
'merdä',
'merdà',
Expand Down Expand Up @@ -117,6 +118,96 @@ export const badWords: string[] = [
'me.rde',
'me<rde',
'me>rde',
'Merde',
'Con',
'Salope',
'Putain',
'Enculé',
'Bordel',
'Nique',
'Conne',
'Chié',
'Fils de pute',
'Va te faire foutre',
'Batard',
'Clochard',
'Connard',
'Salopard',
'Branler',
'Pédé',
'Gros con',
'Tapette',
'Grosse vache',
'Sale pute',
'Pute',
'Sucer',
'Fion',
'Cogne',
'Trou du cul',
'Zoophile',
'Raciste',
'Homophobe',
'Grosse merde',
'Connasse',
'Andouille',
'Pauvre con',
'Gouine',
'Bouffon',
'Nique ta mère',
'Saloperie',
'Abruti',
'Sac à merde',
'Tarlouze',
'Fils de chien',
'Merdeux',
'Enculard',
'Sauropode',
'Boudin',
'Conasse',
'Grosse tache',
'Taré',
'Putain de merde',
'Foufoune',
'Bâtard',
'Pouffiasse',
'Blessé',
'Culé',
'Peste',
'Gogole',
'Pétasse',
'Crétin',
'Merdeuse',
'Gros tas',
'Bitch',
'Gros nul',
'Paon',
'Gros porc',
'Bête',
'Sale bâtard',
'Soupe au lait',
'Bordélique',
'Barjot',
'Stronzo',
'Suceur',
'Imbécile',
'Clodo',
'Culs',
'Foutre',
'Pisse',
'Pénis',
'Cochon',
'Fille de pute',
'Baiseur',
'Ordure',
'Traînée',
'Gros suceur',
'Gourde',
'Chiffon',
'Glandeur',
'Macho',
'Sale con',
'Carnage',
'Ducon',
'me/rde',
'me?rde',
'mer!de',
Expand Down Expand Up @@ -276,6 +367,7 @@ export const badWords: string[] = [
'co/n',
'co?n',
'ocn',
'Salope',
'salope',
'sälope',
'sàlope',
Expand Down

0 comments on commit b132efe

Please sign in to comment.