UNPKG

glin-profanity

Version:

Glin-Profanity is a lightweight and efficient npm package designed to detect and filter profane language in text inputs across multiple languages. Whether you’re building a chat application, a comment section, or any platform where user-generated content

77 lines (75 loc) 2.32 kB
/** * Type definitions for ML-based profanity detection. */ /** * Toxicity categories detected by the TensorFlow.js model. * These map to the civil comments dataset labels. */ type ToxicityLabel = 'identity_attack' | 'insult' | 'obscene' | 'severe_toxicity' | 'sexual_explicit' | 'threat' | 'toxicity'; /** * Result from a single toxicity prediction. */ interface ToxicityPrediction { /** The toxicity category */ label: ToxicityLabel; /** Whether the text matches this category (null if below threshold) */ match: boolean | null; /** Probability scores [non-toxic, toxic] */ probabilities: [number, number]; } /** * Result from ML-based toxicity analysis. */ interface MLAnalysisResult { /** Whether any toxicity was detected */ isToxic: boolean; /** Overall toxicity score (0-1) */ overallScore: number; /** Predictions for each category */ predictions: ToxicityPrediction[]; /** Categories that matched */ matchedCategories: ToxicityLabel[]; /** Processing time in milliseconds */ processingTimeMs: number; } /** * Configuration for the ML toxicity detector. */ interface MLDetectorConfig { /** * Minimum confidence threshold for predictions. * Values below this threshold will return null for match. * @default 0.85 */ threshold?: number; /** * Specific toxicity categories to check. * If not specified, all categories are checked. */ labels?: ToxicityLabel[]; /** * Whether to load the model immediately on instantiation. * If false, model will be loaded on first use. * @default false */ preloadModel?: boolean; } /** * Combined result from both rule-based and ML detection. */ interface HybridAnalysisResult { /** Rule-based detection result */ ruleBasedResult: { containsProfanity: boolean; profaneWords: string[]; }; /** ML-based detection result (null if ML not enabled) */ mlResult: MLAnalysisResult | null; /** Combined decision */ isToxic: boolean; /** Confidence score for the decision */ confidence: number; /** Reason for the decision */ reason: string; } export type { HybridAnalysisResult as H, MLDetectorConfig as M, ToxicityLabel as T, MLAnalysisResult as a, ToxicityPrediction as b };