allprofanity 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +765 -37
- package/dist/index.js +704 -53
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,81 +8,348 @@ export { default as tamilBadWords } from "./languages/tamil-words.js";
|
|
|
8
8
|
export { default as teluguBadWords } from "./languages/telugu-words.js";
|
|
9
9
|
export { default as brazilianBadWords } from "./languages/brazilian-words.js";
|
|
10
10
|
/**
|
|
11
|
-
* Logger interface for
|
|
11
|
+
* Logger interface for AllProfanity library logging operations.
|
|
12
|
+
*
|
|
13
|
+
* @interface Logger
|
|
14
|
+
* @description Provides a contract for logging implementations used by the AllProfanity library.
|
|
15
|
+
* Implement this interface to provide custom logging behavior (e.g., logging to files, external services).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* class CustomLogger implements Logger {
|
|
20
|
+
* info(message: string): void {
|
|
21
|
+
* // Custom info logging logic
|
|
22
|
+
* }
|
|
23
|
+
* warn(message: string): void {
|
|
24
|
+
* // Custom warning logging logic
|
|
25
|
+
* }
|
|
26
|
+
* error(message: string): void {
|
|
27
|
+
* // Custom error logging logic
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* const filter = new AllProfanity({ logger: new CustomLogger() });
|
|
31
|
+
* ```
|
|
12
32
|
*/
|
|
13
33
|
export interface Logger {
|
|
14
34
|
/**
|
|
15
|
-
* Log informational messages.
|
|
16
|
-
*
|
|
35
|
+
* Log informational messages about normal operations.
|
|
36
|
+
*
|
|
37
|
+
* @param message - The informational message to log
|
|
38
|
+
* @returns void
|
|
17
39
|
*/
|
|
18
40
|
info(message: string): void;
|
|
19
41
|
/**
|
|
20
|
-
* Log warning messages.
|
|
21
|
-
*
|
|
42
|
+
* Log warning messages about potential issues or deprecated usage.
|
|
43
|
+
*
|
|
44
|
+
* @param message - The warning message to log
|
|
45
|
+
* @returns void
|
|
22
46
|
*/
|
|
23
47
|
warn(message: string): void;
|
|
24
48
|
/**
|
|
25
|
-
* Log error messages.
|
|
26
|
-
*
|
|
49
|
+
* Log error messages about failures or critical issues.
|
|
50
|
+
*
|
|
51
|
+
* @param message - The error message to log
|
|
52
|
+
* @returns void
|
|
27
53
|
*/
|
|
28
54
|
error(message: string): void;
|
|
29
55
|
}
|
|
30
56
|
/**
|
|
31
|
-
* Configuration options for AllProfanity.
|
|
57
|
+
* Configuration options for initializing an AllProfanity instance.
|
|
58
|
+
*
|
|
59
|
+
* @interface AllProfanityOptions
|
|
60
|
+
* @description Comprehensive configuration object for customizing profanity detection behavior,
|
|
61
|
+
* algorithm selection, performance optimizations, and logging.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const filter = new AllProfanity({
|
|
66
|
+
* languages: ['english', 'french'],
|
|
67
|
+
* enableLeetSpeak: true,
|
|
68
|
+
* strictMode: true,
|
|
69
|
+
* algorithm: {
|
|
70
|
+
* matching: 'hybrid',
|
|
71
|
+
* useBloomFilter: true
|
|
72
|
+
* },
|
|
73
|
+
* performance: {
|
|
74
|
+
* enableCaching: true,
|
|
75
|
+
* cacheSize: 500
|
|
76
|
+
* }
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
32
79
|
*/
|
|
33
80
|
export interface AllProfanityOptions {
|
|
81
|
+
/**
|
|
82
|
+
* Array of language keys to load (e.g., 'english', 'hindi', 'french').
|
|
83
|
+
* Available languages: english, hindi, french, german, spanish, bengali, tamil, telugu, brazilian.
|
|
84
|
+
*
|
|
85
|
+
* @default ['english', 'hindi'] (loaded by default in constructor)
|
|
86
|
+
*/
|
|
34
87
|
languages?: string[];
|
|
88
|
+
/**
|
|
89
|
+
* Custom dictionaries to load in addition to built-in languages.
|
|
90
|
+
* Key is the dictionary name, value is an array of words.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* customDictionaries: {
|
|
95
|
+
* 'gaming': ['noob', 'trash'],
|
|
96
|
+
* 'custom': ['word1', 'word2']
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
35
100
|
customDictionaries?: Record<string, string[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Single character to use as replacement placeholder for profane characters.
|
|
103
|
+
*
|
|
104
|
+
* @default "*"
|
|
105
|
+
*/
|
|
36
106
|
defaultPlaceholder?: string;
|
|
107
|
+
/**
|
|
108
|
+
* Enable detection and normalization of leet speak variations (e.g., "h3ll0" -> "hello").
|
|
109
|
+
*
|
|
110
|
+
* @default true
|
|
111
|
+
*/
|
|
37
112
|
enableLeetSpeak?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Enable case-sensitive matching. When false, all matching is done in lowercase.
|
|
115
|
+
*
|
|
116
|
+
* @default false
|
|
117
|
+
*/
|
|
38
118
|
caseSensitive?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Array of words to whitelist (never flag as profanity even if in dictionaries).
|
|
121
|
+
*
|
|
122
|
+
* @example ['hello', 'class', 'assignment']
|
|
123
|
+
*/
|
|
39
124
|
whitelistWords?: string[];
|
|
125
|
+
/**
|
|
126
|
+
* Strict mode requires profanity to be surrounded by word boundaries (spaces, punctuation).
|
|
127
|
+
* When false, profanity embedded in other words may be detected.
|
|
128
|
+
*
|
|
129
|
+
* @default false
|
|
130
|
+
*/
|
|
40
131
|
strictMode?: boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Allow detection of profanity as partial matches within larger words.
|
|
134
|
+
* When true, "badword" will be detected in "mybadwordhere".
|
|
135
|
+
*
|
|
136
|
+
* @default false
|
|
137
|
+
*/
|
|
41
138
|
detectPartialWords?: boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Custom logger implementation for handling log messages.
|
|
141
|
+
* If not provided, defaults to ConsoleLogger unless silent mode is enabled.
|
|
142
|
+
*/
|
|
42
143
|
logger?: Logger;
|
|
144
|
+
/**
|
|
145
|
+
* Silent mode suppresses all logging output.
|
|
146
|
+
* When true, uses SilentLogger to discard all log messages.
|
|
147
|
+
*
|
|
148
|
+
* @default false
|
|
149
|
+
*/
|
|
150
|
+
silent?: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Advanced algorithm configuration for pattern matching strategies.
|
|
153
|
+
*/
|
|
43
154
|
algorithm?: {
|
|
155
|
+
/**
|
|
156
|
+
* Primary matching algorithm to use.
|
|
157
|
+
* - 'trie': Fast prefix tree matching (default, best for most use cases)
|
|
158
|
+
* - 'aho-corasick': Multi-pattern matching (best for large dictionaries)
|
|
159
|
+
* - 'hybrid': Combines Aho-Corasick with Bloom Filter (best for extreme performance)
|
|
160
|
+
*
|
|
161
|
+
* @default "trie"
|
|
162
|
+
*/
|
|
44
163
|
matching?: "trie" | "aho-corasick" | "hybrid";
|
|
164
|
+
/**
|
|
165
|
+
* Enable Aho-Corasick automaton for multi-pattern matching.
|
|
166
|
+
* Automatically enabled when matching is set to 'aho-corasick' or 'hybrid'.
|
|
167
|
+
*
|
|
168
|
+
* @default false
|
|
169
|
+
*/
|
|
45
170
|
useAhoCorasick?: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* Enable Bloom Filter for probabilistic quick rejection of non-profane text.
|
|
173
|
+
* Automatically enabled when matching is set to 'hybrid'.
|
|
174
|
+
*
|
|
175
|
+
* @default false
|
|
176
|
+
*/
|
|
46
177
|
useBloomFilter?: boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Enable context analysis to reduce false positives based on surrounding words.
|
|
180
|
+
*
|
|
181
|
+
* @default false
|
|
182
|
+
*/
|
|
47
183
|
useContextAnalysis?: boolean;
|
|
48
184
|
};
|
|
185
|
+
/**
|
|
186
|
+
* Bloom Filter configuration for probabilistic matching optimization.
|
|
187
|
+
*/
|
|
49
188
|
bloomFilter?: {
|
|
189
|
+
/**
|
|
190
|
+
* Enable Bloom Filter.
|
|
191
|
+
*
|
|
192
|
+
* @default false
|
|
193
|
+
*/
|
|
50
194
|
enabled?: boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Expected number of items to be stored in the Bloom Filter.
|
|
197
|
+
* Higher values increase memory usage but reduce false positive rate.
|
|
198
|
+
*
|
|
199
|
+
* @default 10000
|
|
200
|
+
*/
|
|
51
201
|
expectedItems?: number;
|
|
202
|
+
/**
|
|
203
|
+
* Target false positive rate (probability of incorrectly identifying non-profanity as profanity).
|
|
204
|
+
* Lower values increase memory usage but improve accuracy.
|
|
205
|
+
*
|
|
206
|
+
* @default 0.01 (1%)
|
|
207
|
+
*/
|
|
52
208
|
falsePositiveRate?: number;
|
|
53
209
|
};
|
|
210
|
+
/**
|
|
211
|
+
* Aho-Corasick automaton configuration for multi-pattern matching.
|
|
212
|
+
*/
|
|
54
213
|
ahoCorasick?: {
|
|
214
|
+
/**
|
|
215
|
+
* Enable Aho-Corasick automaton.
|
|
216
|
+
*
|
|
217
|
+
* @default false
|
|
218
|
+
*/
|
|
55
219
|
enabled?: boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Pre-build the automaton during initialization.
|
|
222
|
+
* When false, automaton is built lazily on first use.
|
|
223
|
+
*
|
|
224
|
+
* @default false
|
|
225
|
+
*/
|
|
56
226
|
prebuild?: boolean;
|
|
57
227
|
};
|
|
228
|
+
/**
|
|
229
|
+
* Context analysis configuration for reducing false positives.
|
|
230
|
+
*/
|
|
58
231
|
contextAnalysis?: {
|
|
232
|
+
/**
|
|
233
|
+
* Enable context-aware profanity detection.
|
|
234
|
+
*
|
|
235
|
+
* @default false
|
|
236
|
+
*/
|
|
59
237
|
enabled?: boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Number of words before and after the detected word to analyze for context.
|
|
240
|
+
*
|
|
241
|
+
* @default 5
|
|
242
|
+
*/
|
|
60
243
|
contextWindow?: number;
|
|
244
|
+
/**
|
|
245
|
+
* Languages to use for context analysis (e.g., ['en', 'es']).
|
|
246
|
+
*
|
|
247
|
+
* @default ['en']
|
|
248
|
+
*/
|
|
61
249
|
languages?: string[];
|
|
250
|
+
/**
|
|
251
|
+
* Minimum confidence score (0-1) required to flag as profanity.
|
|
252
|
+
* Higher values reduce false positives but may miss some profanity.
|
|
253
|
+
*
|
|
254
|
+
* @default 0.5
|
|
255
|
+
*/
|
|
62
256
|
scoreThreshold?: number;
|
|
63
257
|
};
|
|
258
|
+
/**
|
|
259
|
+
* Performance optimization configuration.
|
|
260
|
+
*/
|
|
64
261
|
performance?: {
|
|
262
|
+
/**
|
|
263
|
+
* Maximum number of results to cache in LRU cache.
|
|
264
|
+
*
|
|
265
|
+
* @default 1000
|
|
266
|
+
*/
|
|
65
267
|
cacheSize?: number;
|
|
268
|
+
/**
|
|
269
|
+
* Enable result caching to speed up repeated queries.
|
|
270
|
+
* Stores detection results for previously seen text.
|
|
271
|
+
*
|
|
272
|
+
* @default false
|
|
273
|
+
*/
|
|
66
274
|
enableCaching?: boolean;
|
|
67
275
|
};
|
|
68
276
|
}
|
|
69
277
|
/**
|
|
70
|
-
* Severity levels for profanity detection.
|
|
278
|
+
* Severity levels for profanity detection results.
|
|
279
|
+
*
|
|
280
|
+
* @enum {number}
|
|
281
|
+
* @description Categorizes the severity of detected profanity based on the number
|
|
282
|
+
* of unique words and total matches found in the text.
|
|
283
|
+
*
|
|
284
|
+
* @readonly
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* const result = filter.detect("some text");
|
|
288
|
+
* if (result.severity === ProfanitySeverity.EXTREME) {
|
|
289
|
+
* // Handle extreme profanity
|
|
290
|
+
* }
|
|
291
|
+
* ```
|
|
71
292
|
*/
|
|
72
293
|
export declare enum ProfanitySeverity {
|
|
294
|
+
/** Mild profanity: 1 unique word or 1 total match */
|
|
73
295
|
MILD = 1,
|
|
296
|
+
/** Moderate profanity: 2 unique words or 2 total matches */
|
|
74
297
|
MODERATE = 2,
|
|
298
|
+
/** Severe profanity: 3 unique words or 3 total matches */
|
|
75
299
|
SEVERE = 3,
|
|
300
|
+
/** Extreme profanity: 4+ unique words or 5+ total matches */
|
|
76
301
|
EXTREME = 4
|
|
77
302
|
}
|
|
78
303
|
/**
|
|
79
|
-
*
|
|
304
|
+
* Result object returned from profanity detection operations.
|
|
305
|
+
*
|
|
306
|
+
* @interface ProfanityDetectionResult
|
|
307
|
+
* @description Contains comprehensive information about detected profanity including
|
|
308
|
+
* what was found, where it was found, how severe it is, and a cleaned version of the text.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* const result = filter.detect("This is a bad word");
|
|
313
|
+
* console.log(result.hasProfanity); // true
|
|
314
|
+
* console.log(result.detectedWords); // ['bad word']
|
|
315
|
+
* console.log(result.cleanedText); // 'This is a *** ****'
|
|
316
|
+
* console.log(result.severity); // ProfanitySeverity.MILD
|
|
317
|
+
* console.log(result.positions); // [{ word: 'bad word', start: 10, end: 18 }]
|
|
318
|
+
* ```
|
|
80
319
|
*/
|
|
81
320
|
export interface ProfanityDetectionResult {
|
|
321
|
+
/**
|
|
322
|
+
* Whether any profanity was detected in the text.
|
|
323
|
+
*
|
|
324
|
+
* @type {boolean}
|
|
325
|
+
*/
|
|
82
326
|
hasProfanity: boolean;
|
|
327
|
+
/**
|
|
328
|
+
* Array of detected profane words/phrases as they appeared in the original text.
|
|
329
|
+
* Includes case and formatting from the original text.
|
|
330
|
+
*
|
|
331
|
+
* @type {string[]}
|
|
332
|
+
*/
|
|
83
333
|
detectedWords: string[];
|
|
334
|
+
/**
|
|
335
|
+
* The text with all profanity replaced by placeholder characters.
|
|
336
|
+
* Each profane character is replaced with the configured placeholder (default: '*').
|
|
337
|
+
*
|
|
338
|
+
* @type {string}
|
|
339
|
+
*/
|
|
84
340
|
cleanedText: string;
|
|
341
|
+
/**
|
|
342
|
+
* Severity level of detected profanity.
|
|
343
|
+
*
|
|
344
|
+
* @type {ProfanitySeverity}
|
|
345
|
+
*/
|
|
85
346
|
severity: ProfanitySeverity;
|
|
347
|
+
/**
|
|
348
|
+
* Precise positions of each detected profane word in the original text.
|
|
349
|
+
* Useful for highlighting or further processing.
|
|
350
|
+
*
|
|
351
|
+
* @type {Array<{ word: string; start: number; end: number }>}
|
|
352
|
+
*/
|
|
86
353
|
positions: Array<{
|
|
87
354
|
word: string;
|
|
88
355
|
start: number;
|
|
@@ -90,7 +357,99 @@ export interface ProfanityDetectionResult {
|
|
|
90
357
|
}>;
|
|
91
358
|
}
|
|
92
359
|
/**
|
|
93
|
-
*
|
|
360
|
+
* AllProfanity - Professional-grade multilingual profanity detection and filtering library.
|
|
361
|
+
*
|
|
362
|
+
* @class AllProfanity
|
|
363
|
+
* @description A comprehensive, high-performance profanity filtering system supporting 9+ languages
|
|
364
|
+
* with advanced features including leet speak detection, context analysis, multiple matching algorithms,
|
|
365
|
+
* and customizable filtering options.
|
|
366
|
+
*
|
|
367
|
+
* @remarks
|
|
368
|
+
* ### Features:
|
|
369
|
+
* - **Multi-language Support**: English, Hindi, French, German, Spanish, Bengali, Tamil, Telugu, Brazilian Portuguese
|
|
370
|
+
* - **Advanced Algorithms**: Trie, Aho-Corasick, Bloom Filter, and hybrid approaches
|
|
371
|
+
* - **Leet Speak Detection**: Automatically normalizes and detects variations like "h3ll0"
|
|
372
|
+
* - **Context Analysis**: Reduces false positives using surrounding word context
|
|
373
|
+
* - **Performance**: Built-in caching and optimized data structures
|
|
374
|
+
* - **Flexible**: Custom dictionaries, whitelisting, severity levels
|
|
375
|
+
*
|
|
376
|
+
* ### Default Behavior:
|
|
377
|
+
* - Loads English and Hindi dictionaries by default
|
|
378
|
+
* - Case-insensitive matching
|
|
379
|
+
* - Leet speak detection enabled
|
|
380
|
+
* - Uses Trie algorithm (fastest for most cases)
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```typescript
|
|
384
|
+
* // Basic usage with default instance
|
|
385
|
+
* import allProfanity from 'allprofanity';
|
|
386
|
+
*
|
|
387
|
+
* const result = allProfanity.detect("This is some bad text");
|
|
388
|
+
* console.log(result.hasProfanity); // true
|
|
389
|
+
* console.log(result.cleanedText); // "This is some *** text"
|
|
390
|
+
* console.log(result.severity); // ProfanitySeverity.MILD
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```typescript
|
|
395
|
+
* // Advanced usage with custom configuration
|
|
396
|
+
* import { AllProfanity, ProfanitySeverity } from 'allprofanity';
|
|
397
|
+
*
|
|
398
|
+
* const filter = new AllProfanity({
|
|
399
|
+
* languages: ['english', 'french', 'spanish'],
|
|
400
|
+
* enableLeetSpeak: true,
|
|
401
|
+
* strictMode: true,
|
|
402
|
+
* algorithm: {
|
|
403
|
+
* matching: 'hybrid',
|
|
404
|
+
* useBloomFilter: true
|
|
405
|
+
* },
|
|
406
|
+
* performance: {
|
|
407
|
+
* enableCaching: true,
|
|
408
|
+
* cacheSize: 500
|
|
409
|
+
* },
|
|
410
|
+
* whitelistWords: ['class', 'assignment']
|
|
411
|
+
* });
|
|
412
|
+
*
|
|
413
|
+
* const text = "This text has some b@d w0rds";
|
|
414
|
+
* const result = filter.detect(text);
|
|
415
|
+
*
|
|
416
|
+
* if (result.hasProfanity) {
|
|
417
|
+
* console.log(`Found ${result.detectedWords.length} profane words`);
|
|
418
|
+
* console.log(`Severity: ${ProfanitySeverity[result.severity]}`);
|
|
419
|
+
* console.log(`Cleaned: ${result.cleanedText}`);
|
|
420
|
+
* }
|
|
421
|
+
* ```
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```typescript
|
|
425
|
+
* // Using individual methods
|
|
426
|
+
* const filter = new AllProfanity();
|
|
427
|
+
*
|
|
428
|
+
* // Simple check
|
|
429
|
+
* if (filter.check("some text")) {
|
|
430
|
+
* console.log("Contains profanity!");
|
|
431
|
+
* }
|
|
432
|
+
*
|
|
433
|
+
* // Clean with custom placeholder
|
|
434
|
+
* const cleaned = filter.clean("bad words here", "#");
|
|
435
|
+
*
|
|
436
|
+
* // Load additional languages
|
|
437
|
+
* filter.loadLanguage('german');
|
|
438
|
+
* filter.loadIndianLanguages(); // Loads hindi, bengali, tamil, telugu
|
|
439
|
+
*
|
|
440
|
+
* // Add custom words
|
|
441
|
+
* filter.add(['customword1', 'customword2']);
|
|
442
|
+
*
|
|
443
|
+
* // Remove words
|
|
444
|
+
* filter.remove(['someword']);
|
|
445
|
+
*
|
|
446
|
+
* // Whitelist words
|
|
447
|
+
* filter.addToWhitelist(['class', 'assignment']);
|
|
448
|
+
* ```
|
|
449
|
+
*
|
|
450
|
+
* @see {@link AllProfanityOptions} for all configuration options
|
|
451
|
+
* @see {@link ProfanityDetectionResult} for detection result format
|
|
452
|
+
* @see {@link ProfanitySeverity} for severity levels
|
|
94
453
|
*/
|
|
95
454
|
export declare class AllProfanity {
|
|
96
455
|
private readonly profanityTrie;
|
|
@@ -111,8 +470,43 @@ export declare class AllProfanity {
|
|
|
111
470
|
private matchingAlgorithm;
|
|
112
471
|
private resultCache;
|
|
113
472
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
473
|
+
* Creates a new AllProfanity instance with the specified configuration.
|
|
474
|
+
*
|
|
475
|
+
* @constructor
|
|
476
|
+
* @param {AllProfanityOptions} [options] - Configuration options for profanity detection behavior
|
|
477
|
+
*
|
|
478
|
+
* @remarks
|
|
479
|
+
* ### Default Initialization:
|
|
480
|
+
* - Loads English and Hindi dictionaries automatically
|
|
481
|
+
* - Enables leet speak detection
|
|
482
|
+
* - Case-insensitive matching
|
|
483
|
+
* - Uses Trie algorithm for pattern matching
|
|
484
|
+
*
|
|
485
|
+
* ### Performance Considerations:
|
|
486
|
+
* - Initial load time depends on number of languages loaded
|
|
487
|
+
* - Aho-Corasick automaton (if enabled) is built during construction
|
|
488
|
+
* - Bloom Filter (if enabled) is populated during construction
|
|
489
|
+
*
|
|
490
|
+
* @throws {TypeError} If invalid options are provided
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```typescript
|
|
494
|
+
* // Default instance
|
|
495
|
+
* const filter = new AllProfanity();
|
|
496
|
+
*
|
|
497
|
+
* // Custom configuration
|
|
498
|
+
* const filter = new AllProfanity({
|
|
499
|
+
* languages: ['english', 'french'],
|
|
500
|
+
* strictMode: true,
|
|
501
|
+
* defaultPlaceholder: '#',
|
|
502
|
+
* algorithm: { matching: 'hybrid' }
|
|
503
|
+
* });
|
|
504
|
+
*
|
|
505
|
+
* // Silent mode (no logging)
|
|
506
|
+
* const filter = new AllProfanity({ silent: true });
|
|
507
|
+
* ```
|
|
508
|
+
*
|
|
509
|
+
* @see {@link AllProfanityOptions} for all available configuration options
|
|
116
510
|
*/
|
|
117
511
|
constructor(options?: AllProfanityOptions);
|
|
118
512
|
/**
|
|
@@ -173,9 +567,56 @@ export declare class AllProfanity {
|
|
|
173
567
|
*/
|
|
174
568
|
private applyContextAnalysis;
|
|
175
569
|
/**
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* @
|
|
570
|
+
* Detects profanity in the provided text and returns comprehensive analysis.
|
|
571
|
+
*
|
|
572
|
+
* @param {string} text - The text to analyze for profanity
|
|
573
|
+
* @returns {ProfanityDetectionResult} Detailed detection result including matches, positions, severity, and cleaned text
|
|
574
|
+
*
|
|
575
|
+
* @throws {TypeError} If text is not a string
|
|
576
|
+
*
|
|
577
|
+
* @remarks
|
|
578
|
+
* ### Performance:
|
|
579
|
+
* - Time Complexity: O(n*m) where n is text length, m is average word length in dictionary
|
|
580
|
+
* - With Bloom Filter: O(n) average case (faster early rejection)
|
|
581
|
+
* - With Caching: O(1) for repeated identical text
|
|
582
|
+
*
|
|
583
|
+
* ### Features:
|
|
584
|
+
* - Detects leet speak variations (if enabled): "h3ll0" → "hello"
|
|
585
|
+
* - Respects word boundaries (strict mode) or detects partial matches
|
|
586
|
+
* - Returns exact positions for highlighting/masking
|
|
587
|
+
* - Calculates severity based on match count and uniqueness
|
|
588
|
+
*
|
|
589
|
+
* ### Caching:
|
|
590
|
+
* - Results are cached if `performance.enableCaching` is true
|
|
591
|
+
* - Cache uses LRU eviction when size limit is reached
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* ```typescript
|
|
595
|
+
* const filter = new AllProfanity();
|
|
596
|
+
* const result = filter.detect("This has bad words");
|
|
597
|
+
*
|
|
598
|
+
* console.log(result.hasProfanity); // true
|
|
599
|
+
* console.log(result.detectedWords); // ['bad']
|
|
600
|
+
* console.log(result.cleanedText); // 'This has *** words'
|
|
601
|
+
* console.log(result.severity); // ProfanitySeverity.MILD
|
|
602
|
+
* console.log(result.positions); // [{ word: 'bad', start: 9, end: 12 }]
|
|
603
|
+
* ```
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* // With leet speak detection
|
|
608
|
+
* const filter = new AllProfanity({ enableLeetSpeak: true });
|
|
609
|
+
* const result = filter.detect("st0p b3ing b@d");
|
|
610
|
+
*
|
|
611
|
+
* if (result.hasProfanity) {
|
|
612
|
+
* result.positions.forEach(pos => {
|
|
613
|
+
* console.log(`Found "${pos.word}" at position ${pos.start}-${pos.end}`);
|
|
614
|
+
* });
|
|
615
|
+
* }
|
|
616
|
+
* ```
|
|
617
|
+
*
|
|
618
|
+
* @see {@link ProfanityDetectionResult} for result structure
|
|
619
|
+
* @see {@link ProfanitySeverity} for severity levels
|
|
179
620
|
*/
|
|
180
621
|
detect(text: string): ProfanityDetectionResult;
|
|
181
622
|
/**
|
|
@@ -193,33 +634,214 @@ export declare class AllProfanity {
|
|
|
193
634
|
*/
|
|
194
635
|
private generateCleanedText;
|
|
195
636
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* @
|
|
637
|
+
* Quick boolean check for profanity presence in text.
|
|
638
|
+
*
|
|
639
|
+
* @param {string} text - The text to check for profanity
|
|
640
|
+
* @returns {boolean} True if profanity is detected, false otherwise
|
|
641
|
+
*
|
|
642
|
+
* @throws {TypeError} If text is not a string
|
|
643
|
+
*
|
|
644
|
+
* @remarks
|
|
645
|
+
* - Convenience method that internally calls `detect()` and returns only the boolean result
|
|
646
|
+
* - For detailed information about matches, use `detect()` instead
|
|
647
|
+
* - Results are cached if caching is enabled (same cache as `detect()`)
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* ```typescript
|
|
651
|
+
* const filter = new AllProfanity();
|
|
652
|
+
*
|
|
653
|
+
* if (filter.check("This has bad words")) {
|
|
654
|
+
* console.log("Profanity detected!");
|
|
655
|
+
* }
|
|
656
|
+
*
|
|
657
|
+
* // Quick validation
|
|
658
|
+
* const isClean = !filter.check(userInput);
|
|
659
|
+
* ```
|
|
660
|
+
*
|
|
661
|
+
* @see {@link detect} for detailed profanity analysis
|
|
199
662
|
*/
|
|
200
663
|
check(text: string): boolean;
|
|
201
664
|
/**
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
* @param
|
|
205
|
-
* @
|
|
665
|
+
* Cleans text by replacing profanity with a placeholder character.
|
|
666
|
+
*
|
|
667
|
+
* @param {string} text - The text to clean
|
|
668
|
+
* @param {string} [placeholder] - Optional custom placeholder character (uses default if not provided)
|
|
669
|
+
* @returns {string} The cleaned text with profanity replaced
|
|
670
|
+
*
|
|
671
|
+
* @throws {TypeError} If text is not a string
|
|
672
|
+
*
|
|
673
|
+
* @remarks
|
|
674
|
+
* ### Character-level Replacement:
|
|
675
|
+
* - Each profane character is replaced individually
|
|
676
|
+
* - "bad" with placeholder "*" becomes "***"
|
|
677
|
+
* - Preserves text length and structure
|
|
678
|
+
*
|
|
679
|
+
* ### Placeholder Behavior:
|
|
680
|
+
* - If no placeholder provided, uses the instance's default placeholder
|
|
681
|
+
* - If placeholder provided, uses only the first character
|
|
682
|
+
* - Empty placeholder throws error
|
|
683
|
+
*
|
|
684
|
+
* @example
|
|
685
|
+
* ```typescript
|
|
686
|
+
* const filter = new AllProfanity();
|
|
687
|
+
*
|
|
688
|
+
* // Using default placeholder (*)
|
|
689
|
+
* const cleaned = filter.clean("This has bad words");
|
|
690
|
+
* console.log(cleaned); // "This has *** *****"
|
|
691
|
+
*
|
|
692
|
+
* // Using custom placeholder
|
|
693
|
+
* const cleaned = filter.clean("This has bad words", "#");
|
|
694
|
+
* console.log(cleaned); // "This has ### #####"
|
|
695
|
+
* ```
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```typescript
|
|
699
|
+
* // Clean user-generated content for display
|
|
700
|
+
* const userComment = "Some inappropriate words here";
|
|
701
|
+
* const safeComment = filter.clean(userComment);
|
|
702
|
+
* displayComment(safeComment);
|
|
703
|
+
* ```
|
|
704
|
+
*
|
|
705
|
+
* @see {@link cleanWithPlaceholder} for word-level replacement
|
|
706
|
+
* @see {@link setPlaceholder} to change default placeholder
|
|
206
707
|
*/
|
|
207
708
|
clean(text: string, placeholder?: string): string;
|
|
208
709
|
/**
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* @param
|
|
212
|
-
* @
|
|
710
|
+
* Cleans text by replacing each profane word with a single placeholder string (word-level replacement).
|
|
711
|
+
*
|
|
712
|
+
* @param {string} text - The text to clean
|
|
713
|
+
* @param {string} [placeholder="***"] - The placeholder string to use for each profane word
|
|
714
|
+
* @returns {string} The cleaned text with each profane word replaced by the placeholder
|
|
715
|
+
*
|
|
716
|
+
* @throws {TypeError} If text is not a string
|
|
717
|
+
*
|
|
718
|
+
* @remarks
|
|
719
|
+
* ### Word-level Replacement:
|
|
720
|
+
* - Each profane word is replaced with the entire placeholder string (not character-by-character)
|
|
721
|
+
* - "bad words" with placeholder "***" becomes "*** ***"
|
|
722
|
+
* - Does NOT preserve original text length
|
|
723
|
+
*
|
|
724
|
+
* ### Difference from `clean()`:
|
|
725
|
+
* - `clean()`: Character-level replacement - "bad" becomes "***" (preserves length)
|
|
726
|
+
* - `cleanWithPlaceholder()`: Word-level replacement - "bad" becomes "***" (fixed placeholder)
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```typescript
|
|
730
|
+
* const filter = new AllProfanity();
|
|
731
|
+
*
|
|
732
|
+
* // Default placeholder (***) const text = "This has bad words";
|
|
733
|
+
* const cleaned = filter.cleanWithPlaceholder(text);
|
|
734
|
+
* console.log(cleaned); // "This has *** ***"
|
|
735
|
+
*
|
|
736
|
+
* // Custom placeholder
|
|
737
|
+
* const cleaned2 = filter.cleanWithPlaceholder(text, "[CENSORED]");
|
|
738
|
+
* console.log(cleaned2); // "This has [CENSORED] [CENSORED]"
|
|
739
|
+
* ```
|
|
740
|
+
*
|
|
741
|
+
* @example
|
|
742
|
+
* ```typescript
|
|
743
|
+
* // Censoring chat messages
|
|
744
|
+
* const message = "You are a badword and stupid";
|
|
745
|
+
* const censored = filter.cleanWithPlaceholder(message, "[***]");
|
|
746
|
+
* // Result: "You are a [***] and [***]"
|
|
747
|
+
* ```
|
|
748
|
+
*
|
|
749
|
+
* @see {@link clean} for character-level replacement
|
|
213
750
|
*/
|
|
214
751
|
cleanWithPlaceholder(text: string, placeholder?: string): string;
|
|
215
752
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
753
|
+
* Dynamically adds one or more words to the profanity filter at runtime.
|
|
754
|
+
*
|
|
755
|
+
* @param {string | string[]} word - A single word or array of words to add to the filter
|
|
756
|
+
* @returns {void}
|
|
757
|
+
*
|
|
758
|
+
* @remarks
|
|
759
|
+
* ### Behavior:
|
|
760
|
+
* - Words are added to all active data structures (Trie, Aho-Corasick, Bloom Filter)
|
|
761
|
+
* - Automatically normalizes words based on caseSensitive setting
|
|
762
|
+
* - Skips whitelisted words
|
|
763
|
+
* - Validates and filters out non-string or empty values
|
|
764
|
+
* - Changes take effect immediately for subsequent detect/check/clean calls
|
|
765
|
+
*
|
|
766
|
+
* ### Use Cases:
|
|
767
|
+
* - Adding context-specific profanity
|
|
768
|
+
* - Building dynamic word lists from user reports
|
|
769
|
+
* - Customizing filters for specific communities/applications
|
|
770
|
+
*
|
|
771
|
+
* @example
|
|
772
|
+
* ```typescript
|
|
773
|
+
* const filter = new AllProfanity();
|
|
774
|
+
*
|
|
775
|
+
* // Add single word
|
|
776
|
+
* filter.add('newbadword');
|
|
777
|
+
*
|
|
778
|
+
* // Add multiple words
|
|
779
|
+
* filter.add(['word1', 'word2', 'word3']);
|
|
780
|
+
*
|
|
781
|
+
* // Now these words will be detected
|
|
782
|
+
* filter.check('newbadword'); // true
|
|
783
|
+
* ```
|
|
784
|
+
*
|
|
785
|
+
* @example
|
|
786
|
+
* ```typescript
|
|
787
|
+
* // Add game-specific slang dynamically
|
|
788
|
+
* const filter = new AllProfanity();
|
|
789
|
+
* const gamingSlang = ['noob', 'trash', 'tryhard'];
|
|
790
|
+
* filter.add(gamingSlang);
|
|
791
|
+
*
|
|
792
|
+
* const message = "You're such a noob";
|
|
793
|
+
* console.log(filter.check(message)); // true
|
|
794
|
+
* ```
|
|
795
|
+
*
|
|
796
|
+
* @see {@link remove} to remove words
|
|
797
|
+
* @see {@link loadCustomDictionary} for loading named dictionaries
|
|
218
798
|
*/
|
|
219
799
|
add(word: string | string[]): void;
|
|
220
800
|
/**
|
|
221
|
-
*
|
|
222
|
-
*
|
|
801
|
+
* Dynamically removes one or more words from the profanity filter at runtime.
|
|
802
|
+
*
|
|
803
|
+
* @param {string | string[]} word - A single word or array of words to remove from the filter
|
|
804
|
+
* @returns {void}
|
|
805
|
+
*
|
|
806
|
+
* @remarks
|
|
807
|
+
* ### Behavior:
|
|
808
|
+
* - Removes words from all active data structures (Trie, dynamic words set)
|
|
809
|
+
* - Normalizes words based on caseSensitive setting before removal
|
|
810
|
+
* - Only removes dynamically added words, not words from loaded language dictionaries
|
|
811
|
+
* - Changes take effect immediately for subsequent detect/check/clean calls
|
|
812
|
+
*
|
|
813
|
+
* ### Important Notes:
|
|
814
|
+
* - Cannot remove words from built-in language dictionaries
|
|
815
|
+
* - To exclude dictionary words, use `addToWhitelist()` instead
|
|
816
|
+
* - Validates and filters out non-string or empty values
|
|
817
|
+
*
|
|
818
|
+
* @example
|
|
819
|
+
* ```typescript
|
|
820
|
+
* const filter = new AllProfanity();
|
|
821
|
+
*
|
|
822
|
+
* // Add then remove a word
|
|
823
|
+
* filter.add('tempword');
|
|
824
|
+
* filter.check('tempword'); // true
|
|
825
|
+
*
|
|
826
|
+
* filter.remove('tempword');
|
|
827
|
+
* filter.check('tempword'); // false
|
|
828
|
+
*
|
|
829
|
+
* // Remove multiple words
|
|
830
|
+
* filter.remove(['word1', 'word2']);
|
|
831
|
+
* ```
|
|
832
|
+
*
|
|
833
|
+
* @example
|
|
834
|
+
* ```typescript
|
|
835
|
+
* // Managing custom word list
|
|
836
|
+
* const filter = new AllProfanity();
|
|
837
|
+
* filter.add(['custom1', 'custom2', 'custom3']);
|
|
838
|
+
*
|
|
839
|
+
* // Later, remove one that's no longer needed
|
|
840
|
+
* filter.remove('custom2');
|
|
841
|
+
* ```
|
|
842
|
+
*
|
|
843
|
+
* @see {@link add} to add words
|
|
844
|
+
* @see {@link addToWhitelist} to exclude dictionary words without removing them
|
|
223
845
|
*/
|
|
224
846
|
remove(word: string | string[]): void;
|
|
225
847
|
/**
|
|
@@ -239,9 +861,60 @@ export declare class AllProfanity {
|
|
|
239
861
|
*/
|
|
240
862
|
private isWhitelisted;
|
|
241
863
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* @
|
|
864
|
+
* Loads a built-in language dictionary into the profanity filter.
|
|
865
|
+
*
|
|
866
|
+
* @param {string} language - The language key to load (case-insensitive)
|
|
867
|
+
* @returns {boolean} True if language was loaded successfully, false if not found or already loaded
|
|
868
|
+
*
|
|
869
|
+
* @remarks
|
|
870
|
+
* ### Available Languages:
|
|
871
|
+
* - `'english'` - English profanity words
|
|
872
|
+
* - `'hindi'` - Hindi profanity words
|
|
873
|
+
* - `'french'` - French profanity words
|
|
874
|
+
* - `'german'` - German profanity words
|
|
875
|
+
* - `'spanish'` - Spanish profanity words
|
|
876
|
+
* - `'bengali'` - Bengali profanity words
|
|
877
|
+
* - `'tamil'` - Tamil profanity words
|
|
878
|
+
* - `'telugu'` - Telugu profanity words
|
|
879
|
+
* - `'brazilian'` - Brazilian Portuguese profanity words
|
|
880
|
+
*
|
|
881
|
+
* ### Behavior:
|
|
882
|
+
* - Language keys are case-insensitive
|
|
883
|
+
* - Loading is idempotent - calling multiple times for same language is safe
|
|
884
|
+
* - Returns true if language loaded successfully or was already loaded
|
|
885
|
+
* - Returns false if language not found
|
|
886
|
+
* - Logs success/failure messages (unless silent mode enabled)
|
|
887
|
+
* - Words are added to all active data structures
|
|
888
|
+
*
|
|
889
|
+
* ### Default Languages:
|
|
890
|
+
* English and Hindi are loaded automatically in the constructor
|
|
891
|
+
*
|
|
892
|
+
* @example
|
|
893
|
+
* ```typescript
|
|
894
|
+
* const filter = new AllProfanity();
|
|
895
|
+
*
|
|
896
|
+
* // Load additional languages
|
|
897
|
+
* filter.loadLanguage('french');
|
|
898
|
+
* filter.loadLanguage('spanish');
|
|
899
|
+
*
|
|
900
|
+
* // Case-insensitive
|
|
901
|
+
* filter.loadLanguage('GERMAN'); // Works
|
|
902
|
+
*
|
|
903
|
+
* // Check if loaded
|
|
904
|
+
* console.log(filter.getLoadedLanguages()); // ['english', 'hindi', 'french', 'spanish', 'german']
|
|
905
|
+
* ```
|
|
906
|
+
*
|
|
907
|
+
* @example
|
|
908
|
+
* ```typescript
|
|
909
|
+
* // Load all Indian languages at once
|
|
910
|
+
* const filter = new AllProfanity();
|
|
911
|
+
* filter.loadIndianLanguages();
|
|
912
|
+
* ```
|
|
913
|
+
*
|
|
914
|
+
* @see {@link loadLanguages} to load multiple languages at once
|
|
915
|
+
* @see {@link loadIndianLanguages} for convenience method
|
|
916
|
+
* @see {@link getAvailableLanguages} to see all available languages
|
|
917
|
+
* @see {@link getLoadedLanguages} to see currently loaded languages
|
|
245
918
|
*/
|
|
246
919
|
loadLanguage(language: string): boolean;
|
|
247
920
|
/**
|
|
@@ -256,9 +929,64 @@ export declare class AllProfanity {
|
|
|
256
929
|
*/
|
|
257
930
|
loadIndianLanguages(): number;
|
|
258
931
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
* @param
|
|
932
|
+
* Loads a custom dictionary of profane words with a specific name.
|
|
933
|
+
*
|
|
934
|
+
* @param {string} name - Unique name/identifier for this custom dictionary
|
|
935
|
+
* @param {string[]} words - Array of profane words to add to the dictionary
|
|
936
|
+
* @returns {void}
|
|
937
|
+
*
|
|
938
|
+
* @throws {TypeError} If name is not a string or words is not an array
|
|
939
|
+
*
|
|
940
|
+
* @remarks
|
|
941
|
+
* ### Behavior:
|
|
942
|
+
* - Creates a new named dictionary or overwrites existing one with same name
|
|
943
|
+
* - Validates and filters out non-string and empty values from words array
|
|
944
|
+
* - Words are added to all active data structures (Trie, Aho-Corasick, Bloom Filter)
|
|
945
|
+
* - Dictionary name is converted to lowercase for storage
|
|
946
|
+
* - Logs count of loaded words (unless silent mode enabled)
|
|
947
|
+
*
|
|
948
|
+
* ### Use Cases:
|
|
949
|
+
* - Domain-specific profanity (gaming, medical, legal, etc.)
|
|
950
|
+
* - Organization-specific word lists
|
|
951
|
+
* - Temporary or context-dependent filters
|
|
952
|
+
* - Testing and development
|
|
953
|
+
*
|
|
954
|
+
* @example
|
|
955
|
+
* ```typescript
|
|
956
|
+
* const filter = new AllProfanity();
|
|
957
|
+
*
|
|
958
|
+
* // Load gaming-specific slang
|
|
959
|
+
* filter.loadCustomDictionary('gaming', [
|
|
960
|
+
* 'noob',
|
|
961
|
+
* 'scrub',
|
|
962
|
+
* 'tryhard',
|
|
963
|
+
* 'trash'
|
|
964
|
+
* ]);
|
|
965
|
+
*
|
|
966
|
+
* // Load company-specific terms
|
|
967
|
+
* filter.loadCustomDictionary('company', [
|
|
968
|
+
* 'competitor1',
|
|
969
|
+
* 'bannedTerm1',
|
|
970
|
+
* 'inappropriateJargon'
|
|
971
|
+
* ]);
|
|
972
|
+
*
|
|
973
|
+
* console.log(filter.check('You are such a noob')); // true
|
|
974
|
+
* ```
|
|
975
|
+
*
|
|
976
|
+
* @example
|
|
977
|
+
* ```typescript
|
|
978
|
+
* // Load from external source
|
|
979
|
+
* const filter = new AllProfanity();
|
|
980
|
+
*
|
|
981
|
+
* async function loadExternalDictionary() {
|
|
982
|
+
* const response = await fetch('https://example.com/custom-words.json');
|
|
983
|
+
* const customWords = await response.json();
|
|
984
|
+
* filter.loadCustomDictionary('external', customWords);
|
|
985
|
+
* }
|
|
986
|
+
* ```
|
|
987
|
+
*
|
|
988
|
+
* @see {@link add} for adding individual words dynamically
|
|
989
|
+
* @see {@link loadLanguage} for loading built-in language dictionaries
|
|
262
990
|
*/
|
|
263
991
|
loadCustomDictionary(name: string, words: string[]): void;
|
|
264
992
|
/**
|