allprofanity
Version:
A TypeScript package to filter Hindi and Hinglish bad words from text
268 lines (267 loc) • 8.59 kB
TypeScript
export { default as englishBadWords } from "./languages/english-words.js";
export { default as hindiBadWords } from "./languages/hindi-words.js";
export { default as frenchBadWords } from "./languages/french-words.js";
export { default as germanBadWords } from "./languages/german-words.js";
export { default as spanishBadWords } from "./languages/spanish-words.js";
export { default as bengaliBadWords } from "./languages/bengali-words.js";
export { default as tamilBadWords } from "./languages/tamil-words.js";
export { default as teluguBadWords } from "./languages/telugu-words.js";
/**
* Logger interface for the library.
*/
export interface Logger {
/**
* Log informational messages.
* @param message - The message to log.
*/
info(message: string): void;
/**
* Log warning messages.
* @param message - The message to log.
*/
warn(message: string): void;
/**
* Log error messages.
* @param message - The message to log.
*/
error(message: string): void;
}
/**
* Configuration options for AllProfanity.
*/
export interface AllProfanityOptions {
languages?: string[];
customDictionaries?: Record<string, string[]>;
defaultPlaceholder?: string;
enableLeetSpeak?: boolean;
caseSensitive?: boolean;
whitelistWords?: string[];
strictMode?: boolean;
detectPartialWords?: boolean;
logger?: Logger;
}
/**
* Severity levels for profanity detection.
*/
export declare enum ProfanitySeverity {
MILD = 1,
MODERATE = 2,
SEVERE = 3,
EXTREME = 4
}
/**
* Detection result for profanity detection.
*/
export interface ProfanityDetectionResult {
hasProfanity: boolean;
detectedWords: string[];
cleanedText: string;
severity: ProfanitySeverity;
positions: Array<{
word: string;
start: number;
end: number;
}>;
}
/**
* Main class for profanity detection and filtering.
*/
export declare class AllProfanity {
private readonly profanityTrie;
private readonly whitelistSet;
private readonly loadedLanguages;
private readonly logger;
private defaultPlaceholder;
private enableLeetSpeak;
private caseSensitive;
private strictMode;
private detectPartialWords;
private readonly availableLanguages;
private readonly leetMappings;
private readonly dynamicWords;
/**
* Create an AllProfanity instance.
* @param options - Profanity filter configuration options.
*/
constructor(options?: AllProfanityOptions);
/**
* Normalize leet speak to regular characters.
* @param text - The input text.
* @returns Normalized text.
*/
private normalizeLeetSpeak;
/**
* Escape regex special characters in a string.
* @param str - The string to escape.
* @returns The escaped string.
*/
private escapeRegex;
/**
* Check if a match is bounded by word boundaries (strict mode).
* @param text - The text.
* @param start - Start index.
* @param end - End index.
* @returns True if match is at word boundaries, false otherwise.
*/
private hasWordBoundaries;
/**
* Determine if a match is a whole word.
* @param text - The text.
* @param start - Start index.
* @param end - End index.
* @returns True if whole word, false otherwise.
*/
private isWholeWord;
/**
* Check if a match is whitelisted.
* @param word - Word from dictionary.
* @param matchedText - Actual matched text.
* @returns True if whitelisted, false otherwise.
*/
private isWhitelistedMatch;
/**
* Remove overlapping matches, keeping only the longest at each start position.
* @param matches - Array of match results.
* @returns Deduplicated matches.
*/
private deduplicateMatches;
/**
* Detect profanity in a given text.
* @param text - The text to check.
* @returns Profanity detection result.
*/
detect(text: string): ProfanityDetectionResult;
/**
* Main matching function, with whole-word logic.
* @param searchText - The normalized text to search.
* @param originalText - The original text.
* @param matches - Array to collect matches.
*/
private findMatches;
/**
* Generate cleaned text by replacing profane words.
* @param originalText - The original text.
* @param matches - Array of matches.
* @returns Cleaned text.
*/
private generateCleanedText;
/**
* Check if a string contains profanity.
* @param text - The text to check.
* @returns True if profanity is found, false otherwise.
*/
check(text: string): boolean;
/**
* Clean text with a custom placeholder.
* @param text - The text to clean.
* @param placeholder - The placeholder to use.
* @returns Cleaned text.
*/
clean(text: string, placeholder?: string): string;
/**
* Clean text by replacing each profane word with a single placeholder (word-level).
* @param text - The text to clean.
* @param placeholder - The placeholder to use.
* @returns Word-level cleaned text.
*/
cleanWithPlaceholder(text: string, placeholder?: string): string;
/**
* Add word(s) to the profanity filter.
* @param word - Word or array of words to add.
*/
add(word: string | string[]): void;
/**
* Remove word(s) from the profanity filter.
* @param word - Word or array of words to remove.
*/
remove(word: string | string[]): void;
/**
* Add words to the whitelist.
* @param words - Words to whitelist.
*/
addToWhitelist(words: string[]): void;
/**
* Remove words from the whitelist.
* @param words - Words to remove from whitelist.
*/
removeFromWhitelist(words: string[]): void;
/**
* Check if a word is whitelisted.
* @param word - The word to check.
* @returns True if whitelisted, false otherwise.
*/
private isWhitelisted;
/**
* Load a built-in language dictionary.
* @param language - The language key.
* @returns True if loaded, false otherwise.
*/
loadLanguage(language: string): boolean;
/**
* Load multiple language dictionaries.
* @param languages - Array of languages to load.
* @returns Number of successfully loaded languages.
*/
loadLanguages(languages: string[]): number;
/**
* Load all supported Indian languages.
* @returns Number of loaded Indian languages.
*/
loadIndianLanguages(): number;
/**
* Load a custom dictionary.
* @param name - Name of the dictionary.
* @param words - Words to add.
*/
loadCustomDictionary(name: string, words: string[]): void;
/**
* Add a single word to the trie.
* @param word - The word to add.
* @returns True if added, false otherwise.
*/
private addWordToTrie;
/**
* Calculate severity from matches.
* @param matches - Array of matches.
* @returns Severity level.
*/
private calculateSeverity;
/**
* Clear all loaded dictionaries and dynamic words.
*/
clearList(): void;
/**
* Set the placeholder character for filtered words.
* @param placeholder - The placeholder character.
*/
setPlaceholder(placeholder: string): void;
/**
* Get the list of loaded languages.
* @returns Array of loaded language keys.
*/
getLoadedLanguages(): string[];
/**
* Get the list of available built-in languages.
* @returns Array of available language keys.
*/
getAvailableLanguages(): string[];
/**
* Get the current configuration of the profanity filter.
* @returns Partial configuration object.
*/
getConfig(): Partial<AllProfanityOptions>;
/**
* Rebuild the profanity trie from loaded dictionaries and dynamic words.
*/
private rebuildTrie;
/**
* Update configuration options for the profanity filter.
* @param options - Partial configuration object.
*/
updateConfig(options: Partial<AllProfanityOptions>): void;
}
/**
* Singleton instance of AllProfanity with default configuration.
*/
declare const allProfanity: AllProfanity;
export default allProfanity;